[root@slave1 ~]# !diff diff /etc/my.cnf slave2.cnf 62c62 < server-id = 11 --- > server-id = 12 63a64 > replicate_do_db = test 65,69d65 < ssl-ca=/usr/local/mysql/ssl/cacert.pem < ssl-key=/usr/local/mysql/ssl/mysql.key < ssl-cert=/usr/local/mysql/ssl/mysql.crt < binlog-format=ROW < log-bin=slave1-bin 81c77 < report-host=slave1.sanyu.com --- > report-host=slave2.sanyu.com [root@slave1 ~]# scp slave2.cnf 10.10.10.63:/etc/my.cnf [root@slave1 ~]# service mysqld restart [root@slave2 ~]# service mysqld start slave1> show variables like '%gtid%'; +--------------------------+-----------+ | Variable_name | Value | +--------------------------+-----------+ | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | +--------------------------+-----------+ 6 rows in set (0.01 sec) slave1> show variables like '%uuid%'; +---------------+--------------------------------------+ | Variable_name | Value | +---------------+--------------------------------------+ | server_uuid | e1edf259-08d4-11e3-bc72-000c29bc01c7 | +---------------+--------------------------------------+ 1 row in set (0.00 sec) salve2> show variables like '%gtid%'; +--------------------------+-----------+ | Variable_name | Value | +--------------------------+-----------+ | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | +--------------------------+-----------+ 6 rows in set (0.00 sec) salve2> show variables like '%uuid%'; +---------------+--------------------------------------+ | Variable_name | Value | +---------------+--------------------------------------+ | server_uuid | e14444e3-0da9-11e3-9bf5-000c294f12ec | +---------------+--------------------------------------+ 1 row in set (0.02 sec)
授权复制用户:
slave1> GRANT REPLICATION SLAVE ON *.* TO [email protected] IDENTIFIED BY 'redhat'; slave1> flush privileges;
导入数据
[root@slave2 ~]# mysql shop
连接主服务器
salve2> CHANGE MASTER TO MASTER_HOST='slave1.sanyu.com', MASTER_USER='repluser', MASTER_PASSWORD='redhat', MASTER_AUTO_POSITION=1;
开始复制
salve2> start slave; slave1> create table t1(id int); Query OK, 0 rows affected (0.31 sec)
测试test库
slave1> show tables; +----------------+ | Tables_in_test | +----------------+ | t1 | +----------------+ 1 row in set (0.00 sec) slave2> show tables; +----------------+ | Tables_in_test | +----------------+ | t1 | +----------------+ 1 row in set (0.00 sec) slave2> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: slave1.sanyu.com Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: slave1-bin.000002 Read_Master_Log_Pos: 952 Relay_Log_File: slave2-relay-bin.000003 Relay_Log_Pos: 1124 Relay_Master_Log_File: slave1-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: shop,test 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: 952 Relay_Log_Space: 1541 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: 11 Master_UUID: e1edf259-08d4-11e3-bc72-000c29bc01c7 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 the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: e1edf259-08d4-11e3-bc72-000c29bc01c7:3-7 Executed_Gtid_Set: e14444e3-0da9-11e3-9bf5-000c294f12ec:1, e1edf259-08d4-11e3-bc72-000c29bc01c7:1-7 Auto_Position: 1 1 row in set (0.01 sec)
测试shop库
master> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | +---------+-----------+---------------------+ 4 rows in set (0.00 sec) slave1> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | +---------+-----------+---------------------+ 4 rows in set (0.01 sec) slave2> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | +---------+-----------+---------------------+ 4 rows in set (0.00 sec)
插入一条记录
master> insert into shop.ecs_users(user_name,last_time) values ('slave1',now()); Query OK, 1 row affected, 7 warnings (10.14 sec)
一条简单的记录花了10s+,是启用半同步复制的结果
slave1> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | +---------+-----------+---------------------+ 4 rows in set (0.00 sec) slave1上并没有该记录 slave1> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 10.10.10.61 Master_User: repluser Master_Port: 3306 Connect_Retry: 10 Master_Log_File: mysql-bin.000017 Read_Master_Log_Pos: 484 Relay_Log_File: slave1-relay-bin.000002 Relay_Log_Pos: 266 Relay_Master_Log_File: mysql-bin.000017 Slave_IO_Running: No Slave_SQL_Running: Yes Replicate_Do_DB: shop 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: 484 Relay_Log_Space: 548 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: Yes Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem Master_SSL_CA_Path: /usr/local/mysql/ssl Master_SSL_Cert: /usr/local/mysql/ssl/mysql.crt Master_SSL_Cipher: Master_SSL_Key: /usr/local/mysql/ssl/mysql.key Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 1593 Last_IO_Error: The slave IO thread stops because the master has GTID_MODE OFF and this server has GTID_MODE ON Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 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 the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: 130826 13:19:52 Last_SQL_Error_Timestamp: Master_SSL_Crl: /usr/local/mysql/ssl/cacert.pem Master_SSL_Crlpath: /usr/local/mysql/ssl Retrieved_Gtid_Set: Executed_Gtid_Set: e1edf259-08d4-11e3-bc72-000c29bc01c7:1-7 Auto_Position: 0 1 row in set (0.00 sec)
可见,一旦启用了基于GTID的复制,所有结点都应该启用