使用主从的原因:
- 用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
- 业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?
主从的作用:
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
主从形式
- 一主一从
- 主主复制
- 一主多从---扩展系统读取的性能,因为读是在从库读取的
- 多主一从---5.7开始支持
- 联级复制
主从复制原理
主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
1 主从复制配置(传统主从配置)
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL
服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.31.129 | centos8/redhat8 mysql-5.7 | 有数据 |
从数据库 | 192.168.31.131 | centos8/redhat8 mysql-5.7 | 无数据 |
1.1 mysql安装
1.2主从配置
1.2.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
主库192.168.31.129
从库192.168.31.131
//先查看主库有哪些库
[root@localhost ~]# mysql -uroot -p'123456'
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
//再查看从库有哪些库
[root@localhost ~]# mysql -uroot -p'ltt429520.' -h127.0.0.1 -P3306
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
//此锁表的终端必须在备份完成以后才能退出
//备份主库并将备份文件传送到从库
[root@localhost ~]# mysqldump -uroot -p123456 --all-databases > /opt/all-$(date '+%Y%m%d%H%M%S').sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls /opt/
all-20220703201201.sql data
[root@localhost ~]# scp /opt/all-20220703201201.sql root@192.168.31.131:/opt/
The authenticity of host '192.168.31.131 (192.168.31.131)' can't be established.
ECDSA key fingerprint is SHA256:SnDB0Bvu9G0De5LWH0eKRgDOaZghNQ1WABlU049ZEew.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.31.131' (ECDSA) to the list of known hosts.
root@192.168.31.131's password:
all-20220703201201.sql 100% 854KB 101.6MB/s 00:00
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@localhost ~]# mysql -uroot -p'ltt429520.' -h127.0.0.1 -P3306 < /opt/all-20220703201201.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p'ltt429520.' -h127.0.0.1 -P3306
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 4
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
1.2.2 在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave on *.* to 'repl'@'192.168.31.131' i
dentified by 'repl123!';
Query OK, 0 rows affected, 1 warning (0.00 sec) //从主机的ip
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
1.2.3 配置主数据库
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# 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
server-id = 10 //添加。主库的server-id值必须比从库的小
log-bin = mysql_bin
//重启
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enable>
Active: active (running) since Sun 2022-07-03 20:25:21 CST; 18>
Process: 1883 ExecStop=/usr/local/mysql/support-files/mysqld st>
Process: 1910 ExecStart=/usr/local/mysql/support-files/mysqld s>
Main PID: 1924 (mysqld_safe)
Tasks: 28 (limit: 11216)
Memory: 181.6M
CGroup: /system.slice/mysqld.service
├─1924 /bin/sh /usr/local/mysql/bin/mysqld_safe --data>
└─2138 /usr/local/mysql/bin/mysqld --basedir=/usr/loca>
Jul 03 20:25:20 localhost.localdomain systemd[1]: mysqld.service:>
Jul 03 20:25:20 localhost.localdomain systemd[1]: Stopped mysql s>
Jul 03 20:25:20 localhost.localdomain systemd[1]: Starting mysql >
Jul 03 20:25:21 localhost.localdomain mysqld[1910]: Starting MySQ>
Jul 03 20:25:21 localhost.localdomain systemd[1]: Started mysql s>
[root@localhost ~]#
//查看主库的状态
[root@localhost ~]# mysql -uroot -p'123456'
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)
1.2.4 配置从数据库
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log
server-id = 20
relay-log = myrelay
//重启
[root@localhost ~]# systemctl restart my3306
[root@localhost ~]# systemctl status my3306
● my3306.service - 3306 server daemon
Loaded: loaded (/usr/lib/systemd/system/my3306.service; enable>
Active: active (running) since Sun 2022-07-03 20:33:25 CST; 12>
Process: 4339 ExecStop=/usr/bin/ps -ef|grep 3306|grep -v grep|a>
Process: 4346 ExecStart=/usr/local/mysql/bin/mysqld_multi start>
Main PID: 4352 (mysqld_safe)
Tasks: 28 (limit: 11088)
Memory: 186.8M
CGroup: /system.slice/my3306.service
├─4352 /bin/sh /usr/local/mysql/bin/mysqld_safe --data>
└─4516 /usr/local/mysql/bin/mysqld --basedir=/usr/loca>
Jul 03 20:33:25 localhost.localdomain systemd[1]: my3306.service:>
Jul 03 20:33:25 localhost.localdomain systemd[1]: Stopped 3306 se>
Jul 03 20:33:25 localhost.localdomain systemd[1]: Starting 3306 s>
Jul 03 20:33:25 localhost.localdomain systemd[1]: Started 3306 se>
[root@localhost ~]#
//防火墙关掉
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# getenforce
Disabled
[root@localhost ~]# systemctl disable --now firewalld
//配置并启动主从复制
mysql> change master to
-> master_host='192.168.31.129',
-> master_user='repl',
-> master_password='repl123!',
-> master_port=3306,
-> master_log_pos='mysql_bin.000001',
-> mysql_log_poss=154;
mysql> start slave;
//查看从服务器状态
mysql> show slave status \G
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //此处必须为Yes
Slave_SQL_Running: Yes //此处必须为Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
1.2.5 测试验证
在主服务器的student库的ltt1表中插入数据:
mysql> select * from ltt1;
Empty set (0.00 sec)
mysql> insert into ltt1 values (1,'sean',20),(2,'tom',23),(3,'jerry',30);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from ltt1;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
+----+-------+------+
3 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
mysql> use student;
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 ltt1;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
+----+-------+------+
3 rows in set (0.00 sec)