一、主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?
1.主从作用
实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务
2. 主从形式
一主一从
主主复制
一主多从---扩展系统读取的性能,因为读是在从库读取的
多主一从---5.7开始支持
3.主从复制原理
主从复制步骤:
主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
从库生成两个线程,一个I/O线程,一个SQL线程
I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
二、主从复制配置
主从复制配置步骤:
1.确保从数据库与主数据库里的数据一样
2.在主数据库里创建一个同步账号授权给从数据库使用
3.配置主数据库(修改配置文件)
4.配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
主数据库 | 192.168.35.142 | rocky Linux9 | 有数据 |
从数据库 | 192.168.35.143 | rocky Linux9 | 无数据 |
主数据库
//全备主库,全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致,退出数据库即可解锁
[root@mysql ~]# mysql -uroot -p
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)//此锁表的终端必须在备份完成以后才能退出
//在锁打开情况下进行全备
[root@mysql ~]# mysqldump -uroot -predhat --all-databases > /opt/all-202408060854.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@mysql ~]# ll /opt/all-202408060854.sql
-rw-r--r-- 1 root root 876126 Aug 6 08:54 /opt/all-202408060854.sql
//发送到从库
[root@mysql ~]# scp /opt/all-202408060854.sql root@192.168.35.143:/opt/
//在主数据库里创建一个同步账号授权给从数据库使用
[root@mysql ~]# mysql -uroot -p
mysql> create user 'repl'@'192.168.35.143' identified by 'redhat'; //创建用户
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.35.143'; //赋权
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; //刷新
Query OK, 0 rows affected (0.00 sec)
//配置主数据库
[root@mysql ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin //启用binlog日志
server-id=1 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
symbolic-links=0 //符号链接
log-error=/var/log/mysqld.log //错误日志
pid-file=/var/run/mysqld/mysqld.pid
//重启服务,查看端口
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:25151 0.0.0.0:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 511 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 511 *:443 *:*
LISTEN 0 80 *:3306 *:*
//查看数据库状态
[root@mysql ~]# mysql -uroot -p
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
从数据库
//恢复数据
[root@mysql2 ~]# mysql -uroot -predhat < /opt/all-202408060854.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
//进入数据库,查看主库数据是否一致
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lsy |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use lsy;
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 | sam | 22 |
| 3 | sdam | 23 |
+----+------+------+
3 rows in set (0.00 sec)
//配置从数据库
[root@mysql2 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2 //设置从库的唯一标识符,从库的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
//重启服务
[root@mysql2 ~]# systemctl restart mysqld
[root@mysql2 ~]# 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 [::]:*
//配置并启动主从复制
mysql> change master to master_host='192.168.35.142',master_user='repl',master_password='redhat',master_log_file='mysql-bin.000001',master_log_pos=154;
//查看服务器状态,只需关注以下两点即可
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
验证
//主数据库添加数据,从数据同步数据即可
//查看日志文件看数据是否同步
//主数据库日志
mysql> show binlog events in 'mysql-bin.000001';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.37-log, Binlog ver: 4 |
| mysql-bin.000001 | 123 | Previous_gtids | 1 | 154 | |
| mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 219 | Query | 1 | 290 | BEGIN |
| mysql-bin.000001 | 290 | Table_map | 1 | 343 | table_id: 108 (lsy.student) |
| mysql-bin.000001 | 343 | Write_rows | 1 | 388 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000001 | 388 | Xid | 1 | 419 | COMMIT /* xid=22 */ |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
//从数据库日志
mysql> show relaylog events in 'mysql-relay-bin.000002';
+------------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-relay-bin.000002 | 4 | Format_desc | 2 | 123 | Server ver: 5.7.37, Binlog ver: 4 |
| mysql-relay-bin.000002 | 123 | Previous_gtids | 2 | 154 | |
| mysql-relay-bin.000002 | 154 | Rotate | 1 | 0 | mysql-bin.000001;pos=154 |
| mysql-relay-bin.000002 | 201 | Format_desc | 1 | 0 | Server ver: 5.7.37-log, Binlog ver: 4 |
| mysql-relay-bin.000002 | 320 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-relay-bin.000002 | 385 | Query | 1 | 290 | BEGIN |
| mysql-relay-bin.000002 | 456 | Table_map | 1 | 343 | table_id: 108 (lsy.student) |
| mysql-relay-bin.000002 | 509 | Write_rows | 1 | 388 | table_id: 108 flags: STMT_END_F |
| mysql-relay-bin.000002 | 554 | Xid | 1 | 419 | COMMIT /* xid=22 */ |
+------------------------+-----+----------------+-----------+-------------+---------------------------------------+
9 rows in set (0.00 sec