mysql多源復制(多主一從)配置


mysql多源復制(多主一從)配置

應用場景

  • 數據匯總,可將多個主數據庫同步匯總到一個從數據庫中,方便數據統計分析。
  • 讀寫分離,從庫只用於查詢,提高數據庫整體性能。

1.1.主庫配置

my.cnf

#確保唯一

server-id=1

#作為Master要開啟binlog

log-bin=mysql-bin

#binlog format有三種形式:Statement、Mixed、Row,默認設置為mixed

binlog-format=Row

#需要同步的庫,不指定默認同步全部庫

binlog-do-db=hi_db

#不需要同步的庫

binlog-ignore-db=test

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

binlog-ignore-db=performance_schema

binlog-ignore-db=sys

#這個比較重要,直接影響同步的性能 延時等問題.mysql5.7多源復制參數,5.7之前版本可不加

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#GTID模式

gtid-mode=on

enforce-gtid-consistency

1.2創建主庫授權從庫同步的用戶

mysql>grant replication slave on *.* to 'slave'@'192.168.100.%' identified by '308731044';

replication slave:遠程用戶備份權限

*.*:第一個星號代表庫,第二個星號代表數據庫里的表。可指定庫和表

'slave'@'192.168.100.%':@前為用戶名,@后為授權的IP段(就是允許那些IP使用這個賬號權限訪問)

'308731044':遠程備份用戶密碼

1.3刷新設置(修改的內容刷新到數據庫配置里 )

mysql>flush privileges;

查看和刪除授權授權用戶(刪除不使用的賬號)

log_bin是否開啟

mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set

查看master狀態

mysql> show master status \G *************************** 1. row *************************** File: mysql-bin.000003 Position: 438 Binlog_Do_DB: test3 Binlog_Ignore_DB: mysql,information_schema,performation_schema,sys

1.4從庫配置

#確保唯一

server-id = 3

#復制的庫,不指定默認備份全部庫

replicate-do-db = master1

replicate-do-db = master2

#不復制的庫

replicate-ignore-db = mysql

replicate-ignore-db = information_schema

replicate-ignore-db = performance_schema

replicate-ignore-db = sys

#binlog日志設置

relay-log = /home/mysql/data/

mysqld-relay-bin

log-slave-updates = ON

slave-parallel-type=LOGICAL_CLOCK

relay_log_recovery=ON

#超時

slave_net_timeout = 30

#復制並發數設置

slave_parallel_workers = 16

#從庫復制跳過錯誤

slave-skip-errors = 1062,1053,1146,1213,1264,1205,1396

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#mysql5.7多源復制必須添加的參數(不加報錯),5.7版本之前不用加

master_info_repository=TABLE

relay_log_info_repository=TABLE

#GTID模式(使用GTID就可以不用記錄備份位置點)

gtid-mode=on

enforce-gtid-consistency

1.5.從庫配置用戶及binlog位置信息(推薦使用GTID模式)

#GTID模式配置

CHANGE MASTER TO MASTER_HOST='192.168.1.131', MASTER_USER='slave', MASTER_PORT=3451, MASTER_PASSWORD='xxxxxx', MASTER_AUTO_POSITION = 1 FOR CHANNEL 'master-1';

主庫配置可以使用到多個主庫上,從庫添加相應的配置即可(GTID模式配置)

 

#Binlog模式

CHANGE MASTER TO
MASTER_HOST='10.60.1.126',
MASTER_PORT=3306,
MASTER_USER='root',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=154
for channel '126';

CHANGE MASTER TO
MASTER_HOST='10.60.1.127',
MASTER_PORT=3306,
MASTER_USER='root',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=154
for channel '127';

stop slave;     //停止同步
start slave;     //開始同步
//必須和【主庫】的信息匹配。
CHANGE MASTER TO
MASTER_HOST='192.168.10.212',     //主庫IP
MASTER_PORT=3600,                       //主庫端口
MASTER_USER='root',                     //訪問主庫且有同步復制權限的用戶
MASTER_PASSWORD='123456',      //登錄密碼
//【關鍵處】從主庫的該log_bin文件開始讀取同步信息,主庫show master status返回結果
MASTER_LOG_FILE='mysql-bin.000003',
//【關鍵處】從文件中指定位置開始讀取,主庫show master status返回結果
MASTER_LOG_POS=438
for channel '126';            //定義通道名稱

