Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger
Master-slave database configuration process:
5.7.18 Installation link:
https://www.hi-linux.com/posts/47176.html
MySQL official website explanation:
https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-howto.html
First prepare two servers as the master database and the slave database:
Back up the configuration files in both databases:
cp /etc/my.cnf /etc/my.cnf.bar
Configure the master-slave server: (recommended to clear the original file)
(
Windows
system MySQL configuration file location:
C:\ProgramData\MySQL\MySQL Server 5.7/my.ini
Linux
system configuration file location: /etc/my.cnf )
MySQL master server configuration snippet:
[mysqld]
<-- This thing must have, otherwise the program cannot find it is configured for that program, and an error will be reported -->
server-id = 1
gtid_mode = ON
enforce_gtid_consistency = ON
binlog_format = row
expire_logs_days = 30
max_binlog_size = 100M
binlog-checksum = CRC32
master-verify-checksum = 1
log-bin = /var/log/mysql/mysql-bin
log_bin_index = /var/log/mysql/mysql-bin.index
log-slave-updates = ON
#binlog
log_bin=master-binlog
log-slave-updates=1
binlog_format=row
#强烈建议,其他格式可能造成数据不一致
#relay log
skip_slave_start=1
MySQL从服务器配置片断:
[mysqld]
<--这玩意一定要有,否则程序找不到是给那个程序配置的,会报错 -->
server-id = 2
gtid_mode = ON
enforce_gtid_consistency = ON
log-slave-updates = ON
skip-slave-start = true
expire_logs_days = 30
max_binlog_size = 100M
read_only = ON
slave-sql-verify-checksum = 1
log-bin = /var/log/mysql/mysql-bin
log_bin_index = /var/log/mysql/mysql-bin.index
relay-log = /var/log/mysql/relay-log
relay-log-index = /var/log/mysql/relay-log-index
relay-log-info-file = /var/log/mysql/relay-log.info
master-info-repository = table
relay-log-info-repository = table
relay-log-recovery = ON
report-port = 3306
report-host = 192.168.2.212
replicate-do-db = master1
replicate_wild_do_table=master1.%
#binlog
log-bin=slave-binlog
log-slave-updates=1
binlog_format=row
#强烈建议,其他格式可能造成数据不一致
#relay log
skip_slave_start=1
- 重启(主从)MySQL服务器
service mysql.server restart (具体启动代码根据相应的命令)
service mysqld status 状态
- 创建具有复制权限的用户
mysql 在主服务器上创建一个用户让从服务器根据该用户登录主数据库拿取数据
# 在MySQL主服务器上创建
mysql> grant all on *.* to 'shixu'@'172.16.20.229' identified by '123456';
mysql> flush privileges;
授权用户使用IP地址访问数据库(
该步在数据库安装后操作
)
grant all on *.* to 'root'@'%' identified by 'shixu@2017';
- 查看主库与从库的GTID是否开启
show variables like "%gtid%";
- 查看服务器server_uuid
show variables like '%server_uuid%';
- 查看主服务器状态(查看logfile及节点)
show master status;
- 从库连接至主库(在从数据库中根据上面创建的用户登录主数据库)
CHANGE MASTER TO
MASTER_HOST='
172.16.20.229
',
MASTER_USER='shixu',
MASTER_PASSWORD='123456',
MASTER_AUTO_POSITION=1;
(支持事物,=0不支持事物)
- 在从服务器上启动复制
start slave;
(stop slave;关闭复制)
- 启动成功后查看SLAVE的状态
show slave status \G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
确认
Slave_IO_Running
和
Slave_SQL_Running
两个参数都为 Yes 状态。
- 在主服务器上查看从库的主机信息
show slave hosts;
- 测试GTID主从复制,检查从服务器状态
flush logs;(刷新日志)
(慎用)
reset master; (当设定为一个起点之后,就不要轻易使用该命令,否则之前的所有操作将不会同步到从服务器)
将bin_log日志置为0;将bin_log日志置为初始状态。
强制同步数据:(同步前关闭slave)
change master
to
master_auto_position
=
0
; (将事物关闭,否则同步binlog时同步出错)
change master to
master_host='172.16.20.229',
master_user='shixu',
master_password='123456',
master_port=3306,
master_log_file='master-binlog.000004',
master_log_pos=194;
相关知识:
MySQL集群,A创建数据库在B中创建同名数据库就会自动同步,MySQL集群不会同步数据库只会同步表