文章目录
1 . 主从概述
主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库;主数据库一般是准实时的业务数据库。
1.1 . 主从的作用
做数据的热备
作为后备数据库,主数据库故障后,可切换到从数据库继续工作,避免数据丢失。
读写分离
使数据库能够支撑更大的并发,在报表中尤其重要。由于部分报表sql语句非常的慢,导致锁表,影响前台使用master,报表使用Slave,那么报表sql将不会造成前台锁,保证了前台速度
架构的扩展
业务量越来越大,IO访问频率过高,单机无法满足,此时做多库的存储,降低磁盘I/O访问的频率,提高单个机器的I/O性能。
1.2 . 主从形式
一主一从
主主复制
一主多从----扩展系统读取的性能,因为读是在从库读取的
多主一从----5.7开始支持
联级复制
2 . 主从复制原理
主从复制步骤:
主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
从库生成两个线程,一个I/O线程,一个SQL线程
I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3 . 主从复制配置
主从复制配置步骤:
确保从数据库与主数据库里的数据一样
在主数据库里创建一个同步账号授权给从数据库使用
配置主数据库(修改配置文件)
配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 172.16.12.145 | centos7/redhat7 mysql-5.7 | 有数据 |
从数据库 | 172.16.12.179 | centos7/redhat7 mysql-5.7 | 无数据 |
3.1 mysql安装
分别在主从两台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章。
3.2 mysql主从配置
3.2.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
[root@yeqixian ~]# mysql -e ‘show databases;’
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
//再查看从库有哪些库
[root@yer ~]# mysql -e ‘show databases;’
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
//此锁表的终端必须在备份完成以后才能退出
//备份主库并将备份文件传送到从库
[root@yeqixian ~]# mysqldump --all-databases > /opt/all-201808191200.sql
[root@yeqixian ~]# ls
anaconda-ks.cfg
[root@yeqixian ~]# ls /opt/
all-201808191200.sql
[root@yeqixian ~]# scp /opt/all-201808191200.sql root@192.168.116.179:/opt/
The authenticity of host ‘192.168.116.179 (192.168.116.179)’ can’t be established.
ECDSA key fingerprint is SHA256:N7Tr4KeNIah13/20DwbH7XQF8w+9IZVJn7m74BnMUxc.
ECDSA key fingerprint is MD5:0b:88:e1:eb:47:88:5e:ee:50:8a:dc:8c:ac:d9:16:ae.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.116.179’ (ECDSA) to the list of known hosts.
root@192.168.116.179’s password:
all-201808191200.sql 100% 503KB 70.5MB/s 00:00
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@yer ~]# mysql < /opt/all-201808191200.sql
[root@yer ~]# mysql -e ‘show databases;’
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
3.2.2 在主数据库里创建一个同步账号授权给从数据库使用
MariaDB [(none)]> CREATE USER ‘repl’@‘172.16.12.179’ IDENTIFIED BY ‘repl123’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT REPLICATION SLAVE ON . TO ‘repl’@‘172.16.12.179’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.2.3 配置主数据库
[root@localhost ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin
server-id=1
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
symbolic-links=0
//重启mysql服务
[root@yeqixian ~]# systemctl restart mariadb
[root@yeqixian ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 :3306 :
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 :::22 :::
LISTEN 0 100 ::1:25 :::
//查看主库的状态
MariaDB [(none)]> show master status;
±-----------------±---------±-------------±-----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
±-----------------±---------±-------------±-----------------+
| mysql-bin.000001 | 245 | | |
±-----------------±---------±-------------±-----------------+
1 row in set (0.00 sec)
3.2.4 配置从数据库
[root@yer ~]# vim /etc/my.cnf
//添加如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2
relay-log=mysql-relay-bin
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
symbolic-links=0
//重启从库的mysql服务
[root@yer ~]# systemctl restart mariadb
[root@yer ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 :3306 :
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 :::22 :::
LISTEN 0 100 ::1:25 :::
//配置并启动主从复制
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST=‘192.168.116.145’,
-> MASTER_USER=‘repl’,
-> MASTER_PASSWORD=‘repl123’,
-> MASTER_LOG_FILE=‘mysql-bin.000001’,
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
//查看从服务器状态
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.116.145
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Connecting
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: 154
Relay_Log_Space: 245
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error connecting to master ‘repl@192.168.116.145:3306’ - retry-time: 60 retries: 86400 message: Can’t connect to MySQL server on ‘192.168.116.145’ (113)
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
1 row in set (0.00 sec)
3.2.5 测试验证
在主服务器的student库的bj2表中插入数据:
mysql> select * from bj2;
Empty set (0.00 sec)
mysql> insert into bj2 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 bj2;
±—±------±-----+
| 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 bj2;
±—±------±-----+
| id | name | age |
±—±------±-----+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
±—±------±-----+
3 rows in set (0.00 sec)