查看同步狀態

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.212 Master_User: slave Master_Port: 4300 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 1860 Relay_Log_File: 0ad84f013600-relay-bin-300.000002 Relay_Log_Pos: 1742 Relay_Master_Log_File: mysql-bin.000003 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: 1860 Relay_Log_Space: 1960 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 300 Master_UUID: 4efae154-ebd4-11e8-bf7d-0242ac110007 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: 300 Master_TLS_Version: *************************** 2. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.212 Master_User: slave Master_Port: 4400 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 2461 Relay_Log_File: 0ad84f013600-relay-bin-400.000002 Relay_Log_Pos: 1187 Relay_Master_Log_File: mysql-bin.000003 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: 2461 Relay_Log_Space: 1405 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 400 Master_UUID: 8fb0aa81-ebd4-11e8-ac5b-0242ac110009 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: 400 Master_TLS_Version: *************************** 3. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.212 Master_User: slave Master_Port: 4500 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 2151 Relay_Log_File: 0ad84f013600-relay-bin-500.000002 Relay_Log_Pos: 626 Relay_Master_Log_File: mysql-bin.000003 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: 2151 Relay_Log_Space: 844 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 500 Master_UUID: b87704d2-ebd5-11e8-a90c-0242ac11000a Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: 500 Master_TLS_Version: 3 rows in set (0.00 sec) 

可以看見設置三個的主從同步通道的所有狀態信息。
只有【Slave_IO_Running】和【Slave_SQL_Running】都是Yes,則同步是正常的。
如果是No或者Connecting都不行,可查看mysql-error.log,以排查問題。

mysql> show variables like 'log_error%'; +---------------------+--------+ | Variable_name | Value | +---------------------+--------+ | log_error | stderr | | log_error_verbosity | 3 | +---------------------+--------+ 2 rows in set

配置完成,則【從庫128】開始自動同步。

若需要單獨啟動或停止某個同步通道,可使用如下命令:
start slave for channel '126';     //啟動名稱為300的同步通道
stop slave for channel '127';     //停止名稱為300的同步通道

驗證數據同步

建庫

使用root賬號登錄【主庫300】,創建test3數據庫 

mysql> CREATE DATABASE test3;
Query OK, 1 row affected (0.00 sec)

mysql> USE test3;
Database changed

建表

在【主庫300】中創建user表

