十一、Mysql - 主从复制原理 - 异步复制
目录
2、先将Mysql的数据库全备导出,然后再slave上导入,达到两边服务器到的基础数据一样
3、确保master上开启了二进制功能,slave也开启二进制日志,也可以不开启,但是需要指定server_id
4、在master上创建一个可以有复制权限的授权用户,这样slave可以到master服务器里面来复制二进制日志了
有什么方法可以实现在线主从复制? ------- XtraBackup
知识点1:什么是主从复制?
其实就是复制的是主服务器的二进制日志,从服务器然后再重新根据日志操作一遍,从而达到和主服务器里的数据一样,但是时间上会有延迟。
如何解决延迟的问题?
集群:很多台服务器做一样的事情
讲一讲主从复制
有两台mysql服务器,一台master,一台slave
1、首先master上面要开启二进制日志,当有data changes的时候,数据就会存入二进制日志里面,
2、master里面有一个log dump线程,一旦二进制日志里面有变化,就会通知slave里面的I/O线程来读取数据
3、I/o线程读取到数据以后就会将数据写入Relay log 中继日志里面,然后由slave里面的SQL线程进行read和replay操作,达到数据一致性的目的。
############################################################################
知识点2:主从复制之异步复制实验
实验步骤
1、master和slave都要安装相同版本的Mysql
master:
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.34-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@(none) 11:36 mysql>
slave:
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@(none) 11:37 mysql>
############################################################################
2、先将Mysql的数据库全备导出,然后再slave上导入,达到两边服务器到的基础数据一样
[root@localhost ~]# mysqldump -uroot -p'Sanchuang123#' --all-databases >/backup/all_db.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# cd /backup/
[root@localhost backup]# ls
all_db.sql sanchuang.sql
[root@localhost backup]#
# 将全备份sql文件scp传到slave服务器上面
[root@localhost backup]# scp all_db.sql 192.168.44.160:~/
root@192.168.44.160's password:
all_db.sql 100% 867KB 60.6MB/s 00:00
# 在slave服务器上面导入全备份sql文件
[root@network ~]# mysql -uroot -p'Sanchuang123#' < all_db.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
# 可以看到,基础数据已经一致
root@(none) 11:28 mysql>show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 5
Current database: *** NONE ***
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sanchuang |
| student |
| sys |
| test |
| ucar_cloud |
| wangsh |
+--------------------+
9 rows in set (0.00 sec)
root@(none) 11:43 mysql>
############################################################################
3、确保master上开启了二进制功能,slave也开启二进制日志,也可以不开启,但是需要指定server_id
master:
# binary log
log_bin
server_id = 1
slave:
server_id = 2
############################################################################
4、在master上创建一个可以有复制权限的授权用户,这样slave可以到master服务器里面来复制二进制日志了
root@(none) 11:47 mysql>grant replication slave on *.* to 'liu'@'192.168.44.%' identified by 'Sanchuang1234#';
Query OK, 0 rows affected, 1 warning (0.01 sec)
root@(none) 11:49 mysql>
############################################################################
5、在slave上添加授权用户的信息
root@(none) 15:09 mysql> CHANGE MASTER TO MASTER_HOST='192.168.44.170' ,
-> MASTER_USER='liu',
-> MASTER_PASSWORD='Sanchuang1234#',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='localhost-bin.000001',
-> MASTER_LOG_POS=6135;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
添加成功以后,查看slave状态,发现slave两个线程:I/O线程和SQL线程都是关闭状态,所以接下来要将它们开启
root@(none) 15:11 mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.44.170
Master_User: liu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: localhost-bin.000001
Read_Master_Log_Pos: 6135
Relay_Log_File: network-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: localhost-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
############################################################################
6、启动slave角色
root@(none) 15:11 mysql>start slave;
Query OK, 0 rows affected (0.00 sec)
root@(none) 15:13 mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.44.170
Master_User: liu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: localhost-bin.000001
Read_Master_Log_Pos: 6135
Relay_Log_File: network-relay-bin.000002
Relay_Log_Pos: 324
Relay_Master_Log_File: localhost-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
启动了slave角色以后,发现两个线程都已经成功起来。
############################################################################
7、验证主从复制的数据是否可以同步
在master机器上面新建一个数据库liuhongjie,新建两个表t1,t2
root@(none) 15:16 mysql>create database liuhongjie;
Query OK, 1 row affected (0.00 sec)
root@(none) 15:16 mysql>use liuhongjie
Database changed
root@liuhongjie 15:16 mysql>create table t1(id int,name varchar(10));
Query OK, 0 rows affected (0.01 sec)
root@liuhongjie 15:17 mysql>create table t2(id int,name varchar(10));
Query OK, 0 rows affected (0.00 sec)
root@liuhongjie 15:17 mysql>show tables;
+----------------------+
| Tables_in_liuhongjie |
+----------------------+
| t1 |
| t2 |
+----------------------+
2 rows in set (0.00 sec)
root@liuhongjie 15:17 mysql>
slave服务器:发现数据成功同步
root@(none) 15:14 mysql>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| liuhongjie |
| mysql |
| performance_schema |
| sanchuang |
| student |
| sys |
| test |
| ucar_cloud |
| wangsh |
+--------------------+
10 rows in set (0.00 sec)
root@(none) 15:17 mysql>use liuhongjie;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
root@liuhongjie 15:17 mysql>show tables;
+----------------------+
| Tables_in_liuhongjie |
+----------------------+
| t1 |
| t2 |
+----------------------+
2 rows in set (0.00 sec)
root@liuhongjie 15:18 mysql>
root@liuhongjie 15:18 mysql>show processlist;
+----+-------------+-----------+------------+---------+------+--------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------------+---------+------+--------------------------------------------------------+------------------+
| 2 | root | localhost | liuhongjie | Query | 0 | starting | show processlist |
| 3 | system user | | NULL | Connect | 655 | Waiting for master to send event | NULL |
| 4 | system user | | NULL | Connect | 428 | Slave has read all relay log; waiting for more updates | NULL |
+----+-------------+-----------+------------+---------+------+--------------------------------------------------------+------------------+
3 rows in set (0.00 sec)
一些其他验证:
查看刚才创建的用户是否从slave机器上面连接过来
root@liuhongjie 15:17 mysql>show processlist;
+----+------+----------------------+------------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+----------------------+------------+-------------+------+---------------------------------------------------------------+------------------+
| 16 | root | localhost | liuhongjie | Query | 0 | starting | show processlist |
| 17 | liu | 192.168.44.160:45246 | NULL | Binlog Dump | 669 | Master has sent all binlog to slave; waiting for more updates | NULL |
+----+------+----------------------+------------+-------------+------+---------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
查看slave机器上面的一些相关文件
############################################################################
知识点3:生产环境下的主从复制该怎么做?
我们刚才在master和slave机器上面做的主从复制都是在非生产状态的情况下完成的,
那么如果是要在生产环境下(即数据库在提供服务,数据在不断刷新)进行主从复制怎么办呢?
其实生产环境下的主从复制和非生产环境下的主从复制步骤几乎一致
但是要解决的难题是:
在slave机器上面添加用户信息时,因为正在提供服务,二进制日志在变化,position位置点也是在不断变化的。
我们无法精准的找到位置点,那么主从复制就无法保证数据的一致性。
最好地解决办法就是停止服务,进行主从备份。
有什么方法可以实现在线主从复制? ------- XtraBackup
mysqldump对于导出10G以下的数据库或几个表,还是适用的,而且更快捷。一旦数据量达到100-500G,无论是对原库的压力还是导出的性能,mysqldump就力不从心了。Percona-Xtrabackup备份工具,是实现MySQL在线热备工作的不二选择,可进行全量、增量、单表备份和还原。(但当数据量更大时,可能需要考虑分库分表,或使用 LVM 快照来加快备份速度了)
############################################################################
知识点4:什么时候会发生主从切换,主从切换怎么完成?
1.什么时候需要主从切换,主从切换如何实现?
主服务器挂了,需要提升原来的从为主 --》主从切换
完全手工去操作:
步骤:
1.stop slave
2.reset master
3.开启二进制日志
4.建立授权复制的用户
5.再启动一台机器做从,配置master信息去拉取二进制日志
如何将网站的新的的流量切到新的master上?
1.直接修改web里的代码里的ip,换成新的master的ip
2.修改域名对应的ip为新的master的ip
3.如果使用中间件,需要在中间件里调整
是否可以自动实现主从切换?
答案: 可以
使用脚本实现
1.监控master
在另外一台机器扫描端口:nc 3306
直接访问: mysql -h ip -uroot -p'**' -e 'show databases;'
每秒钟监控一次
2.马上执行手工操作的步骤,脚本自动执行
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通