一、异步主从模式介绍
MySQL主从复制有异步模式、半同步模式、GTID模式以及多源复制模式,MySQL默认模式是异步模式。所谓异步模式,只MySQL 主服务器上I/O thread 线程将二进制日志写入binlog文件之后就返回客户端结果,不会考虑二进制日志是否完整传输到从服务器以及是否完整存放到从服务器上的relay日志中,这种模式一旦主服务(器)宕机,数据就会发生丢失。
使用主从同步的好处:
- 通过增加从服务器来提高数据库的性能,在主服务器上执行写入和更新,在从服务器上向外提供读功能,可以动态地调整从服务器的数量,从而调整整个数据库的性能。
- 提高数据安全,因为数据已复制到从服务器,从服务器可以终止复制进程,所以,可以在从服务器上备份而不破坏主服务器相应数据。
- 在主服务器上生成实时数据,而在从服务器上分析这些数据,从而提高主服务器的性能。
二、异步主从复制配置步骤
0、主从复制原理图
1、server-id规划
序号 | 角色 | IP地址 | server-id |
---|---|---|---|
1 | master | 192.168.0.124 | 124 |
2 | slave | 192.168.0.125 | 125 |
2、安装数据库
可以通过rpm安装、解压版安装等方式安装mysql数据库,rpm安装方式见链接https://blog.csdn.net/carefree2005/article/details/113313468
3、修改主从节点my.cnf配置文件
s124节点的my.cnf配置添加如下配置:
#主从配置
server-id=124
log_bin=mysql-bin
log_slave_updates=1
s125节点的my.cnf配置添加如下配置:
#主从配置
server-id=125
log_bin=mysql-bin
log_slave_updates=1
4、重启数据库
[root@test1 opt]# systemctl restart mysqld
[root@test2 ~]# systemctl restart mysqld
5、创建主从复制账号
mysql> create user bak@‘192.168.0.%’ identified by ‘Bak!1234’;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO ‘bak’@‘192.168.0.%’;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
6、备份主节点数据库
[root@test1 ~]# mysqldump --single-transaction -uroot -p --master-data=2 -A > all.sql
Enter password:
[root@test1 ~]# ll -h
total 784K
-rw-r–r–. 1 root root 775K Feb 15 21:21 all.sql
7、检查并记录MASTER的binglog日志和pos值的位置
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 787
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.01 sec)
8、将主节点备份文件还原至从节点
[root@test2 ~]# scp 192.168.0.124:/root/all.sql ./
root@192.168.0.124’s password:
all.sql 100% 774KB 61.9MB/s 00:00
[root@test2 ~]# mysql -uroot -p < all.sql
Enter password:
9、在从库上配置主从
mysql> change master to
-> MASTER_HOST=‘192.168.0.124’,
-> MASTER_USER=‘bak’,
-> MASTER_PASSWORD=‘Bak!1234’,
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE=‘mysql-bin.000001’, MASTER_LOG_POS=787;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
10、启动主从复制
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
11、查看主从复制状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.124
Master_User: bak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1582
Relay_Log_File: test2-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: 1582
Relay_Log_Space: 527
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: 124
Master_UUID: 5738531f-6f8d-11eb-8d4e-000c290b9fa0
Master_Info_File: /var/lib/mysql/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 row in set (0.00 sec)
三、常用主从管理命令
1、在从库上查看主从复制状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.124
Master_User: bak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1582
Relay_Log_File: test2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
…
2、查看主库的binlog文件和position位置
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 1582
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.01 sec)
3、在从库上配置主从过程
mysql> change master to
4、开启主从同步
mysql> start slave;
5、关闭主从同步
mysql> stop slave;
6、清空从库的所有配置信息
清空前需要停止主从服务
mysql> reset slave all;
Query OK, 0 rows affected (0.00 sec)