最近的文章都是关于mysql,因为它是数据库,企业的核心价值。这篇呢,也是面试中经常被问到的问题,主从。
上篇文章是数据备份,其主要用到的还是binlog,主从也是需要它。
环境搭建 前提是数据库的配置文件已经开启binlog日志了
没有开启的戳这里 MySQL 日志管理 下面也有讲解
$ tree -L 1 /data/ 3307 # 主 3308 # 从 3309 # 从
数据库还不会搭建的小伙伴,戳这里 MySQL 同版本多实例
systemctl start mysqld3307 systemctl start mysqld3308 systemctl start mysqld3309 # 查看server_id 和server_uuid $ mysql -S /tmp/mysql3307.sock -e "select @@server_id ;" +-------------+ | @@server_id | +-------------+ | 7 | +-------------+ $ mysql -S /tmp/mysql3308.sock -e "select @@server_id ;" +-------------+ | @@server_id | +-------------+ | 8 | +-------------+ $ mysql -S /tmp/mysql3309.sock -e "select @@server_id ;" +-------------+ | @@server_id | +-------------+ | 9 | +-------------+ $ mysql -S /tmp/mysql3309.sock -e "select @@server_uuid ;" +--------------------------------------+ | @@server_uuid | +--------------------------------------+ | 1c920eb6-901a-11ea-a2a5-000c29248f69 | +--------------------------------------+ $ mysql -S /tmp/mysql3308.sock -e "select @@server_uuid ;" +--------------------------------------+ | @@server_uuid | +--------------------------------------+ | 195bb724-901a-11ea-a083-000c29248f69 | +--------------------------------------+ $ mysql -S /tmp/mysql3307.sock -e "select @@server_uuid ;" +--------------------------------------+ | @@server_uuid | +--------------------------------------+ | 15ac32d2-901a-11ea-9ee5-000c29248f69 | +--------------------------------------+
主库操作
更改my.cnf配置文件
$ vim /data/3307/my.cnf server_id=7 log_bin=/data/3307/mysql-bin
重启服务
systemctl restart mysqld3307
授权
$ mysql -S /tmp/mysql3307.sock -e "select @@log_bin ;" $ mysql -S /tmp/mysql3307.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';" $ mysql -S /tmp/mysql3307.sock -e "select user,host ,plugin from mysql.user;"
备份主库数据,恢复到从库
$ mysqldump -S /tmp/mysql3307.sock -A --master-data=2 --single-transaction -R -E --triggers --max_allowed_packet=64M >/data/full.sql $ mysql -S /tmp/mysql3308.sock</data/full.sql $ mysql -S /tmp/mysql3309.sock</data/full.sql
从库操作
告知从库复制的信息
help change master to # 使用帮助命令 CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_USER='repl', MASTER_PASSWORD='123', MASTER_PORT=3307, MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=444, MASTER_CONNECT_RETRY=10; # log_file 和 pos号 从备份的sql文件查找 vim /data/full.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=444;
启动从库线程
start slave;
查看是否成功
$ mysql -S /tmp/mysql3309.sock -e "show slave status\G"|grep "Running:" Slave_IO_Running: Yes Slave_SQL_Running: Yes $ mysql -S /tmp/mysql3308.sock -e "show slave status\G"|grep "Running:" Slave_IO_Running: Yes Slave_SQL_Running: Yes
主从原理
这才是本篇核心知识
master服务器将数据的改变都记录到二进制binlog日志中,只要master上的数据发生改变,则将其改变写入二进制日志; salve服务器会在一定时间间隔内对master二进制日志进行探测其是否发生改变,如果发生改变,则开始一个I/O Thread请求master二进制事件 同时主节点为每个I/O线程启动一个dump线程,用于向其发送二进制事件,并保存至从节点本地的中继日志中 从节点将启动SQL线程从中继日志中读取二进制日志,在本地重放,使得其数据和主节点的保持一致 最后I/O Thread和SQL Thread将进入睡眠状态,等待下一次被唤醒. 需要理解: 1)从库会生成两个线程,一个I/O线程,一个SQL线程; 2)I/O线程会去请求主库的binlog,并将得到的binlog写到本地的relay-log(中继日志)文件中;* 3)主库会生成一个log dump线程,用来给从库I/O线程传binlog; 4)SQL线程,会读取relay log文件中的日志,并解析成sql语句逐一执行;
流程图
如何监控主从复制
主库
$ mysql -S /tmp/mysql3307.sock -e "show processlist" |grep "Dump" 7 repl db01:34640 NULL Binlog Dump 5355 Master has sent all binlog to slave; waiting for more updates NULL 9 repl db01:34642 NULL Binlog Dump 687 Master has sent all binlog to slave; waiting for more updates NULL $ mysql -S /tmp/mysql3307.sock -e "show slave hosts;" +-----------+------+------+-----------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +-----------+------+------+-----------+--------------------------------------+ | 9 | | 3309 | 7 | 1c920eb6-901a-11ea-a2a5-000c29248f69 | | 8 | | 3308 | 7 | 195bb724-901a-11ea-a083-000c29248f69 | +-----------+------+------+-----------+--------------------------------------+
从库
$ mysql -S /tmp/mysql3308.sock -e "show slave status \G" # 主库连接信息、binlog位置信息(master.info) Master_Host: 10.0.0.51 Master_User: repl Master_Port: 3307 Connect_Retry: 10 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 444 # 从库中relay-log的回放信息 Relay_Log_File: db01-relay-bin.000006 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Exec_Master_Log_Pos: 444 # 线程监控信息:主要用来排查主从故障 Slave_IO_Running: Yes Slave_SQL_Running: Yes Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: # 过滤复制相关信息 Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: # 落后于主库的秒数 Seconds_Behind_Master: 0 # 延时从库状态信息 SQL_Delay: 0 SQL_Remaining_Delay: NULL # GTID复制信息 Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0