MySql主从
mysql主从
1. 主从的简介
主从就是主从复制,简单来说,是使用两个或两个以上相同的数据库,将一个数据库当做主数据库,而另一个数据库当做从数据库。在主数据库中进行相应操作时,从数据库记录下所有主数据库的操作,使其二者一模一样。
1.1 主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2 主从形式
2. 主从复制原理
主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3. 主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL
服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
3.1 mysql安装
分别在主从两台服务器上安装mysql-5.7
版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章。
3.2 mysql主从配置
情景一:公司有两台数据库都没有数据,我们来配置主从复制
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库master | 192.168.153.132 | mariadb/redhat8 | 无数据 |
从数据库slave | 192.168.153.133 | mariadb/redhat8 | 无数据 |
准备工作(安装mysql关闭防火墙)
//master主机
[root@master ~]# yum -y install mariadb*
[root@master ~]# systemctl start mariadb
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]# systemctl stop firewalld
[root@master ~]# systemctl disable firewalld
[root@master ~]# setenforce 0
//slave主机
[root@slave ~]# yum -y install mariadb*
[root@slave ~]# systemctl start mariadb
[root@slave ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@slave ~]# systemctl stop firewalld
[root@slave ~]# systemctl disable firewalld
[root@slave ~]# setenforce 0
在主数据库里创建一个同步账号授权给从数据库使用
//master主机
[root@master ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant replication slave on *.* to 'lc'@'192.168.153.133' identified by '123456';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> quit
Bye
//slave主机
[root@slave ~]# mysql -ulc -p123456 -h192.168.153.132 //测试连接
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
配置主数据库
[root@master ~]# vim /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
//添加下面三行配置,=号左边格式要一样,右边格式也要一样
[mysqld]
log-bin = mysql_bin //启用binlog日志
server-id = 10 //数据库服务器唯一标识符(从库的server-id值必须大于主库的值)
[root@master ~]# systemctl restart mariadb //重启mariadb
//查看主库状态
[root@master ~]# mysql
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
MariaDB [(none)]>
配置从数据库
[root@slave ~]# vim /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
[mysqld]
relay-log = myrelay //启用中继日志relay-log
server-id = 20 //数据库服务器唯一标识符(从库的server-id值必须大于主库的值)
[root@slave ~]# systemctl restart mariadb //重启mariadb
//配置并启动主从复制
[root@slave ~]# mysql
MariaDB [(none)]> change master to master_host='192.168.153.132',master_user='lc',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=328;
Query OK, 0 rows affected (0.010 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> show slave status\G; //查看从数据库的状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.153.132
Master_User: lc
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 328
Relay_Log_File: myrelay.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //这里一定要是yes
Slave_SQL_Running: Yes //这里一定要是yes
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
测试验证
在主服务器的school库的student表中插入数据:
[root@master ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.17-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> use school;
Database changed
MariaDB [school]> create table student(id int not null primary key auto_increment,name varchar(20),age tinyint);
Query OK, 0 rows affected (0.017 sec)
MariaDB [school]> insert student(name,age)values('tom',15),('eric',21),('jerry',19);Query OK, 3 rows affected (0.002 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [school]> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 15 |
| 2 | eric | 21 |
| 3 | jerry | 19 |
+----+-------+------+
3 rows in set (0.000 sec)
MariaDB [school]> quit
Bye
[root@master ~]#
在从数据库中查看数据是否同步:
[root@slave ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
+--------------------+
4 rows in set (0.000 sec)
MariaDB [(none)]> use school;
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
MariaDB [school]> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student |
+------------------+
1 row in set (0.000 sec)
MariaDB [school]> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 15 |
| 2 | eric | 21 |
| 3 | jerry | 19 |
+----+-------+------+
3 rows in set (0.000 sec)
MariaDB [school]>
验证完毕数据同步
情景一:公司有两台数据库,主数据库中有数据,我们来配置主从复制
环境说明
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库master | 192.168.153.132 | mariadb/redhat8 | 有数据 |
从数据库slave | 192.168.153.133 | mariadb/redhat8 | 无数据 |
准备工作(安装mysql关闭防火墙)
//master主机
[root@master ~]# yum -y install mariadb*
[root@master ~]# systemctl start mariadb
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]# systemctl stop firewalld
[root@master ~]# systemctl disable firewalld
[root@master ~]# setenforce 0
//slave主机
[root@slave ~]# yum -y install mariadb*
[root@slave ~]# systemctl start mariadb
[root@slave ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@slave ~]# systemctl stop firewalld
[root@slave ~]# systemctl disable firewalld
[root@slave ~]# setenforce 0
模拟主数据库master有数据
[root@master ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database info;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> use info
Database changed
MariaDB [info]> create table basic(id int not null primary key auto_increment,name varchar(50),job varchar(50));
Query OK, 0 rows affected (0.013 sec)
MariaDB [info]> insert basic (name,job) values ('tom','engineer'),('jerry','office');
Query OK, 2 rows affected (0.001 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [info]> create table salary(name varchar(50),salary float);
Query OK, 0 rows affected (0.005 sec)
MariaDB [info]> insert salary values('tom',8000),('jerry',8500),('zhangshan',9000),('lisi',7500);
Query OK, 4 rows affected (0.001 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [info]> show databases;
+--------------------+
| Database |
+--------------------+
| info |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [info]> use info;
Database changed
MariaDB [info]> show tables;
+----------------+
| Tables_in_info |
+----------------+
| basic |
| salary |
+----------------+
2 rows in set (0.000 sec)
MariaDB [info]> select * from basic;
+----+-------+----------+
| id | name | job |
+----+-------+----------+
| 1 | tom | engineer |
| 2 | jerry | office |
+----+-------+----------+
2 rows in set (0.000 sec)
MariaDB [info]> select * from salary;
+-----------+--------+
| name | salary |
+-----------+--------+
| tom | 8000 |
| jerry | 8500 |
| zhangshan | 9000 |
| lisi | 7500 |
+-----------+--------+
4 rows in set (0.000 sec)
MariaDB [info]>
确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//查看主数据口有哪些库
[root@master ~]# mysql -uroot -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| info |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
//查看从数据口有哪些库
[root@slave ~]# mysql -uroot -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.001 sec)
//此锁表的终端必须在备份完成以后才能退出
//quit退出后,自动取消读锁
//此时已经无法写入数据了
MariaDB [(none)]> use info;
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
MariaDB [info]> select * from basic
-> ;
+----+-------+----------+
| id | name | job |
+----+-------+----------+
| 1 | tom | engineer |
| 2 | jerry | office |
+----+-------+----------+
2 rows in set (0.000 sec)
//插入数据失败
MariaDB [info]> insert basic(name,job)values('zhangshan','cleaner');
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
//再开启一个master主机终端
[root@master ~]# mysqldump -uroot --all-databases > all.sql //做一个全量备份
[root@master ~]# ls
all.sql anaconda-ks.cfg
[root@master ~]# scp all.sql 192.168.153.133:/root/ //把全备文件拷贝到slave主机的root目录下
The authenticity of host '192.168.153.133 (192.168.153.133)' can't be established.
ECDSA key fingerprint is SHA256:yFpgARX7WkH6WN29fHahmzlDbrUxFzaNBw5Pg8neBd4.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.153.133' (ECDSA) to the list of known hosts.
root@192.168.153.133's password:
all.sql 100% 469KB 36.1MB/s 00:00
//备份完之后,quit退出,读锁自动解除
MariaDB [info]> quit
Bye
//slave主机
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@slave ~]# ls //可以看到all。sql文件已经拷贝进来了
all.sql anaconda-ks.cfg
[root@slave ~]# mysql -e 'show databases;' //此时还没有恢复全备
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[root@slave ~]# mysql < all.sql //恢复全备
[root@slave ~]# mysql -e 'show databases;' //可以看到和master主数据库内容一样了
+--------------------+
| Database |
+--------------------+
| info |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[root@slave ~]#
在主数据库里创建一个同步账号授权给从数据库使用
//master主机
[root@master ~]# mysql
MariaDB [(none)]> grant replication slave on *.* to 'lc'@'192.168.153.133' identified by '123456';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)
//slave主机
[root@slave ~]# mysql -ulc -p123456 -h192.168.153.132 //测试连接
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
配置主数据库
[root@master ~]# vim /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
//添加下面三行配置,=号左边格式要一样,右边格式也要一样
[mysqld]
log-bin = mysql_bin //启用binlog日志
server-id = 10 //数据库服务器唯一标识符(从库的server-id值必须大于主库的值)
[root@master ~]# systemctl restart mariadb //重启mariadb
//查看主库状态
[root@master ~]# mysql
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
MariaDB [(none)]>
配置从数据库
[root@slave ~]# vim /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
[mysqld]
relay-log = myrelay //启用中继日志relay-log
server-id = 20 //数据库服务器唯一标识符(从库的server-id值必须大于主库的值)
[root@slave ~]# systemctl restart mariadb //重启mariadb
//配置并启动主从复制
[root@slave ~]# mysql
MariaDB [(none)]> change master to
master_host='192.168.153.132',master_user='lc',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=328;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> show slave status\G //查看从数据库的状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.153.132
Master_User: lc
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 328
Relay_Log_File: myrelay.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //这里一定要是yes
Slave_SQL_Running: Yes //这里一定要是yes
测试验证
在从服务器上查看一下info库里basic的数据
[root@slave ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| info |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> use info;
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
MariaDB [info]> show tables;
+----------------+
| Tables_in_info |
+----------------+
| basic |
| salary |
+----------------+
2 rows in set (0.000 sec)
MariaDB [info]> select * from basic;
+----+-------+----------+
| id | name | job |
+----+-------+----------+
| 1 | tom | engineer |
| 2 | jerry | office |
+----+-------+----------+
2 rows in set (0.000 sec)
MariaDB [info]>
在主数据库中的info库中的basic表里插入一条数据
[root@master ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.17-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| info |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> use info;
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
MariaDB [info]> show tables;
+----------------+
| Tables_in_info |
+----------------+
| basic |
| salary |
+----------------+
2 rows in set (0.000 sec)
MariaDB [info]> insert basic(name,job)values('lc','student') //插入一个数据
-> ;
Query OK, 1 row affected (0.002 sec)
MariaDB [info]>
在从数据库上查看一下basic表的信息是否同步
[root@slave ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| info |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
MariaDB [(none)]> use info;
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
MariaDB [info]> show tables;
+----------------+
| Tables_in_info |
+----------------+
| basic |
| salary |
+----------------+
2 rows in set (0.000 sec)
MariaDB [info]> select * from basic;
+----+-------+----------+
| id | name | job |
+----+-------+----------+
| 1 | tom | engineer |
| 2 | jerry | office |
| 3 | lc | student |
+----+-------+----------+
3 rows in set (0.000 sec)
MariaDB [info]>
验证完毕数据同步