问题
1、 主从复制的原理?
2、 show master status、show slave status 返回的数值有什么含义?
3、 主从不一致,如何搭建从库?
MySQL 主从复制的原理
主库: binlog dump 线程。
从库: io线程、SQL线程。
在主库上执行下面sql,可以看到主库的dump线程。
show processlist;
show master status、show slave status 返回的数值有什么含义?
show master status ;查看主库的状态。如果没有值,可以看看是否开启binlog。
mysql> SHOW MASTER STATUS\G
*************************** 1. row ***************************
File: master-bin.000002
Position: 1307 //主库二进制日志的位置
Binlog_Do_DB: test
Binlog_Ignore_DB: manual, mysql
Executed_Gtid_Set: 3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5
1 row in set (0.00 sec)
每隔一段时间来,执行show master status; 判断position数值,是否不断增大,如果不断增大,说明有应用在不断的在修改数据。
具体slave_IO_state的取值含义:www.mysqlzh.com/doc/60.html
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: x.x.x.x
Master_User: xxx
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000399
Read_Master_Log_Pos: 48329175 //显示当前IO线程正在读取二进制日志的位置
Relay_Log_File: mysql-relay-bin.000929
Relay_Log_Pos: 283 //显示SQL线程已经读取和执行的中继日志位置
Relay_Master_Log_File: mysql-bin.000399
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: xxxx_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: 1
Exec_Master_Log_Pos: 48329175 //SQL线程执行relay log相对于主库的二进制日志的偏移量,等于主库的position的话,就代表主从数据一致
Relay_Log_Space: 503
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: 6
主从不一致,如何搭建从库?
无论如何,先备份主库。取到一个快照版本
RESET MASTER;
- 发出该语句:
mysql> FLUSH TABLES WITH READ LOCK;
- 仍然加锁时,执行该命令(或它的变体):
shell> tar zcf /tmp/backup.tar.gz /var/lib/mysql
- 发出该语句并且确保记录了以后用到的输出:
mysql>SHOW MASTER STATUS;
- 释放锁:
mysql> UNLOCK TABLES;
或者通过sqldump来备份
mysqldump –uroot –pnoodzhan@1234 --all-databases > sqlfile.sql
或
mysqldump -uroot -pnoodzhan@1234 --databases hvac logaudit mingjingtai nacos nanshan operation service_center shuiliandong unified_index_platform version_manage wechat workflow wuxingshan > sqlfile.sql
从库
mysql -uroot -pnoodzhan@1234 < sqlfile.sql
change master to master_host='172.24.20.12',master_port=3306,master_user='sync',master_password='ypkj@1234',master_log_file ='mysql-bin.000002',master_log_pos=684723;
参考
理解
MySQL的复制,不能扩展解决扩展写的能力,只能扩展读的能力。