mysql主从同步
mysql主从同步
1. 概念
MySQL 主从复制是指数据可以从一个MySQL数据库服务器主节点复制到一个或多个从节点。MySQL 默认采用异步复制方式,这样从节点不用一直访问主服务器来更新自己的数据,数据的更新可以在远程连接上进行,从节点可以复制主数据库中的所有数据库或者特定的数据库,或者特定的表。
主从复制的逻辑有以下几种:
一主一从:单向主从同步模式,只能在Master端写入数据。
一主多从:提高系统的读性能。
双主复制:此架构可以在Master1或Master2进行数据写入,或者两端同时写入(特殊设置)。
多主一从(MySQL5.7开始支持)
级联复制
为什么mysql要设置主从复制?
-
数据库数据是一个公司或者集团企业最为重要的资产,以防数据的丢失和损坏,需要进行备份
-
当用户的访问量越来越高的时候,一旦查询也就是读取数据的操作太频繁了,势必网站崩掉,服务器宕机,很影响用户的体验度
-
提高数据库系统的可用性
2.mysql主从复制原理
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
从服务器slave为什么不能直接存储二进制日志文件里面的数据(为什么不开启bin-log日志)?
本来做数据的主从同步就是为了让计算机快速的进行读写操作,而且是大批量的数据,一旦大量数据进行写入或者更新数据,从数据库slave如果直接从二进制日志来接收,数据是以队列形式进行传输的,若队列的数据没有快速处理,堆积起来,从服务器可能也会崩溃宕机,所以从性能上考虑,从服务器slave创建了I/O线程对象将数据转到中继日志,起个缓存功能。
3.mysql主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL
服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库node1) | 192.168.32.130 | centos7/redhat7 mysql-5.7 | 有数据 |
从数据库(node2) | 192.168.32.135 | centos7/redhat7 mysql-5.7 | 无数据 |
3.1 mysql安装
分别在主从两台服务器上安装mysql-5.7
版本,此处略过安装步骤
mysql二进制安装
[root@node1 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.29, for linux-glibc2.12 (x86_64) using EditLine wrapper
[root@node2 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.29, for linux-glibc2.12 (x86_64) using EditLine wrapper
3.2 mysql主从配置
3.2.1在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave,super on *.* TO 'repl'@'192.168.32.135' identified by 'repl123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.2.2 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
#查看主库有哪些数据库
[root@node1 ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
#查看从库有哪些库
[root@node2 ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
#全备主库
#全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
[root@node1 ~]# mysqldump -uroot -p123456 --all-databases > /opt/all-202006181800
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@node1 ~]# ls /opt/
all-202006181800 data
#用scp工具把全备的文件上传到从库
[root@node1 ~]# scp /opt/all-202006181800 root@192.168.32.135:/opt/
The authenticity of host '192.168.32.135 (192.168.32.135)' can't be established.
ECDSA key fingerprint is SHA256:BFDlHGdhFuhdlBPVKeGY6OhFc/KEMiVvKW1cKrir4uM.
ECDSA key fingerprint is MD5:85:bf:36:c6:8c:13:55:9d:70:d1:77:dc:8e:31:1f:b6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.32.135' (ECDSA) to the list of known hosts.
root@192.168.32.135's password:
all-202006181800 100% 831KB 68.4MB/s 00:00
#在从库上恢复主库的备份并查看,确保与主库一致
[root@node2 ~]# ls /opt/
all-202006181800 data
[root@node2 ~]# mysql -uroot -p123456 < /opt/all-202006181800
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@node2 ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
3.2.3 配置主数据库
开启二进制日志
[root@node1 ~]# vim /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
log-bin = mysql_bin //启用binlog日志
server-id=10 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
#重启mysql服务
[root@node1 ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
[root@node1 ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
LISTEN 0 128 [::]:22 [::]:*
#查看主库的状态
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)
3.2.4 配置从数据库
[root@node2 ~]# vim /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 = 20
relay-log = mysql-relay-bin
#重启从库的mysql服务
[root@node2 ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@node2 ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
LISTEN 0 128 [::]:22 [::]:*
#配置并启动主从复制
mysql> change master to
-> master_host='192.168.32.130',
-> master_user='repl',
-> master_password='repl123',
-> master_log_file='mysql_bin.000001',
-> 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.32.130
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.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //此处必须为Yes
Slave_SQL_Running: Yes //此处必须为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: 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: 10
Master_UUID: cc457968-b2a2-11ea-8cf5-000c29d7d941
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)
3.2.5 验证
在主库的school库的student表中插入数据
mysql> use school;
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 | jerry | 25 |
| 3 | wangwu | 16 |
| 4 | lisi | 33 |
| 5 | zhangsan | 50 |
+----+----------+------+
5 rows in set (0.00 sec)
mysql> insert student(name,age) values('test1',22),('test2',33);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | jerry | 25 |
| 3 | wangwu | 16 |
| 4 | lisi | 33 |
| 5 | zhangsan | 50 |
| 6 | test1 | 22 |
| 7 | test2 | 33 |
+----+----------+------+
7 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
mysql> use school;
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 | jerry | 25 |
| 3 | wangwu | 16 |
| 4 | lisi | 33 |
| 5 | zhangsan | 50 |
| 6 | test1 | 22 |
| 7 | test2 | 33 |
+----+----------+------+
7 rows in set (0.00 sec)