您好,登錄后才能下訂單哦!
本篇文章為大家展示了mysql5.5中怎么實現半同步復制,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
master:192.168.70.101
slave:192.168.70.100
在master上創建用戶repl(只需要在master上創建repl用戶);
mysql>grant replication slave on *.* to 'repl'@'192.168.70.100' identified by 'repl';
在master上my.cnf配置如下:
[mysqld]
datadir=/data/dbdata
user=mysql
port=3306
innodb_data_home_dir = /data/dbdata
socket = /usr/local/mysql/tmp/mysql.sock
server-id=2
log-bin=master-bin
log-bin-index=master-bin.index
sync_binlog = 1
binlog_format = row
character-set-server=utf8
在slave的my.cnf中配置如下:
[mysqld]
innodb_data_home_dir = /data/dbdata
socket = /usr/local/mysql/tmp/mysql.sockport=3306
user=mysql
default-storage-engine=innodb
server-id=3
read_only=on
log-bin=slave-bin
relay_log=slave-relay-bin
relay-log-index=slave-relay-bin.index
binlog_format = row
default-storage-engine=InnoDB
character-set-server=utf8
在master上產看,
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 106 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
在slave上使用change master to命令將slave指向master。然后使用start slave命令啟動復制。
mysql> change master to
->master_host='192.168.70.101',
->master_port=3306,
->master_user='repl',
->master_password='repl',
->master_log_file='master-bin.000003',
->master_log_pos=106;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.70.101
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 106
Relay_Log_File: slave-relay-bin.000003
Relay_Log_Pos: 252
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: Yes ---成功
Slave_SQL_Running: Yes ---成功
flush logs命令強制輪換二進制日志,從而可以得到完整的二進制日志文件。
使用show binlog events in 'master-bin.000004(二進制日志)'\G;
檢查二進制日志里有哪些事件
mysql> show binlog events in 'master-bin.000004'\G;
*************************** 1. row ***************************
Log_name: master-bin.000004
Pos: 4
Event_type: Format_desc
Server_id: 2
End_log_pos: 106
Info: Server ver: 5.1.56-community-log, Binlog ver: 4
*************************** 2. row ***************************
Log_name: master-bin.000004
Pos: 106
Event_type: Query
Server_id: 2
End_log_pos: 200
Info: use `test`; create table tb1(text char(10))
*************************** 3. row ***************************
Log_name: master-bin.000004
Pos: 200
Event_type: Rotate
Server_id: 2
End_log_pos: 244
Info: master-bin.000005;pos=4
3 rows in set (0.00 sec)
//克隆MASTER
[root@server picture]# mysqldump -uroot -pmysql --host=192.168.70.101 --all-databases --master-data=1 > backup-source.sql
--master-data=1選項mysqldump寫change master to 語句,且參數為二進制日志文件及其位置。
然后在slave上恢復備份:
[root@server picture]#mysql -uroot -pmysql --host=192.168.70.100 < backup-source.sql
//克隆SLAVE
//清除Binlog日志
服務器自動清理舊的binlog文件,需設置expire-logs-days選項,這個選項可以作為服務器變量。如果服務重啟后,不受影響,需要在my.cnf設置。
使用purge binary log命令手工清除binlog文件。格式如下:
1,purge binary log before datatime
將清除在給定時間之前的所有文件。
2,purge binary logs to 'filename'
將清除在給定文件之前的所有文件。
//默認情況下,由slave執行的事件沒有被記錄到二進制日志中,如果這個slave是master的一個備份,這時會出現問題。
在my.cnf添加log-slave-updates,以確保來自于master并被執行的語句會被寫入slave的二進制日志中。
切換基本思路:為了讓slave趕上備份服務器,并在正確的位置停止,使用start slave until命令。
slave>start slave until master_log_file='master-bin-000006',master_log_pos=700;
slave>select master_pos_wait('master-bin-000006',700);
//在主從服務器上配置支持半同步復制
[MASTER]
mysql>install plugin rpl_semi_sync_master soname ‘semisync_master.so’;
mysql> SELECT * FROM information_schema.PLUGINS WHERE PLUGIN_NAME='rpl_semi_sync_master'\G;
*************************** 1. row ***************************
PLUGIN_NAME: rpl_semi_sync_master
PLUGIN_VERSION: 1.0
PLUGIN_STATUS: ACTIVE
PLUGIN_TYPE: REPLICATION
PLUGIN_TYPE_VERSION: 1.0
PLUGIN_LIBRARY: semisync_master.so
PLUGIN_LIBRARY_VERSION: 1.3
PLUGIN_AUTHOR: He Zhenxing
PLUGIN_DESCRIPTION: Semi-synchronous replication master
PLUGIN_LICENSE: GPL
LOAD_OPTION: ON
1 row in set (0.00 sec)
mysql> show variables like 'rpl_semi_sync%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
修改rpl_semi_sync_master_enabled=ON,rpl_semi_sync_master_timeout單位時間為毫秒。
mysql> set global rpl_semi_sync_master_enabled = on;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'rpl_semi_sync%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
4 rows in set (0.00 sec)
mysql> show global status like 'Rpl_semi_sync%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 0 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | OFF |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
下面的變量僅僅在主服務的半復制插件初始化后才有效,參數解釋如下:
Rpl_semi_sync_master_clients:表示半復制從服務器的數量。
Rpl_semi_sync_master_net_avg_wait_time:主服務器等候從服務器的平均毫秒時間。(單位為毫秒)
Rpl_semi_sync_master_net_waits:主服務器等候從服務器的總毫秒時間。(單位為毫秒)
Rpl_semi_sync_master_no_times:主服務器關閉半復制的次數。
Rpl_semi_sync_master_no_tx:從服務器提交事物后沒有收到ack確認成功的次數。
Rpl_semi_sync_master_status:決定半服務器是否可以在主服務器上操作,設置為ON表示開啟,OFF表示關閉,為異步模式。
Rpl_semi_sync_master_timefunc_failures:主服務器調用時間函數失敗的次數,例如gettimeofday()。
Rpl_semi_sync_master_tx_avg_wait_time:主服務器等候每個事物的平均時間(毫秒)。
Rpl_semi_sync_master_tx_waits:主服務器等候事物的總次數。
Rpl_semi_sync_master_wait_pos_backtraverse:
Rpl_semi_sync_master_wait_sessions:正在等候從服務器回復的會話數。
Rpl_semi_sync_master_yes_tx:從服務器成功得到答應的提交事物次數。
[SLAVE]
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Rpl_semi_sync_slave_status:
rpl_semi_sync_slave_enabled:
rpl_semi_sync_slave_trace_level:
mysql> show global variables like 'rpl_semi_sync%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_slave_enabled | OFF |
| rpl_semi_sync_slave_trace_level | 32 |
+------------------------------------+-------+
6 rows in set (0.00 sec)
mysql> set global rpl_semi_sync_master_enabled = on;
Query OK, 0 rows affected (0.00 sec)
mysql> set global rpl_semi_sync_slave_enabled = on;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like 'rpl_semi_sync%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+------------------------------------+-------+
6 rows in set (0.00 sec)
//rpl_semi_sync_master_enabled = on :啟動master 支持半同步復制
//rpl_semi_sync_slave_enabled :啟動slave支持半同步復制
當在從服務運行時調整支持半同步復制,必須重啟I/O線程。
mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
在my.cnf配置文件如下:
在主服務器上:
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000 # 1 second
在每個從服務器上:
[mysqld]
rpl_semi_sync_slave_enabled=1
//關于mysql5.5存儲引擎后臺打印
mysql> show engine innodb status\G;
*************************** 1. row ***************************
Type: InnoDB
Name:
Status:
=====================================
120330 14:37:49 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 3 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 367 1_second, 367 sleeps, 35 10_second, 19 background, 19 flush
srv_master_thread log flush and writes: 367
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 32, signal count 32
Mutex spin waits 14, rounds 420, OS waits 0
RW-shared spins 32, rounds 960, OS waits 32
RW-excl spins 0, rounds 0, OS waits 0
Spin rounds per wait: 30.00 mutex, 30.00 RW-shared, 0.00 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 853
Purge done for trx's n:o < 77E undo n:o < 0
History list length 63
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 29, OS thread handle 0x49499940, query id 5743 localhost root
show engine innodb status
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
379 OS file reads, 816 OS file writes, 358 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 276671, node heap has 2 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 3292715
Log flushed up to 3292715
Last checkpoint at 3292715
0 pending log writes, 0 pending chkp writes
313 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 137363456; in additional pool allocated 0
Dictionary memory allocated 1809452
Buffer pool size 8191
Free buffers 7328
Database pages 861
Old database pages 297
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 368, created 493, written 1315
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 861, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread process no. 3289, id 1228495168, state: waiting for server activity
Number of rows inserted 0, updated 0, deleted 0, read 0
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
上述內容就是mysql5.5中怎么實現半同步復制,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。