主从复制
架构
依赖于二进制日志,“实时”备份的一个多节点架构。
主从复制的前提(搭建主从复制),至少2个实例
1.不同的server_id(区别主从)
2.主库要开启二进制日志功能,从库不开(有些特殊情况下也可以开)
3.主库需要开启二进制日志
4.主库需要授权一个专用复制用户
5.主库数据备份
6.开启专用复制线程
准备架构
端口 | 作用 |
---|---|
3307 | 主 |
3308 | 从 |
3309 | 从 |
5.5 MySQL 多实例
5.5.1 创建目录
mkdir -p /data/330{7,8,9}/data
5.5.2 准备配置文件
cat > /data/3307/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
log_error=/data/3307/mysql.log
port=3307
server_id=7
log_bin=/data/3307/mysql-bin
EOF
cat > /data/3308/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
socket=/data/3308/mysql.sock
log_error=/data/3308/mysql.log
port=3308
server_id=8
log_bin=/data/3308/mysql-bin
EOF
cat > /data/3309/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3309/data
socket=/data/3309/mysql.sock
log_error=/data/3309/mysql.log
port=3309
server_id=9
log_bin=/data/3309/mysql-bin
EOF
5.5.3 初始化三套数据
mv /etc/my.cnf /etc/my.cnf.bak
mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/application/mysql
5.5.4 systemd管理多实例
cd /etc/systemd/system
cp mysqld.service mysqld3307.service
cp mysqld.service mysqld3308.service
cp mysqld.service mysqld3309.service
vim mysqld3307.service
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
vim mysqld3308.service
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
vim mysqld3309.service
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
5.5.5 授权
chown -R mysql.mysql /data/*
5.5.6 启动
systemctl start mysqld3307.service
systemctl start mysqld3308.service
systemctl start mysqld3309.service
5.5.7 验证多实例
netstat -lnp|grep 330
mysql -S /data/3307/mysql.sock -e "select @@server_id"
mysql -S /data/3308/mysql.sock -e "select @@server_id"
mysql -S /data/3309/mysql.sock -e "select @@server_id"
主从复制搭建
# 检查3307(主库)的二进制日志情况
[root@localhost system]# mysql -S /data/3307/mysql.sock -e "show variables like '%log_bin%'";
+---------------------------------+----------------------------+
| Variable_name | Value |
+---------------------------------+----------------------------+
| log_bin | ON |
# 主库授权一个专门用于连接的用户
[root@localhost system]# mysql -S /data/3307/mysql.sock
mysql> grant replication slave on *.* to repl@'192.168.31.%' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 主库进行数据备份
[root@localhost ~]# mysqldump -S /data/3307/mysql.sock -A --master-data=2 -R -E --triggers --single-transaction >/tmp/full.sql
# 恢复数据到从库3308
[root@localhost system]# mysql -S /data/3308/mysql.sock
mysql> set sql_log_bin=0;
mysql> source /tmp/full.sql;
# 查看备份使用的日志文件和起点
[root@localhost ~]# vim /tmp/full.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=448;
# 告诉从库要从哪里复制信息
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.31.210',
MASTER_USER='repl',
MASTER_PASSWORD='123',
MASTER_PORT=3307,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=448,
MASTER_CONNECT_RETRY=10;
# 启动复制线程
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
# 在从库上查看状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主从复制的工作流程
名词认识
# 文件
主库: binlog(二进制日志文件)
从库: relay-log(中继日志)、master.info(主库信息文件)、relay-log.info(中继日志应用信息)
# 线程
主库: binlog_dump-thread(二进制日志投递线程)
[root@localhost system]# mysql -S /data/3307/mysql.sock -e "show processlist";
从库: IO_Thread(从库IO线程:请求和接收主库发过来的日志)
SQL_Thread(从库的SQL线程:回放从主库拿过来的日志)
主从监控
# 主库
[root@localhost system]# mysql -S /data/3307/mysql.sock -e "show processlist";
+----+------+----------------------+------+-------------+------+---------------------------------------------------------------+------------------ +
| Id | User | Host | db | Command | Time | State | Info |
+----+------+----------------------+------+-------------+------+---------------------------------------------------------------+------------------ +
| 7 | repl | 192.168.31.210:43850 | NULL | Binlog Dump | 4278 | Master has sent all binlog to slave; waiting for more updates | NULL |
| 10 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+------+----------------------+------+-------------+------+---------------------------------------------------------------+------------------
# 从库
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.210
Master_User: repl
Master_Port: 3307
Connect_Retry: 10
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 448
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-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: 448
Relay_Log_Space: 531
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: 0e06a3df-b12b-11eb-a316-080027fb878b
Master_Info_File: /data/3308/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:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
主从延时原因分析
# 主库
1.日志写入不及时 :(sync_binlog=1)
2.主库并发业务较多:升级硬件、做业务拆分(分布式架构)、
3.从库太多
4.级联主从
# 从库方便
SQL线程只能有一个:5.7加入了MTS,真正实现了事务级别的并发SQL
延时从库
# 3~6小时
stop slave;
CHANGE MASTER TO MASTER_DELAY=300;
start slave
show slave status\G;