MySQL複製總結

 

MySQL複製總結

 

1、MySQL複製原理

MySQL的複製涉及到三個線程,主庫的DUMP線程,從庫的IO線程和SQL線程。

主從同步的詳細過程如下:

1、slave端執行start slave後,連接主服務器,主服務器驗證連接後,爲從服務器開啓一個binlog dump線程。

2. 主庫的binlog dump線程根據從庫IO線程的請求將binlog中的內容發送到從庫。

3. 從庫的IO線程接受到主庫binlog dump線程發送的binlog事件後,將其寫到本地的relay-log。

4. 從庫的SQL線程重放relay-log中的事件。

 

2、ySQL三種複製方式

2.1、異步複製

MySQL異步複製是默認方式,即主庫執行完Commit後,在主庫寫入Binlog日誌後即可成功返回客戶端,無需等等Binlog日誌傳送給從庫,一旦主庫宕機,有可能會丟失日誌。

異步複製的優缺點

性能好,可能丟失數據。

 

2.2、半同步複製

半同步複製,是等待其中一個從庫也接收到Binlog事務併成功寫入Relay Log之後,才返回Commit操作成功給客戶端;如此半同步就保證了事務成功提交後至少有兩份日誌記錄,一份在主庫Binlog上,另一份在從庫的Relay Log上,從而進一步保證數據完整性;半同步複製很大程度取決於主從網絡RTT(往返時延)。

 

參數設置:rpl_semi_sync_master_wait_point=after_commit

 

存在的問題:

 

缺點1: 幻讀

當用戶發起一個事務,該事務已經寫入redo日誌和binlog日誌,但該事務還沒寫入從庫,此時處在waiting slave dump處,此時另一個用戶可以讀取到這條數據,而他自己卻不能。

 

缺點2:數據丟失

一個事務在waiting slave dump處crash後,主庫將比從庫多一條數據。

 

2.3、強半同步複製

增強半同步通過調整寫Storage Commit和Waiting Slave dump的順序,解決幻讀和數據丟失的風險。

參數設置:rpl_semi_sync_master_wait_point=after_sync

特定:

改善1:解決幻讀

當用戶發起一個事務,該事務寫入二進制後,便向從庫進行同步,此時其他用戶無法讀取到該數據,解決了幻讀

 

改善2:解決數據丟失

一個事務在waiting slave dump處crash掉後,可以通過觀察從庫上是否存在主庫的last gtid值,如果存在,這條數據正常恢復,如果不存在則刪除主庫的那條多餘的GTID值,然後恢復,保證了數據的完整性;

 

3、主從配置

 

mysql_5.6下的主從複製

試驗環境:

主服務器:Linux 5.4  5.6.28 源碼 IP:10.100.1.220

從服務器:Linux 5.4  5.6.28 源碼 IP:10.100.1.221

配置:

一、主服務器

1.1、創建一個複製用戶,具有replication slave 權限。

mysql> grant replication slave on *.* to 'slave001'@'10.100.1.221' identified by 'slave001';

1.2、編輯my.cnf文件

vi /etc/my.cnf

添加:

server-id=1

1.3、並開啓log-bin二進制日誌文件

log-bin=mysql-bin

1.4、啓動mysql數據庫

service mysql start

1.5、得到binlog日誌文件名和偏移量

mysql>show master status;  

+——————+———-+————–+——————+  

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |  

+——————+———-+————–+——————+  

| mysql-bin.0000010 | 106| | |  

+——————+———-+————–+——————+

1.6、備份要同步的數據庫

mysqldump -uroot -p --lock-tables --events --triggers --routines --flush-logs --master-data=2 --databases xtra_test > /tmp/backup/db.sql

或:

mysqldump --master-data=2 --single-transaction --events -R --flush-logs --triggers --databases xtra_test > all.sql

1.7、查看position

[root@mytest01 data]# grep MASTER /tmp/backup/db.sql

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.0000010', MASTER_LOG_POS=106;

[root@mytest01 data]#

