环境:
roles | IP |
---|---|
主 | 10.1.1.20 |
从 | 10.1.1.21 |
1、修改MASTER的/etc/my.cnf,在[mysqld]段的最后一行加入:
server-id=1 #主数据库的id
log-bin=master-bin #日志路径,作用是从数据库是根据这个日志来复制主数据库的数据。
重启数据库,新建用户rep,设置密码123456,并授权允许在Slave服务器上登陆:
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> use mysql;
Query OK, 1 row affected, 4 warnings (0.00 sec)
mysql > grant all on *.* to 'rep'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql > flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql > show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000014 | 245 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
2、修改SLAVE的/etc/my.cnf,在[mysqld]段的最后一行加入:
server-id=2#这个id必须不能和主数据库相同
read-only=on#设置该数据库是只读状态
relay-log=relay-bin #日志
重启MySQL,然后使用root登陆,建立主从同步相关信息:
mysql>change master to \
master_host='10.1.1.20',master_user='rep',master_password='123456',master_log_file='master-bin.000005',master_log_pos=245;
启动SLAVE,并检查SLAVE状态:
mysql> start slave; #启动SLAVE
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;# 检查SLAVE状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.20
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 406
Relay_Log_File: relay-bin.000004
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000001
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: 406
Relay_Log_Space: 615
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: a0a896e0-803d-11e9-a717-000c29c380a1
Master_Info_File: /usr/local/mysql/data/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:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
上面的“Slave_IO_Running: Yes”和“Slave_SQL_Running: Yes”说明主从同步已经通了,如果不是yes,则需要排查原因了。
3、测试验证:
在主库上删除自带的test库,则可以看到从库上的test库也消失了。在主库新建111库,则从库也出现了111库。
4、报错案例:
Slave_IO_Running: No
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: 993
Relay_Log_Space: 120
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
上述出现“Slave_IO_Running: No”和“Last_IO_Error”两处错误,从第二个错误可以看出MySQL的UUID错误,提示:主从库的UUID是一样的。因为我们的虚拟机是克隆过来的,克隆之前MySQL已经安装好,所以克隆过的MySQL的UUID和之前的UUID一致,产生冲突。所以,解决此问题,需要修改从库的UUID,UUID记录在/usr/local/mysql/data/auto.cnf中,将UUID的值任意修改一位就可以:
vim /usr/local/mysql/data/auto.cnf
[auto]
server-uuid=d85da196-7d0c-11e9-924a-000c29c380a2
修改完成后,重启MySQL,登陆再次查看slave 状态,这次已经恢复正常:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes