MySQL主从
一、什么是主从
主从(Master-Slave)是一种常见的分布式系统架构,其中一个节点(主节点)负责处理写操作,而其他节点(从节点)负责处理读操作。主节点接收到写操作后,会将数据同步到从节点,从节点则负责响应读请求,并从主节点同步最新的数据。
主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
主从形式
- 一主一从
- 主主复制
- 一主多从
- 多主一从
- 联级复制
二、主从复制原理
主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
三、主从复制配置
主从复制配置步骤:
1. 确保从数据库与主数据库里的数据一样
2. 在主数据库里创建一个同步账号授权给从数据库使用
3. 配置主数据库(修改配置文件)
4. 配置从数据库(修改配置文件)
需求:
搭建两台MySQL
服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 有无数据 |
---|---|---|
主数据库(master) | 192.168.200.39 | 有数据 |
从数据库(slave) | 192.168.200.26 | 无数据 |
1.mysql主从配置
1.1确保从数据库与主数据库里的数据一样
//先查看主库有哪些库
[root@master ~]# mysql -uroot -p123456 -e 'show databases';
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
+--------------------+
//再查看从库有哪些库
[root@slave ~]# mysql -uroot -p1234567 -e 'show databases';
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.001 sec)
//此锁表的终端必须在备份完成以后才能退出
//备份主库并将备份文件传送到从库
[root@master ~]# mysqldump -uroot -p123456 --all-databases > /opt/all-20230907.sql
[root@master ~]# ls /opt/
all-20230907.sql
[root@master ~]# scp /opt/all-20230907.sql root@192.168.200.26:/opt/
The authenticity of host '192.168.200.26 (192.168.200.26)' can't be established.
ECDSA key fingerprint is SHA256:uwDYf94qB7rPfLz60Ep1yhQIUBsPDHhBEaUJSdIvySI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.200.26' (ECDSA) to the list of known hosts.
root@192.168.200.26's password:
all-20230907.sql 100% 467KB 51.9MB/s 00:00
//解除主库的锁表状态,直接退出交互式界面即可
MariaDB [(none)]> quit
Bye
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@slave ~]# mysql -uroot -p1234567 < /opt/all-20230907.sql
[root@slave ~]# mysql -uroot -p1234567 -e 'show databases';
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
+--------------------+
1.2在主数据库里创建一个同步账号授权给从数据库使用
MariaDB [(none)]> create user 'abc'@'192.168.200.26' identified by 'abc123';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> grant replication slave on *.* to 'abc'@'192.168.200.26';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
1.3 配置主数据库
[root@master ~]# vi /etc/my.cnf.d/mariadb-server.cnf
//在[mysqld]这段的后面加上如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin //启用binlog日志
server-id=2 //数据库服务器唯一标识符,主库的server-id值必须比从库的大
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重启mariadb服务
[root@master ~]# systemctl restart mariadb
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
//查看主库的状态
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
1.4配置从数据库
[root@slave ~]# vi /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=1 //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql-relay-bin //启用中继日志relay-log
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重启从库的mariadb服务
[root@slave ~]# vi /etc/my.cnf.d/mariadb-server.cnf
[root@slave ~]# systemctl restart mariadb
[root@slave ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
//配置并启动主从复制
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.200.39',
-> MASTER_USER='abc',
-> MASTER_PASSWORD='abc123',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=678;
Query OK, 0 rows affected (0.008 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.200.39
Master_User: abc
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 342
Relay_Log_File: mysql-relay-bin.000005
Relay_Log_Pos: 641
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes //此处必须为Yes
Slave_SQL_Running: Yes //此处必须为Yes
1.5测试验证
在主服务器的shcool库的student表中插入数据:
MariaDB [school]> insert into student (name,age) values ('zhangshan',26),('lisi',25);
Query OK, 2 rows affected (0.001 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [school]> select * from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | zhangshan | 26 |
| 4 | lisi | 25 |
+----+-----------+------+
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]> select * from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | zhangshan | 26 |
| 4 | lisi | 25 |
+----+-----------+------+
4 rows in set (0.000 sec)