mysql5.7基于binlog的主从复制
主库:192.168.153.128
备库:192.168.153.231
版本:mysql5.7
主库
1.主库创建复制用户:
mysql>CREATE USER 'repl'@'192.168.153.%' IDENTIFIED BY 'mysql';
mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.153.%';
备机验证登录:
#mysql -u repl -p -P 3308 -h 192.168.153.128
2.获取主库的日志信息
mysql>flush tables with read lock;
mysql>show master status;
mysql-bin.000021 1044
3.主库数据生成备份并传到备库
a)innodb推荐方式
#mysqldump --all-databases --master-data -u root -p -P 3308 > dbdump.db ##mysqldump方
式导出所有数据库数据到dbdump.db文件,--master-data表示导出数据直接加上change master to参数
以便备库使用
b) 如果使用文件拷贝的办法:
将主库临时关闭,并将相关文件拷贝到从库上
#tar cf /tmp/db.tar ./data
scp /tmp/db.tar root@192.168.153.128:/root
主库释放锁:
mysql>unlock tables;
4.备库同步主库备份开启slave
检查server_id和uuid与主库是否相同,相同需要修改,否则同步报错
server_id:修改my.cnf文件
#vi my.cnf
uuid:修改auto.cnf文件
#rm -rf auto.cnf
修改后重启备库mysql
service mysqld restart
进入备份目录,source命令应用备份
mysql>source dbdump.db
主备同步:
mysql>CHANGE MASTER TO
MASTER_HOST='192.168.153.128',
MASTER_PORT=3306,
MASTER_USER='repl',
MASTER_PASSWORD='mysql',
MASTER_LOG_FILE='mysql-bin.000021',
MASTER_LOG_POS=1044;
开启备库同步
需要设置autocommit参数设置为on开启,不然备库不会自动commit;
mysql>start slave;
查看备库同步状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.153.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000021
Read_Master_Log_Pos: 1044
Relay_Log_File: MySQL5-relay-bin.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000021
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: 1044
Relay_Log_Space: 528
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: 7
Master_UUID: 735c0b14-3454-11ea-af4e-000c2906eea4
Master_Info_File: /data/3306/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:
Executed_Gtid_Set: 735c0b14-3454-11ea-af4e-000c2906eea4:1,
a61f4d3c-7f49-11eb-8738-000c294c2be0:1-154
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
查看备库参数信息是否正确
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
主库备份前master状态
Relay_Master_Log_File: mysql-bin.000021
Exec_Master_Log_Pos: 1044
同步验证
主库删除测试表dept
delete from dept;
备库验证查看
select count(*) from dept;
至此mysql5.7基于binlog主备复制完成。