MySQL主从
1. 主从简介
MySQL的主从是对数据进行存储备份,从而避免影响业务
1.1 主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2 主从形式
主从的形式有很多种,根据需求来设置
- 一主一从
- 主主复制
- 一主多从----扩展系统读取的性能,因为读是在从库读取的
- 多主一从----5.7开始支持
- 联级复制
2. 主从复制原理
- 主库将所有的写操作记录到binlog日志中;
- 从库生成两个线程,一个I/O线程,一个SQL线程;
- I/O线程去请求主库的binlog;
- 主库会生成一个log dump线程,用来给从库的I/O线程传binlog;
- 从库会将得到的binlog日志写到relay log(中继日志)文件中;
- SQL线程会读取中继日志文件,并解析成具体的操作执行,这样主从的操作就一致了,而最终的数据也就一致了。
3.主从复制配置
3.1 主从部署的必要条件
主库开启binlog日志(设置log-bin参数)
主从server-id不同(主数据库的标识符要比从数据库的标识符小)
从库服务器能连同主库
3.2 主从复制配置步骤
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
3.3 MySQL安装(略)
3.4 MySQL主从配置
3.4.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//若是想要不输入用户和密码登录,则在当前用户的家目录下创建一个隐藏文件,文件名是.my.cnf
[root@localhost ~]# vim .my.cnf
[root@localhost ~]# cat .my.cnf
[client]
user = root
password = zml123!
//这样可以保护数据库的安全性
//先查看主库有哪些库
[root@localhost ~]# mysql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| zabbix |
+--------------------+
6 rows in set (0.00 sec)
//再查看从库有哪些库
[root@localhost ~]# mysql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
[root@localhost ~]# mysql
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
//此锁表的终端必须在备份完成以后才能退出
//备份主库并将备份文件传送到从库
[root@localhost ~]# mysqldump --all-databases > all-backup201902271501
[root@localhost ~]# scp all-backup201902271501 root@192.168.233.129:/root/
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@localhost ~]# mysql < all-backup201902271501
[root@localhost ~]# mysql -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| zabbix |
+--------------------+
[root@localhost ~]# mysql -e "show tables from school;"
+------------------+
| Tables_in_school |
+------------------+
| student |
+------------------+
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
3.4.2 在主数据库里创建一个同步账号授权给从数据库使用
[root@localhost ~]# mysql
mysql> create user 'zqq'@'192.168.233.128' identified by 'zqq123!';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'zqq'@'192.168.233.128';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.4.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
log-bin = mysql-bin //启用binlog日志
server-id = 5 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
//重启mysql服务
[root@localhost ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@localhost ~]# service mysqld start
Starting MySQL. SUCCESS!
//查看主库的状态
[root@localhost ~]# mysql -e "show master status;"
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
3.4.4 配置从数据库
[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
relay-log=mysql-relay-log //启用中继日志relay-log
server-id = 6 //设置从库的唯一标识符,从库的server-id值必须大于主库的该值
//重启从库的mysql服务
[root@localhost ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@localhost ~]# service mysqld start
Starting MySQL.. SUCCESS!
//配置并启动主从复制
[root@localhost ~]# mysql
mysql> CHANGE MASTER TO MASTER_HOST='192.168.233.132',MASTER_USER='zqq',MASTER_PASSWORD='zqq123!',MASTER_LOG_FILE='mysql_log.000001',MASTER_LOG_POS=773;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
//查看从服务器状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.233.132
Master_User: zqq
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_log.000001
Read_Master_Log_Pos: 773
Relay_Log_File: mysql-relay-log.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql_log.000001
Slave_IO_Running: Yes
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: 773
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: 5
Master_UUID: d589b398-34e8-11e9-8ee2-000c295aa226
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.4.5 测试验证
在主服务器的school库的student表中插入数据:
[root@localhost ~]# mysql -uroot -pzml123!
mysql> use school;
mysql> insert into student values(7,'zml',21),(8,'zqq',13);
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
| 6 | dsz | 19 |
| 7 | zml | 21 |
| 8 | zqq | 13 |
+----+-------+------+
8 rows in set (0.01 sec)
在从数据库中查看数据是否同步:
[root@localhost ~]# mysql -uroot -pzml123!
mysql> select * from school.student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
| 6 | dsz | 19 |
| 7 | zml | 21 |
| 8 | zqq | 13 |
+----+-------+------+
8 rows in set (0.00 sec)
4. GTID主从复制
GTID是一个基于原始mysql服务器生成的一个已经被成功执行的全局事务ID,它由服务器ID以及事务ID组合而成。这个全局事务ID不仅仅在原始服务器器上唯一,在所有存在主从关系 的mysql服务器上也是唯一的。正是因为这样一个特性使得mysql的主从复制变得更加简单,以及数据库一致性更可靠。
4.1 GTID的概念
- 全局事务标识:global transaction identifiers;
- GTID是一个事务一一对应,并且全局唯一ID;
- 一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致;
- GTID用来代替传统复制方法,不在使用MASTER_LOG_FILE+MASTER_LOG_POS开启复制。而是使用MASTER_AUTO_POSTION=1的方式开始复制;
- MySQL-5.6.5开始支持的,MySQL-5.6.10后开始完善;
- 在传统的slave端,binlog是不用开启的,但是在GTID中slave端的binlog是必须开启的,目的是记录执行过的GTID(强制)。
4.2 GTID的组成
GTID = source_id:transaction_id
- source_id,用于鉴别原服务器,即mysql服务器唯一的的server_uuid,由于GTID会传递到slave,所以也可以理解为源ID。
- transaction_id,为当前服务器上已提交事务的一个序列号,通常从1开始自增长的序列,一个数值对应一个事务。
示例:
- 3E11FA47-71CA-11E1-9E33-C80AA9429562:23
- 前面的一串为服务器的server_uuid,即3E11FA47-71CA-11E1-9E33-C80AA9429562,后面的23为transaction_id
4.3 GTID的优势
- 更简单的实现failover,不用以前那样在需要找log_file和log_pos;
- 更简单的搭建主从复制;
- 比传统的复制更加安全;
- GTID是连续的没有空洞的,保证数据的一致性,零丢失。
4.4 GTID的工作原理
- 当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中;
- binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,即告诉Slave,下一个要执行的GTID值;
- sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID;
- 如果有记录,说明该GTID的事务已经执行,slave会忽略;
- 如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行;
- 在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
5. 配置GTID
- 配置主库配置文件(/etc/my.cnf)并重启mysql
[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 = 5
log-bin = mysql_log
gtid_mode = ON
enforce-gtid-consistency = true
[root@localhost ~]# mysql -uroot -pzml123!
mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql_log.000002 | 154 | | | d589b398-34e8-11e9-8ee2-000c295aa226:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)
- 配置从库配置文件
[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 = 6
relay-log = mysql-relay-log
gtid_mode = ON
enforce-gtid-consistency = true
relay-log = mysql-relay-log1
- 配置从数据库
[root@localhost ~]# service mysqld start
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.233.132',master_user='zqq',master_password='zqq123!',master_auto_position=1;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.233.132
Master_User: zqq
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_log.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-log1.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql_log.000002
Slave_IO_Running: Yes
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: 575
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: 5
Master_UUID: d589b398-34e8-11e9-8ee2-000c295aa226
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: d589b398-34e8-11e9-8ee2-000c295aa226:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
- 测试
//在从库查看备份的数据库中的student表
mysql> select * from school.student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
| 6 | dsz | 19 |
| 7 | xr | 22 |
| 8 | zxj | 90 |
+----+-------+------+
8 rows in set (0.02 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> insert into student value(9,'gg',40);
Query OK, 1 row affected (0.01 sec)
mysql> select * from school.student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
| 6 | dsz | 19 |
| 7 | xr | 22 |
| 8 | zxj | 90 |
| 9 | gg | 40 |
+----+-------+------+
9 rows in set (0.00 sec)
//在从库中查看
mysql> select * from school.student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 12 |
| 2 | jerry | 13 |
| 3 | jack | 11 |
| 4 | xxy | 10 |
| 5 | harry | 12 |
| 6 | dsz | 19 |
| 7 | xr | 22 |
| 8 | zxj | 90 |
| 9 | gg | 40 |
+----+-------+------+
9 rows in set (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.233.132
Master_User: zqq
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_log.000002
Read_Master_Log_Pos: 424
Relay_Log_File: mysql-relay-log1.000003
Relay_Log_Pos: 637
Relay_Master_Log_File: mysql_log.000002
Slave_IO_Running: Yes
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: 424
Relay_Log_Space: 845
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: 5
Master_UUID: d589b398-34e8-11e9-8ee2-000c295aa226
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: d589b398-34e8-11e9-8ee2-000c295aa226:2
Executed_Gtid_Set: d589b398-34e8-11e9-8ee2-000c295aa226:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)