本环境是基于 Centos 7.8 系统构建MySQL-5.7.14
具体构建,请参考 MySQL-5.7.14 环境构建
MySQL内建的复制功能是构建大型,高性能应用程序的基础。
通过将MySQL的某一台主机(master)的数据复制到其他主机(slaves)上,并重新执行一遍来执行复制过程中一台服务器充当主服务器,而其他一个或多个其他服务器充当从服务器。
MySQL支持的复制类型
基于语句(statement)的复制
- 在主服务器上执行SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。
基于行(row)的复制
- 把改变的内容复制过去,而不是把命令在从服务器上执行一遍。从MySQL 5.0开始支持。
混合型(mixed)的复制
- 默认采用基于语句的复制,一旦发现基于语句的无法精确复制时,就会采用基于行的复制
MySQL 主从复制的优势
- 灾备
- 数据分布
- 负载平衡
- 读写分离
- 提高并发能力
主从复制原理
主要基于MySQL二进制日志
主要包括三个线程(2个I/O线程,1个SQL线程)
1、MySQL将数据变化记录到二进制日志中;
2、Slave将MySQL的二进制日志拷贝到Slave的中继日志中;
3、Slave将中继日志中的事件在做一次,将数据变化,反应到自身(Slave)的数据库
详细步骤:
1、从库通过手工执行change master to 语句连接主库,提供了连接的用户一切条件(user 、password、port、ip),并且让从库知道,二进制日志的起点位置(file名 position 号);startslave
2、从库的IO线程和主库的dump线程建立连接。
3、从库根据change master to 语句提供的file名和position号,IO线程向主库发起binlog的请求。
4、主库dump线程根据从库的请求,将本地binlog以events的方式发给从库IO线程。
5、从库IO线程接收binlog events,并存放到本地relay-log中,传送过来的信息,会记录到master.info中
6、从库SQL线程应用relay-log,并且把应用过的记录到relay-log.info中,默认情况下,已经应用过的relay 会自动被清理purge
两台主机
- mysql-yum:mysqld主服务
- mysql-source_code:多实例部署两个mysqld服务
主服务:
- IP:192.168.5.11/24
- port:3306
- server_id:1
- 开启 bin_log
从服务器:
- IP:192.168.5.14/24
- port:3306、3307
- server_id:1、2
- 未开启 bin_log
主库
开启bin_log
[root@mysql-yum ~]# vim /etc/my.cnf
log_bin=mysql-bin
server_id=1
创建主从同步用户
mysql> grant replication slave on *.* to 'wan'@'192.168.5.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.35 sec)
mysql> show grants for 'wan'@'192.168.5.%';
+-------------------------------------------------------+
| Grants for [email protected].% |
+-------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'wan'@'192.168.5.%' |
+-------------------------------------------------------+
1 row in set (0.00 sec)
备份数据
# 锁表
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
# 查看主库状态(日志文件名、位置点信息)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 8790 | | | |
+------------------+----------+--------------+------------------+-------------------+
# 备份数据
[root@mysql-yum ~]# mysqldump -uroot -p123456 -A -B | gzip > /backup/mysql/mysql_date_bak.sql.gz
[root@mysql-yum ~]# ll /backup/mysql/mysql_date_bak.sql.gz
-rw-r--r-- 1 root root 208933 Feb 5 12:59 /backup/mysql/mysql_date_bak.sql.gz
# 解锁
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
备份数据同步到从节点上
[root@mysql-yum ~]# scp /backup/mysql/mysql_date_bak.sql.gz 192.168.5.14:/opt/
从库
还原主库数据
[root@mysql-source_code ~]# zcat /opt/mysql_date_bak.sql.gz | mysql -uroot -p123456 -S /mysql/3306/mysql.sock
[root@mysql-source_code ~]# zcat /opt/mysql_date_bak.sql.gz | mysql -uroot -p123456 -S /mysql/3307/mysql.sock
# 查看数据
--- 3306
[root@mysql-source_code ~]# mysql -uroot -p123456 -S /mysql/3306/mysql.sock
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.14 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
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.
mysql> select * from db2.emp;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
15 rows in set (0.01 sec)
mysql>
--- 3307
[root@mysql-source_code ~]# mysql -uroot -p123456 -S /mysql/3307/mysql.sock
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.14 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
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.
mysql> select * from db2.emp;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
15 rows in set (0.01 sec)
mysql>
设置从库
[root@mysql-source_code ~]# vim /etc/my.cnf
# 开启server_id
[mysqld3306]
server_id=2
[mysqld3307]
server_id=3
[root@mysql-source_code ~]# mysqld_multi stop 3306,3307
[root@mysql-source_code ~]# netstat -lnutp | grep mysqld
[root@mysql-source_code ~]# mysqld_multi start 3306,3307
[root@mysql-source_code ~]# netstat -lnutp | grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 5320/mysqld
tcp6 0 0 :::3307 :::* LISTEN 5346/mysqld
# 设置主从同步
--- 3306
mysql> change master to
-> master_host='192.168.95.11',
-> master_port=3306,
-> master_user='wan',
-> master_password='123456',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=8790;
Query OK, 0 rows affected, 2 warnings (0.35 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
--- 3307
mysql> change master to
-> master_host='192.168.95.11',
-> master_port=3306,
-> master_user='wan',
-> master_password='123456',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=8790;
Query OK, 0 rows affected, 2 warnings (0.35 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看状态
测试:
插入数据前:
主:
mysql> select * from db2.emp;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
15 rows in set (0.00 sec)
从:
3306
mysql> select * from db2.emp;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
15 rows in set (0.43 sec)
3307
mysql> select * from db2.emp;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
15 rows in set (0.00 sec)
插入数据:
mysql> insert into db2.emp values
-> (1016,'丁真','销售员',1106,'2001-10-01',22500,3000,40),
-> (1017,'王冰冰','记者',1007,'2000-10-01',21500,4000,50);
Query OK, 2 rows affected (0.00 sec)
插入数据后:
主:
mysql> select * from db2.emp;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
| 1016 | 丁真 | 销售员 | 1106 | 2001-10-01 | 22500 | 3000 | 40 |
| 1017 | 王冰冰 | 记者 | 1007 | 2000-10-01 | 21500 | 4000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
17 rows in set (0.00 sec)
从:
3306
mysql> select * from db2.emp;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
| 1016 | 丁真 | 销售员 | 1106 | 2001-10-01 | 22500 | 3000 | 40 |
| 1017 | 王冰冰 | 记者 | 1007 | 2000-10-01 | 21500 | 4000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
17 rows in set (0.00 sec)
3307
mysql> select * from db2.emp;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
| 1016 | 丁真 | 销售员 | 1106 | 2001-10-01 | 22500 | 3000 | 40 |
| 1017 | 王冰冰 | 记者 | 1007 | 2000-10-01 | 21500 | 4000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
17 rows in set (0.00 sec)
主从同步配置成功!!!