二、從服務器

2.1、編輯my.cnf文件

vi /etc/my.cnf

添加

server-id=2

注:需要把默認的server-id=1去掉

不要嘗試把master配置屬性寫在my.cnf 中,5.1.7以後,mysql已經不支持這樣做了

2.2、啓動從數據庫

service mysql start

2.3、把生產的數據導進從服務器:

mysql -uroot -proot123</tmp/backup/db.sql

2.3、對從數據庫進行相應設置

mysql> change master to  

master_host='10.100.1.220',

master_user='slave001',

master_password='slave001',

master_log_file='mysql-bin.000010',

master_log_pos=106;

2.4、啓動從服務器slave線程

mysql>start slave;

執行show processlist命令顯示以下進程:

mysql>show processlist\G  

 

*************************** 2. row ***************************  

Id: 2  

User: system user  

Host:  

db: NULL  

Command: Connect  

Time: 2579  

State: Has read all relay log; waiting for the slave I/O thread to update it

Info: NULL表示slave已經連接上master,開始接受並執行日誌

2.5、查看slave線程狀態

mysql>show slave status;  

*************************** 1. row ***************************  

Slave_IO_State: Waiting for master to send event  

Master_Host: 10.100.1.220

Master_User: repl  

Master_Port: 3306  

Connect_Retry: 60  

Master_Log_File: mysql-bin.0000010  

Read_Master_Log_Pos: 106  

Relay_Log_File: centos-relay-bin.000002  

Relay_Log_Pos: 529  

Relay_Master_Log_File: mysql-bin.0000010  

Slave_IO_Running: Yes  

Slave_SQL_Running: Yes  

Replicate_Do_DB:  

Replicate_Ignore_DB:  

Replicate_Do_Table:  

Replicate_Ignore_Table:  

Replicate_Wild_Do_Table:  

Replicate_Wild_Ignore_Table:  

Last_Errno: 0  

Last_Error:  

Skip_Counter: 0  

Exec_Master_Log_Pos: 106  

Relay_Log_Space: 830  

Until_Condition: None  

Until_Log_File:  

Until_Log_Pos: 0  

Master_SSL_Allowed: No  

Master_SSL_CA_File:  

Master_SSL_CA_Path:  

Master_SSL_Cert:  

Master_SSL_Cipher:  

Master_SSL_Key:  

Seconds_Behind_Master: 0  

Master_SSL_Verify_Server_Cert: No  

Last_IO_Errno: 0  

Last_IO_Error:  

Last_SQL_Errno: 0  

Last_SQL_Error:  

1 row in set (0.00 sec)

驗證是否配置正確

在從服務器上執行

mysql> show slave status\G

Waiting for master to send event  

Slave_IO_Running: Yes  

Slave_SQL_Running: Yes

如以上二行同時爲Yes 說明配置成功

PS:show slave status\G 後不要添加 ;號, 不然會出 ERROR

 

測試:

1、在主服務器test數據庫中創建user表

mysql>use xtra_test;  

mysql>create table user(id int);

2、在從服務器中查看user表

mysql>use xtra_test;

mysql> show tables like ‘user’;  

+———————-+  

| Tables_in_test(user) |  

+———————-+  

| user |  

+———————-+  

1 row in set (0.00 sec)

說明主從數據同步成功。

常見問題歸納:

1.在從數據庫中查看slave狀態時出現

The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the –replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it)

說明主從服務器裏my.cnf中的server-id有相同的。

解決辦法:

修改my.cnf裏的server-id,並重啓數據庫服務。my.cnf文件默認有server-id=1

其它說明

主服務器my.cnf

#binlog-do-db=需要備份的數據庫名,可寫多行

#binlog-ignore-db=不需要備份的數據庫名,可寫多行

從服務器my.cnf

配置已經無用,要在mysql中執行

 

三、手動更新

 

在主服務器上執行

mysql>flush tables with read lock;  

Query OK,rows affected (0.01 sec)  

mysql>show master status;  

