mysql主从
-
mysgl主从
-
1.主从简介
- 1.1主从作用
- 1.2主丛形式
-
-
2.主从复制原理
-
3.主从复制配置
-
3.1 mysql安装
-
3.2 mysgl主从配置
- 3.2.1确保从数据库与主数据库里的数据一样
- 3.2.2在主数据库里创建一个同步账号授权给从数据库使用
- 3.2.3配置主数据库
- 3.2.4配置从数据库
- 3.2.5测试验证
1.主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
●用一台数据库存放数据,若此数据 库服务器宕机了导致数据丢失怎么办?
●业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?
1.1主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2主从形式
- 一主一从
- 主主复制
- 一主多从–扩展系统读取的性能,因为读是在从库读取的
- 多主一从—5.7开始支持
- 联级复制
2.主从复制原理
主从复制步骤:
- 主库将所有的写 操作记录到binlog日志中并生成一个log dump线程,将binlog日 志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log (中继日志)文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3.主从复制配置
主从复制配置步骤:
1.确保从数据库与主数据库里的数据一样
2.在主数据库里创建一个同步账号授权给从数据库使用
3.配置主数据库(修改配置文件)
4.配置从数据库(修改配置文件)
需求:
搭建两台MySQL 服务器,台作为主 服务器,-台作为从服务器, 主服务器进行写操作, 从务器进行读操作
环境说明:
数据库角色 | IP |
---|---|
主数据库 | 192.168.20.188 |
从数据库 | 192.168.20.189 |
3.1 mysql安装
分别在主从两台服务器上安装mysq1-5. 7版本,此处略过安装步骤,若有疑问请 参考《mysq基础》与《mysq|l进阶》两篇文章。
3.2 mysql主从配置
3.2.1确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据- -样,先全备主数据库并还原到从数据库中
//查看数据库
主库的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangqiang |
| ymm |
| yqm |
+--------------------+
7 rows in set (0.00 sec)
从库的数据库
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)
//此锁表的终端必须在备份完成以后才能退出
//备份主库并将备份文件传送到从库
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
[root@zhu ~]# mysqldump -uroot -pwangqiang123! --all-databases > /opt/all-202005071840.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@zhu ~]# ls /opt/
all-202005071840.sql data
[root@zhu ~]# scp /opt/all-202005071840.sql root@192.168.20.189:/opt/
The authenticity of host '192.168.20.189 (192.168.20.189)' can't be established.
ECDSA key fingerprint is SHA256:jGbA8bp7RphbXXW5GoFJmJXXz6WPlSDlr20861QZu5g.
ECDSA key fingerprint is MD5:ac:3f:95:37:fa:31:7c:a9:11:35:12:81:a4:b2:a8:16.
Are you sure you want to continue connecting (yes/no)? yes
root@192.168.20.189's password:
all-202005071840.sql 100% 777KB 17.3MB/s 00:00
//在从库上恢复主库的备份并查看,与主库保持一致
[root@cong ~]# ls /opt/
all-202005071840.sql data
[root@cong ~]# mysql -uroot -pwangqiang123! < /opt/all-202005071840.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@cong ~]# mysql -uroot -pwangqiang123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangqiang |
| ymm |
| yqm |
+--------------------+
3.2.2在数据库里创建一个同步账号授权给从数据库使用
mysql> create user 'yqm'@'192.168.20.189' identified by 'yqm123!' ;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'yqm'@'192.168.20.189';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.2.3配置主数据库
[root@zhu ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin //启用binlog日志
server-id=1 //数据库服务器唯一标识符,主库的值一定要大于从库
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重启mysql服务
[root@zhu ~]# systemctl restart mysqld
[root@zhu ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
3.2.4配置从数据库
[root@cong ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql.sock
server-id=2 //设置从库的唯一标识符,从库的值必须小于主库
relay-log=mysql-relay-bin //启动中继日志
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@cong ~]# systemctl restart mysqld
Failed to restart mysqld.service: Unit not found.
[root@cong ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//配置并启动主从复制
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.20.188',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl123',
-> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=1580;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
//查看状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.20.188
Master_User: yqm
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 423
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
3.2.5测试验证
在主服务器的yqm库的yqm表中插入数据
mysql> insert into yqm values(1,'tr',20),(2,'uu',14);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
在从库中查看数据是否同步:
mysql> select * from yqm;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tr | 20 |
| 2 | uu | 14 |
+----+------+------+
2 rows in set (0.00 sec)