一、主从简介

在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?

1、 主从作用

实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务

2、 主从形式

一主一从
主主复制
一主多从—扩展系统读取的性能,因为读是在从库读取的
多主一从—5.7开始支持
联级复制

3、 主从复制原理

主从复制步骤:
主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
从库生成两个线程,一个I/O线程,一个SQL线程
I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的

4、主从复制配置

主从复制配置步骤:
1.确保从数据库与主数据库里的数据一样
2.在主数据库里创建一个同步账号授权给从数据库使用
3.配置主数据库(修改配置文件)
4.配置从数据库(修改配置文件)

二、配置从数据库

1、环境

两台虚拟机版本为 rocky Linux mysql-5.7,主数据库有数据,从数据库无数据,使用 Xshell 进行连接

mysql搭建:http://t.csdnimg.cn/4dlmU

主数据库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shiqian |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> show tables;
+-------------------+
| Tables_in_shiqian |
+-------------------+
| sq |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from sq;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 18 |
| 2 | marry | 19 |
| 3 | rose | 20 |
| 4 | mike | 19 |
+----+-------+------+
4 rows in set (0.00 sec)

从数据库:

1
2
3
4
5
6
7
8
9
10
11
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

2、全备主库

全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致

1
2
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)

新开一个终端连接到主数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@mysql ~]# mysqldump -uroot -predhat --all-databases > /opt/all-202408061001.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@mysql ~]# ls /opt/
all-202408061001.sql data software

//将文件传输到从数据库端
[root@mysql ~]# scp /opt/all-202408061001.sql root@192.168.100.112:/opt/
The authenticity of host '192.168.100.112 (192.168.100.112)' can't be established.
ED25519 key fingerprint is SHA256:qoGezja/t9Hid/DD5+L7baEr1jkiPvJqhdXfVXaJjBo.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.100.112' (ED25519) to the list of known hosts.
root@192.168.100.112's password:
all-202408061001.sql 100% 856KB 48.0MB/s 00:00

在这些都做完后,解除主库的锁表状态,直接exit退出数据库即可

1
2
3
mysql> exit
Bye
[root@mysql ~]#

3、从库恢复主库数据

在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@client ~]# mysql -uroot -predhat < /opt/all-202408061001.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@client ~]# mysql -uroot -predhat
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shiqian |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use shiqian;
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> show tables;
+-------------------+
| Tables_in_shiqian |
+-------------------+
| sq |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from sq;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 18 |
| 2 | marry | 19 |
| 3 | rose | 20 |
| 4 | mike | 19 |
+----+-------+------+
4 rows in set (0.00 sec)

4、配置主库

创建同步用户

主库创建一个同步账号给从库使用

1
2
3
4
5
6
7
8
9
10
11
//创建同步用户
mysql> create user 'repl'@'192.168.100.112' identified by 'redhat';
Query OK, 0 rows affected (0.00 sec)

//给同步用户授权
mysql> grant replication slave on *.* to 'repl'@'192.168.100.112';
Query OK, 0 rows affected (0.00 sec)

//刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

修改主库配置文件

1
2
3
4
5
6
7
8
[root@mysql ~]# vim /etc/my.cnf
//在末尾写入以下内容
log-bin=mysql-bin
server-id=1
symbolic-links=0
log-error=/var/log/mysqld.log
//第一个是启用binlog日志
//第二个是数据库服务器唯一标识符,主库的server-id值必须比从库的小

重启MySQL服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# 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 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
[root@mysql ~]# mysql -uroot -predhat
mysql: [Warning] Using a password on the command line interface can be insecure.

//查看主库状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

5、配置从库

修改从库配置文件

1
2
3
4
5
6
[root@client ~]# vim /etc/my.cnf
//在末尾写入
server-id=2
relay-log=mysql-relay-bin
symbolic-links=0symbolic-links=0
log-error = /var/log/mysqld.log

重启mysql服务

1
2
3
4
5
6
[root@client ~]# systemctl restart mysqld
[root@client ~]# ss -anlt
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 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*

6、启动主从配置

从库配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
[root@client ~]# mysql -uroot -predhat
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> change master to master_host='192.168.100.111',
-> master_user='repl',
-> master_password='redhat',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.111
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 364
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 364
Relay_Log_Space: 527
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: dcf64bad-4e4d-11ef-a5b1-000c29d1dcca
Master_Info_File: /opt/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

一定要确保 Slave_IO_Running: Yes
Slave_SQL_Running: Yes
全部为yes,这样主从才配置完成

7、验证

在主库添加信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
mysql> select * from sq;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 18 |
| 2 | marry | 19 |
| 3 | rose | 20 |
| 4 | mike | 19 |
+----+-------+------+
4 rows in set (0.00 sec)

mysql> insert into sq values(5,'zhangsan',18),(6,'lisi',20),(7,'wangwu',18);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from sq;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 18 |
| 2 | marry | 19 |
| 3 | rose | 20 |
| 4 | mike | 19 |
| 5 | zhangsan | 18 |
| 6 | lisi | 20 |
| 7 | wangwu | 18 |
+----+----------+------+
7 rows in set (0.00 sec)

查看从库信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shiqian |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> use shiqian;
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 sq;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 18 |
| 2 | marry | 19 |
| 3 | rose | 20 |
| 4 | mike | 19 |
| 5 | zhangsan | 18 |
| 6 | lisi | 20 |
| 7 | wangwu | 18 |
+----+----------+------+
7 rows in set (0.00 sec)