+——————+———-+————–+——————+  

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |  

+——————+———-+————–+——————+  

| mysql-bin.0000011 | 260| | |  

+——————+———-+————–+——————

在從服務器上執行

 

mysql>select master_pos_wait(‘mysql-bin.0000011′,’260′);  

+————————————————–+  

| master_pos_wait(‘mysql-bin.0000011′,’260′) |  

+————————————————–+  

| 0 |  

+————————————————–+  

1 row in set (0.01 sec)

注意,執行這條語句的時候,可能會有MySQL server has gone away 錯誤,木有關係,多執行幾次就好了

 

同步完成後,在主服務器上執行解鎖

mysql>unlock tables;

四、只複製特定數據庫

主數據庫:

vi /etc/my.cnf添加以下配置

binlog-do-db    =test

binlog-ignore-db = mysql

 

從數據庫:

vi /etc/my.cnf添加以下配置

replicate-do-db=test

replicate-ignore-db=mysql

 

重啓主、從數據庫。

service  mysql  restart

 

 

4、配置半同步

 

查詢mysql複製有沒有使用半同步,

 

mysql> show variables like 'rpl%';

 

+-------------------+-------+

 

| Variable_name    | Value |

 

+-------------------+-------+

 

| rpl_recovery_rank | 0    |

 

+-------------------+-------+

 

1 row in set (0.00 sec)

 

或者使用show status like  'rpl%';

 

啓用半同步模式:

 

在master執行

 

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';

 

Query OK, 0 rows affected (0.03 sec)

 

mysql> set global rpl_semi_sync_master_enabled=0;

 

Query OK, 0 rows affected (0.00 sec)

 

 

mysql> show variables like 'rpl%';

 

+------------------------------------+-------+

 

| Variable_name                      | Value |

 

+------------------------------------+-------+

 

| rpl_recovery_rank                  | 0    |

 

| 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    |

 

+------------------------------------+-------+

 

5 rows in set (0.00 sec)

 

 

mysql> set global rpl_semi_sync_master_enabled=1;

 

Query OK, 0 rows affected (0.00 sec)

 

mysql> show variables like 'rpl%';

 

+------------------------------------+-------+

 

| Variable_name                      | Value |

 

+------------------------------------+-------+

 

| rpl_recovery_rank                  | 0    |

 

| 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_master_enabled 的值是0(off)或者1(on),默認是0.

 

rpl_semi_sync_master_timeout的值默認是10000(10s)

 

5 rows in set (0.00 sec)

 

/etc/my.cnf 添加參數

rpl_semi_sync_master_enabled    = 1

 

在slave執行如下操作:

 

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';

 

Query OK, 0 rows affected (0.03 sec)

 

 

 

mysql> set global rpl_semi_sync_slave_enabled=1;

 

Query OK, 0 rows affected (0.00 sec)

 

 

 

mysql> show variables like 'rpl%';

 

+---------------------------------+-------+

 

| Variable_name                  | Value |

 

+---------------------------------+-------+

 

| rpl_recovery_rank              | 0    |

 

| rpl_semi_sync_slave_enabled    | ON    |

 

| rpl_semi_sync_slave_trace_level | 32    |

 

+---------------------------------+-------+

 

3 rows in set (0.00 sec)

 

 

mysql> show status like 'rpl%';

 

+----------------------------+-------------+

 

| Variable_name              | Value      |

 

+----------------------------+-------------+

 

| Rpl_semi_sync_slave_status | ON          |

 

| Rpl_status                | AUTH_MASTER |

 

+----------------------------+-------------+

 

2 rows in set (0.01 sec)

 

mysql> stop slave io_thread;

 

Query OK, 0 rows affected (0.00 sec)

 

mysql> start slave io_thread;

 

Query OK, 0 rows affected (0.00 sec)

 

 

rpl_semi_sync_slave_enabled的值是0(off)或者1(on),默認是0.

在/etc/my.cnf 添加參數

rpl_semi_sync_slave_enabled    = 1

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章