实验环境:
192.168.132.121 master1
192.168.132.122 master2
192.168.132.123 slave
使用gtid的方式
两个主分别是192.168.132.121和192.168.132.122,一个从端192.168.132.123分别从两个主端同步数据
192.168.132.121 master1配置
[root@master ~]# cat /etc/my.cnf [mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 net_read_timeout = 120 performance_schema_max_table_instances = 400 server-id = 1 gtid_mode=on enforce_gtid_consistency=on log-slave-updates=1 log-bin=master-bin log-bin-index = master-bin.index relay-log = relay-log relay-log-index = relay-log.index binlog_format=row auto_increment_offset=1 auto_increment_increment=3 [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid
[root@master ~]# mysql -uroot -p123456 mysql> grant replication slave on *.* to 'replication'@'192.168.132.122' identified by '1234567'; mysql> grant replication slave on *.* to 'replication'@'192.168.132.123' identified by '1234567';
192.168.132.122 master2配置
[root@master2 ~]# cat /etc/my.cnf [mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 net_read_timeout = 120 performance_schema_max_table_instances = 400 server-id = 2 gtid_mode=on enforce_gtid_consistency=on log-slave-updates=1 log-bin=master-bin log-bin-index = master-bin.index relay-log = relay-log relay-log-index = relay-log.index binlog_format=row auto_increment_offset=2 auto_increment_increment=3 [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid [root@master2 ~]# mysql -uroot -p123456 mysql> grant replication slave on *.* to 'replication'@'192.168.132.121' identified by '1234567'; mysql> grant replication slave on *.* to 'replication'@'192.168.132.123' identified by '1234567';
192.168.132.123 slave配置
[root@slave ~]# cat /etc/my.cnf [mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 net_read_timeout = 120 performance_schema_max_table_instances = 400 server-id = 3 gtid_mode=on enforce_gtid_consistency=on log-slave-updates=1 log-bin=master-bin log-bin-index = master-bin.index relay-log = relay-log relay-log-index = relay-log.index binlog_format=row auto_increment_offset=3 auto_increment_increment=3 [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid [root@slave ~]# mysql -uroot -p123456
配置从库
mysql> change master to master_host='192.168.132.121',master_user='replication',master_port=3306,master_password='1234567',master_auto_position=1 for channel 'master-1'; ERROR 3077 (HY000): To have multiple channels, repository cannot be of type FILE; Please check the repository configuration and convert them to TABLE. mysql> show variables like '%repository%'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | master_info_repository | FILE | | relay_log_info_repository | FILE | +---------------------------+-------+
需要设置convert表
/etc/my.cnf [mysqld] #添加 master_info_repository=TABLE relay_log_info_repository=TABLE
[root@slave ~]# systemctl restart mysqld
[root@slave ~]# mysql -uroot -p123456
mysql> show variables like '%repository%'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | master_info_repository | TABLE | | relay_log_info_repository | TABLE | +---------------------------+-------+ mysql> change master to master_host='192.168.132.121',master_user='replication',master_port=3306,master_password='1234567',master_auto_position=1 for channel 'master-1'; mysql> change master to master_host='192.168.132.122',master_user='replication',master_port=3306,master_password='1234567',master_auto_position=1 for channel 'master-2';
查看从端状态
mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave statusG; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.132.121 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000002 Read_Master_Log_Pos: 1021 Relay_Log_File: relay-log-master@002d1.000002 Relay_Log_Pos: 1236 Relay_Master_Log_File: master-bin.000002 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: 1021 Relay_Log_Space: 1450 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: 1 Master_UUID: 63a7f26f-a196-11e9-a2b2-000c2991dd19 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: 63a7f26f-a196-11e9-a2b2-000c2991dd19:1-3 Executed_Gtid_Set: 5ad46c94-a197-11e9-8513-000c29f004c9:1, 63a7f26f-a196-11e9-a2b2-000c2991dd19:1-3, 662134df-a196-11e9-b432-000c2963fd11:1-4 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: master-1 #通道1,连接master1 Master_TLS_Version: *************************** 2. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.132.122 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000002 Read_Master_Log_Pos: 1270 Relay_Log_File: relay-log-master@002d2.000002 Relay_Log_Pos: 1485 Relay_Master_Log_File: master-bin.000002 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: 1270 Relay_Log_Space: 1699 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: 2 Master_UUID: 662134df-a196-11e9-b432-000c2963fd11 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: 662134df-a196-11e9-b432-000c2963fd11:1-4 Executed_Gtid_Set: 5ad46c94-a197-11e9-8513-000c29f004c9:1, 63a7f26f-a196-11e9-a2b2-000c2991dd19:1-3, 662134df-a196-11e9-b432-000c2963fd11:1-4 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: master-2 #通道2,连接master2 Master_TLS_Version:
查看进程
mysql> show processlist; +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+ | 2 | root | localhost | NULL | Query | 0 | starting | show processlist | | 3 | system user | | NULL | Connect | 140 | Waiting for master to send event | NULL | | 4 | system user | | NULL | Connect | 2130 | Slave has read all relay log; waiting for more updates | NULL | | 5 | system user | | NULL | Connect | 140 | Waiting for master to send event | NULL | | 6 | system user | | NULL | Connect | 1954 | Slave has read all relay log; waiting for more updates | NULL | +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
验证
master1 mysql> create database master1; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | master1 | | mysql | | performance_schema | | sys | +--------------------+ master2 mysql> create database master2; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | master2 | | mysql | | performance_schema | | sys | +--------------------+ slave查看 #已经同步 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | master1 | | master2 | | mysql | | performance_schema | | sys | +--------------------+
多源复制,配置完成