2019独角兽企业重金招聘Python工程师标准>>>
如需转载请注明出处https://my.oschina.net/feistel/blog/2995251
异步方式,是MySQL的默认方式
我丑 话说在前头:
192.168.1.116 主masterA Windows MySQL8.0
192.168.1.200 备库B,VMUbuntu18 MySQL8.0
vip:192.168.1.201
shell代表Ubuntu终端
cmd代表Windows终端
----------------------------------------------------------------------------------------------------------------
1
修改B的server-id为2,两个库的server-id不能相同
在B etc/mysqld.cnf里的[mysqld]添加:
# Server Id.
server-id=2
#开启binlog写二进制日志文件:
log_slave_updates=1
保存
----------------------------------------------------------------------------------------------------------------
2
每次修改配置文件需要重启服务以便载入
shell>service mysql restart
B mysql>show variables like "%id";
B mysql>show variables like "%slave";
查看相应值是否修改成功,否则说明配置文件不对
说明:如果etc/mysqld.cnf里新加的参数不对或打错什么的,重启数据库服务会出错
关闭AB防火墙,Windows控制面板,Ubuntu为:
shell>service iptables stop
----------------------------------------------------------------------------------------------------------------
3
创建主从复制账号并授权:
A mysql>create user 'lgl'@'%' identified by '123456';
A mysql>grant replication slave on *.* to 'lgl'@'%';
A mysql>flush privileges;
----------------------------------------------------------------------------------------------------------------
4
使用mysqldump备份,保证主从库里的数据库、表和数据一致
cmd>mysqldump --single-transaction -u root -p --master-data=2 -A >E:\all_20181229.sql
--master-data=2为了获取sql文件里的binlog文件与position号:
-- CHANGE MASTER TO MASTER_LOG_FILE='LGL-PC-bin.000025', MASTER_LOG_POS=1153426;
shell>mysql -u root -p < /home/lgl/公共的/MySQL/all_20181229.sql
相关:MySQL 8.0 mysqldump备份与恢复
----------------------------------------------------------------------------------------------------------------
5
B mysql>
CHANGE MASTER TO
MASTER_HOST='192.168.1.116',
MASTER_USER='lgl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='LGL-PC-bin.000025',
MASTER_LOG_POS=1153426;
开始主从同步:
B mysql>start slave;
B mysql>show slave status\G;
看到如下,表示成功:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
----------------------------------------------------------------------------------------------------------------
5'(附加)
上面是使用binlog+position的搭建方式,这里介绍更快的方式:GTID
在B etc/mysqld.cnf里的[mysqld]添加:
#开启GTID
gtid-mode=on
enforce_gtid_consistency=on
在A mysql.ini里的[mysqld]添加:
#开启GTID
gtid-mode=on
enforce_gtid_consistency=on
都需要重启服务!
B mysql>
CHANGE MASTER TO
MASTER_HOST='192.168.1.116',
MASTER_USER='lgl',
MASTER_PASSWORD='123456',
master_auto_position=1;
对于需要多主一从情况(假设另一主库为115,账号为lgk):
B mysql>
CHANGE MASTER TO
MASTER_HOST='192.168.1.116',
MASTER_USER='lgl',
MASTER_PASSWORD='123456',
master_auto_position=1 for CHANNEL 'm1';
CHANGE MASTER TO
MASTER_HOST='192.168.1.115',
MASTER_USER='lgk',
MASTER_PASSWORD='123456',
master_auto_position=1 for CHANNEL 'm2';
----------------------------------------------------------------------------------------------------------------
6
如果Slave_IO_Running: No
参考:mysql主从复制之异常解决--- Slave_IO_Running: NO 之三大原因
如果Slave_SQL_Running: No
B mysql>
stop slave;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
----------------------------------------------------------------------------------------------------------------
7
在A新建表
B会与A执行相同的操作
----------------------------------------------------------------------------------------------------------------
至此,共勉,如需转载请注明出处https://my.oschina.net/feistel/blog/2995251