mysql主从
1. 主从简介
1.1主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2主从形式
- 一主一从
- 主主复制—设置互斥
- 一主多从—扩展系统读取性能,因为读是从库读取的
- 多主一从—5.7开始支持
- 联级复制
如下图所示:
2. 主从复制原理

主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志)文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3. 主从复制配置
主从复制配置步骤:
- 确保从数据库和主数据库的数据一样
- 在主数据库里创建一个同步账号授权给数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
| 数据库角色 | IP | 应用与系统版本 | 有无数据 |
|---|---|---|---|
| 主数据库 | 192.168.176.111 | centos7/redhat7 mysql-5.7 |
有数据 |
| 从数据库 | 192.168.176.112 | centos7/redhat7 mysql-5.7 |
无数据 |
3.1mysql安装
分别在主从两台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考MySQL基础和MySQL进阶两篇文章
3.2mysql主从配置
3.2.1确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
[root@lwq-mysql ~]# mysql -uroot -plwq -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| liuwanqian |
| mysql |
| performance_schema |
| sys |
+--------------------+
//再查看从库有哪些库
[root@lwq-client ~]# mysql -uroot -plwq -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//全备主库
//全被主库时需开另一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.68 sec)
//此锁表的终端必须在备份完成后才能退出
//备份主库并将备份文件传送到从库
[root@lwq-mysql ~]# mysqldump -uroot -plwq --all-databases > all-20190819.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-mysql ~]# ls
all-20190819.sql
[root@lwq-mysql ~]# scp all-20190819.sql root@192.168.176.112:/root/
The authenticity of host '192.168.176.112 (192.168.176.112)' can't be established.
ECDSA key fingerprint is SHA256:acbRY/PG8nam4qE6Fue8SlFyxibg2xiUSLulWkR4nKM.
ECDSA key fingerprint is MD5:7c:b8:ac:81:bc:3f:d4:76:06:a9:09:d4:0f:76:2a:6c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.176.112' (ECDSA) to the list of known hosts.
root@192.168.176.112's password:
all-20190819.sql 100% 783KB 23.7MB/s 00:00
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
//在从库上恢复主库并查看从库有哪些文件,确保与主库一致
[root@lwq-client ~]# ls
all-20190819.sql
[root@lwq-client ~]# mysql -uroot -plwq -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@lwq-client ~]# mysql -uroot -plwq < all-20190819.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lwq-client ~]# mysql -uroot -plwq -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| liuwanqian |
| mysql |
| performance_schema |
| sys |
+--------------------+
3.2.2在主数据库里创建一个同步账号授权给从数据库使用
mysql> create user 'lwq'@'192.168.176.112' identified by 'lwq123';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'lwq'@'192.168.176.112';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.2.3配置主数据库
[root@lwq-mysql ~]# vim /etc/my.cnf
[root@lwq-mysql ~]# cat /etc/my.cnf //添加以下内容
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin = mysql-bin //启用binlog日志
server-id = 1 //数据库服务器唯一标识符,主库的server-id值必须比从库小,值越小优先级越高
//重启mysql服务并查看mysql端口号
[root@lwq-mysql ~]# systemctl restart mysqld
[root@lwq-mysql ~]# ss -antl
//查看主库的状态
mysql> show master status;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 2
Current database: *** NONE ***
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3.2.4配置从数据库
[root@lwq-client ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 2 //设置从库的唯一标识符,从库的server-id值必须大于主库的该值
relay-log = mysql-relay-bin //启用中继日志relay-log
//二进制安装的mysql用绝对路径启动该服务
[root@lwq-client ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL............... SUCCESS!
[root@lwq-client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 127.0.0.1:6011 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
LISTEN 0 128 ::1:6011 :::*
LISTEN 0 80 :::3306 :::*
//配置并启动主从复制
[root@lwq-client ~]# mysql -uroot -plwq
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 2
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> change master to
-> master_host='192.168.176.111',
-> master_user='lwq',
-> master_password='lwq123',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
//查看从服务器状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.176.111
Master_User: lwq
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes //此处必须为yes
Slave_SQL_Running: Yes //此处必须为yes
3.2.5测试验证
在主服务器的liuwanqian库的studnet表中插入数据:
mysql> use liuwanqian;
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
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
mysql> insert into student(name,age) values('xixi',10),('haha',11);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
| 12 | xixi | 10 |
| 13 | haha | 11 |
+----+-------------+------+
13 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
mysql> use liuwanqian;
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
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
| 12 | xixi | 10 |
| 13 | haha | 11 |
+----+-------------+------+
13 rows in set (0.00 sec)
//增加了两条新内容,验证成功!
来源:https://blog.csdn.net/weixin_44504261/article/details/99877616
