MySQL8.0一般主从复制和GTID(唯一标识符)的主从复制
程序员文章站
2024-02-29 21:21:28
...
主从复制
实验环境:
yulong01(主库) 192.168.136.157
yulong02(从库) 192.168.136.158
1.选择归档日志格式(主库):
mysql> set global binlog_format = 'ROW';
mysql> show variables like "%binlog_format%";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
1 row in set (0.00 sec)
2.主库上启用二进制日志并且设置server id(主库)
[aaa@qq.com ~]# vim /etc/my.cnf
键入:
log_bin=/var/lib/binlogs/master
server_id=157
3.更改日志目录的权限,确保正常启动(主库)
[aaa@qq.com ~]# chmod 777 /var/lib/binlogs/
[aaa@qq.com ~]# systemctl restart mysqld
4.查看二进制配置(主库)
mysql> show variables like '%log_bin%';
+---------------------------------+-------------------------------+
| Variable_name | Value |
+---------------------------------+-------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/binlogs/master |
| log_bin_index | /var/lib/binlogs/master.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-------------------------------+
6 rows in set (0.01 sec)
5.查看server id(主库)
mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 157 |
| server_id_bits | 32 |
+----------------+-------+
2 rows in set (0.00 sec)
6.查看当前主库binary log文件名和位置(主库)
mysql> mysql> show master status\G
*************************** 1. row ***************************
File: master.000001
Position: 155
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
7.主库上创建一个复制用户,从库使用此账号连接到主库,传统方式验证。(主库)
mysql> create user 'binlog_user'@'%' identified with mysql_native_password by 'Com.123!';
授权:
mysql> grant replication slave on *.* to 'binlog_user'@'%';
Query OK, 0 rows affected (0.01 sec)
8.设置唯一的server id(从库,id不可一样)
[mysqld]
log_bin=/var/lib/binlogs/slave01
server_id=158
9.设置二进制目录的权限,重启服务(从库)
[aaa@qq.com ~]# chmod 777 /var/lib/mysql/binlogs/
[aaa@qq.com ~]# systemctl restart mysqld
10.主库上进行授权
mysql> create user 'root'@'192.168.136.%' identified with mysql_native_password by 'Com.123!';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to 'root'@'192.168.136.%';
Query OK, 0 rows affected (0.01 sec)
11.放行3360(主库)
[aaa@qq.com ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[aaa@qq.com ~]# firewall-cmd --reload
success
12.从库上进行备份
[aaa@qq.com ~]# mysqldump -h 192.168.136.157 -uroot --password='Com.123!' --all-databases --routines --events --single-transaction --master-data > dump.sql
13.备份已经完成,恢复此备份(从库)
[aaa@qq.com ~]# mysql -uroot -pCom.123! < dump.sql
14.查看主库的文件和pos:
mysql> show master status\G
*************************** 1. row ***************************
File: master.000001
Position: 1196
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
14.从库上,恢复数据备份后,必须执行一下命令:
mysql> change master to master_host='192.168.136.157',
-> master_user='binlog_user',master_password='Com.123!',
-> master_log_file='master.000001',master_log_pos=1196;
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 #查看同步信息
15.验证同步情况(题目是创建数据库并插入数据,我这里是删库)
在这里插入图片描述
GTID(唯一标识符备份):
1.已经在服务器上设置过复制
2.查看当前服务器的GTID设置 (主从同时进行):
mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | OFF |
| gtid_executed | |
| gtid_executed_compression_period | 1000 |
| gtid_mode | OFF |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-----------+
9 rows in set (0.01 sec)
3.修改配置文件
#主库:
vim /etc/my.cnf
键入:
gtid_mode=ON
enforce_gtid_consistency=true
#从库:
vim /etc/my.cnf
键入:
gtid_mode=ON
enforce_gtid_consistency=true
skip_slave_start #执行start slave之后,从库才会启动
4.将主库设置为只读,并确保所用从库都能与主库同步。因为主从之间不应该有任何数据的不一致
mysql> set @@global.read_only = ON;
5.重启所有库使GTID生效(主从)
[aaa@qq.com ~]# systemctl restart mysqld
[aaa@qq.com ~]# systemctl restart mysqld
6.从库上执行命令来设置GTID复制:
mysql> change master to master_host='192.168.136.157',
-> master_port=3306,master_user='binlog_user',
-> master_password='Com.123!',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
7.从库上执行start slave:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
若有疑问,请添加QQ(2594676962),博主常在线
上一篇: 绝对路径和相对路径
下一篇: MySQL全局共享内存介绍