https://blog.csdn.net/leshami/article/details/50630691
修改/etc/my.cnf文件
1.主库my.cnf文件
[mysqld]
#GTID:
server_id=123 #服务器id
gtid_mode=on #开启gtid模式
enforce_gtid_consistency=on #强制gtid一致性,开启后对于特定create table不被支持
#binlog
log_bin=master-binlog
log-slave-updates=1
binlog_format=row #强烈建议,其他格式可能造成数据不一致
#relay log
skip_slave_start=1
2.从库my.cnf文件
[mysqld]
#GTID:
gtid_mode=on
enforce_gtid_consistency=on
server_id=456
#binlog
log-bin=slave-binlog
log-slave-updates=1
binlog_format=row #强烈建议,其他格式可能造成数据不一致
#relay log
skip_slave_start=1
重启主库和从库的mysql服务
service mysqld restart
从库执行同步之前要关闭主库的写入操作
从库执行同步
a.从库先登录命令行
>mysql -proot -pyourpass
b.指定主库
mysql> CHANGE MASTER TO
-> MASTER_HOST = '192.168.123.229',
-> MASTER_PORT = 3306,
-> MASTER_USER = 'root',
-> MASTER_PASSWORD = 'yourpass',
-> MASTER_AUTO_POSITION = 1;
c.开启同步
mysql> START SLAVE;
如果想在从库中设置不同步主库的数据,可以在[mysqld]节点下设置
[mysqld]
#replicate-do-db=db2
#replicate-ignore-db=db1
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=information_schema.%
replicate-wild-ignore-table=performance_schema.%
replicate-wild-ignore-table=sys.%
需要注意的是即使设置read_only = ON,root这种超级管理员身份还是可以读写数据的
看到类似如下信息说明设置成功
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.123.229
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-binlog.000005
Read_Master_Log_Pos: 194
Relay_Log_File: localhost-relay-bin.000006
Relay_Log_Pos: 415
Relay_Master_Log_File: master-binlog.000005
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: mysql.%,information_schema.%,performance_schema.%,sys.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 194
Relay_Log_Space: 1151
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: 123
Master_UUID: 7562a1d9-49ea-11e8-867c-001c42105038
Master_Info_File: /data/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: 7562a1d9-49ea-11e8-867c-001c42105038:1-18
Executed_Gtid_Set: 7562a1d9-49ea-11e8-867c-001c42105038:1-18
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql的data目录下auto.cnf记录着server_uuid,请确保此文件存在并且主从服务器的server_uuid值不相同,修改该值需要重启mysql服务
每次执行完CHANGE MASTER TO语句后都要使用
mysql> show slave status\G
检查状态,如果提示Last_IO_Error 错误,则需要
先关闭
mysql> stop slave;
再重新执行CHANGE MASTER TO语句和
mysql> start slave;