CREATE TABLE `user` (   `id` bigint(20) NOT NULL AUTO_INCREMENT,   `name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,   `age` tinyint(3) unsigned NOT NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 

新增

在【主庫300】中向user表插入一條數據:

mysql> use test3; Database changed mysql> INSERT INTO user (id, name, age) VALUES (300, 'Tom', 18); Database changed mysql> SELECT * FROM user; +-----+------+-----+ | id | name | age | +-----+------+-----+ | 300 | Tom | 18 | +-----+------+-----+ 1 row in set (0.00 sec)

在【從庫10345】中查詢user表數據:

mysql> use test3;
Database changed
mysql> SELECT * FROM user;
+-----+------+-----+
| id | name | age | +-----+------+-----+ | 300 | Tom | 18 | +-----+------+-----+ 1 row in set (0.00 sec) 

新增記錄同步成功。

更新

在【主庫126】中修改剛才插入的數據:

mysql> UPDATE user SET name='Peter' where id=300; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0  mysql> select * from user; +-----+-------+-----+ | id | name | age | +-----+-------+-----+ | 300 | Peter | 18 | +-----+-------+-----+ 1 row in set (0.00 sec)

在【從庫10345】中查詢user表數據:

mysql> select * from user;
+-----+-------+-----+
| id | name | age | +-----+-------+-----+ | 300 | Peter | 18 | +-----+-------+-----+ 1 row in set (0.00 sec)

更新記錄同步成功。

刪除

在【主庫126】中刪除剛才更新的數據:

mysql> DELETE FROM user WHERE id=300; Query OK, 1 row affected (0.00 sec)  mysql> select * from user; Empty set (0.00 sec)

在【從庫10345】中查詢user表數據:

mysql> select * from user; Empty set (0.00 sec)

刪除記錄同步成功。
注:【主庫126】、【主庫127】的驗證操作與上述類似。

補充:

      • 如果【主服務器】重啟mysql服務,【從服務器】會等待與【主服務器】重連。當主服務器恢復正常后,從服務器會自動重新連接上主服務器,並正常同步數據。
      • 如果某段時間內,【從數據庫】服務器異常導致同步中斷(可能是同步點位置不匹配),可以嘗試以下恢復方法:進入【主數據庫】服務器(正常),在bin-log中找到【從數據庫】出錯前的position,然后在【從數據庫】上執行change master,將master_log_file和master_log_pos重新指定后,開始同步。 

 

④ 相關操作:

查看單個channel的狀態:

show slave status for channel 't10'\G 

停止單個channel的同步:

stop slave for channel 't10';

開啟單個channel的同步:

start slave for channel 't10';

重置單個channel:

reset slave all for channel 't10';

查看所有channel:

show slave status\G

停止所有channel:

stop slave;

開啟所有channel:

start slave;

跳過一個channel的報錯(類似MariaDB的default_master_connection):

處理方法:先停止所有的channel,再執行 sql_slave_skip_counter,接着開啟報錯的channel,最后開啟所有的channel。

復制代碼
一:
#stop all slaves
stop slave;

# set skip counter
set global sql_slave_skip_counter=1;

# start slave that shall skip one entry
start slave for channel 't10';

set global sql_slave_skip_counter=0;

# start all other slaves
start slave; 

二:
也可以直接停掉錯誤的channel,再skip:
stop slave for channel 't10';

set global sql_slave_skip_counter=1;

start slave for channel 't10';
復制代碼

⑤ 監控系統庫performance_schema增加了一些replication的監控表:

⑤ 監控系統庫performance_schema增加了一些replication的監控表:

復制代碼
mysql> show tables like 'replicat%';
+-------------------------------------------+
| Tables_in_performance_schema (replicat%)  |
+-------------------------------------------+
| replication_applier_configuration         |###查看各個channel是否配置了復制延遲
| replication_applier_status                |###查看各個channel是否復制正常(service_state)以及事務重連的次數
| replication_applier_status_by_coordinator |###查看各個channel是否復制正常,以及復制錯誤的code、message和時間
| replication_applier_status_by_worker      |###查看各個channel是否復制正常,以及並行復制work號,復制錯誤的code、SQL和時間
| replication_connection_configuration      |###查看各個channel的連接配置信息:host、port、user、auto_position等
| replication_connection_status             |###查看各個channel的連接信息
| replication_group_member_stats            |###
| replication_group_members                 |###
+-------------------------------------------+
復制代碼

...

2,在線調整Replication Filter

在上面搭建的主從基礎上,進行過濾規則的添加,比如需要過濾dba_test數據庫:

復制代碼
先關閉sql線程,要是在多源復制中,是關閉所有channel的sql thread。
mysql> stop slave sql_thread;
Query OK, 0 rows affected (0.01 sec)

#過濾1個庫
mysql> CHANGE REPLICATION FILTER REPLICATE_IGNORE_DB=(dba_test);

#過濾2個庫
mysql> CHANGE REPLICATION FILTER REPLICATE_IGNORE_DB=(dba_test1,dba_test);
Query OK, 0 rows affected (0.00 sec)

mysql> start slave sql_thread;
Query OK, 0 rows affected (0.04 sec)
復制代碼

通過show slave status 查看:

              Replicate_Do_DB: 
          Replicate_Ignore_DB: dba_test1,dba_test
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 

比如設置同步dba_test2庫中t1開頭的表

復制代碼
mysql> stop slave sql_thread;
Query OK, 0 rows affected (0.01 sec)

mysql> CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE =('dba_test2.t1%');
Query OK, 0 rows affected (0.00 sec)

mysql> start slave sql_thread;
Query OK, 0 rows affected (0.04 sec)
復制代碼

還原成默認值,即設置成空():

復制代碼
mysql> stop slave sql_thread;
Query OK, 0 rows affected (0.01 sec)

mysql> CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE=();
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE REPLICATION FILTER Replicate_Ignore_DB=();
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE REPLICATION FILTER Replicate_Wild_Ignore_Table=();
Query OK, 0 rows affected (0.00 sec)

mysql> start slave sql_thread;
Query OK, 0 rows affected (0.04 sec)
復制代碼

用紅色字體標記的這個參數就是設置在配置文件的參數,如上面的幾個參數既可以在命令行里執行(5.7)也可以在配置文件里添加。注意一點是在線執行完后,一定要在配置文件里寫,以免重啟后失效。

保證主從關系對應用程序用戶只讀不寫

只讀是為了保持從庫與主庫之間的信息對等,使同步機制不被破壞。

數據庫只讀設置方式

 設定全局變量“read_only=1”只讀模式開啟的解鎖命令為設定“read_only=0”;

設定全局鎖“flush tables with read lock;”,對應的解鎖模式命令為:“unlock tables;”

從庫只讀不影響同步

my.cnf或my.ini

read_only=1

為了保證主從同步可以一直進行,在slave庫上要保證具有super權限的root等用戶只能在本地登錄,不會發生數據變化,其他遠程連接的應用用戶只按需分配為select,insert,update,delete等權限,保證沒有super權限,則只需要將salve設定“read_only=1”模式,即可保證主從同步,又可以實現從庫只讀。

從庫完全只讀設置

my.cnf或my.ini

read_only=1

數據庫鎖表:

flush tables with read lock

為了確保所有用戶,包括具有super權限的用戶也不能進行讀寫操作,就需要執行給所有的表加讀鎖的命令 “flush tables with read lock;”,這樣使用具有super權限的用戶登錄數據庫,想要發生數據變化的操作時,也會提示表被鎖定不能修改的報錯。這樣通過 設置“read_only=1”和“flush tables with read lock;”兩條命令,就可以確保數據庫處於只讀模式,不會發生任何數據改變,在MySQL進行數據庫遷移時,限定master主庫不能有任何數據變化,就可以通過這種方式來設定。

常用賬號賦予權限

# 創建用戶

CREATE USER 'boonya'@'%' IDENTIFIED BY 'boonya@2019';

#為用戶授權

GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON *.* TO 'boonya'@'%' IDENTIFIED BY 'boonya@2019';

新建用戶不是超級用戶root,所以無法插入數據了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM