mysql双主双从技术

一、准备环境

[root@localhost ~]# vim /etc/hosts

192.168.40.154 master1     192.168.40.129 master2                                                                                         
192.168.40.138 slave1       192.168.40.128 slave2
注:四台虚拟机配置都相同,且防火墙关闭

二、双主设置

master1(其数据库中提前准备点库)

1、[root@localhost ~]# vim /etc/my.cnf

log_bin
server-id=1
gtid_mode=ON
enforce_gtid_consistency=1

2、[root@localhost ~]# systemctl restart mysqld

3、授权:mysql>grant replication slave,replication client on *.* to 'rep'@'192.168.40.%' identified by 'Zjz@5740';

4、mysql>  flush privileges;(刷新)

5、备份:mysqldump -p'Zjz@5740' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql

6、#scp -r 2017-1-1-mysql-all.sql master2:/tmp

7、观察二进制日志分割点

CHANGE MASTER TO MASTER_LOG_FILE='localhost-bin.000002', MASTER_LOG_POS=154;

master2

1、[root@localhost ~]#vim /etc/my.cnf( 启动二进制日志,服务器ID,GTID)

log_bin
server-id=2
gtid_mode=ON
enforce_gtid_consistency=1

2、[root@localhost ~]# systemctl restart mysqld

 还恢复手动同步数据

mysql>set sql_log_bin=0;

mysql>source /tmp/2017-1-1-mysql-full.sql

mysql>select * from master1db.master1tab;

3、设置主服务器

mysql> change master to
master_host='master1',
master_user='rep',
master_password='Zjz@5740',
master_auto_position=1;

start slave;

show slave status\G;

master1认master2为主

master2授权:mysql>grant replication slave,replication client on *.* to 'rep'@'192.168.40.%' identified by 'Zjz@5740';

flush privileges;(刷新)

master1:设置主服务器

mysql> change master to
master_host='master2',
master_user='rep',
master_password='Zjz@5740',
master_auto_position=1;

start slave;

show slave status\G;

mysqldump -p'Zjz@5740' --all-databases --single-transaction --master-data=2  --flush-logs > `date +%F`-mysql-all.sql

scp -r 2017-8-9-mysql-all.sql slave1:/tmp

scp -r 2017-8-9-mysql-all.sql slave2:/tmp

三、双从设置

slave1    # mysql -p'Zjz@5740' < /tmp/2017-8-9-mysql-all.sql

slave2     #mysql -p'Zjz@5740' < /tmp/2017-8-9-mysql-all.sql

启动从服务器ID,gtid

slave1                                                              slave2

#vim /etc/my.cnf                                              # vim /etc/my.cnf
server-id=3                                                       server-id=4
gtid_mode=ON                                                 gtid_mode=ON
enforce_gtid_consistency=1                            enforce_gtid_consistency=1
master-info-repository=TABLE                        master-info-repository=TABLE
relay-log-info-repository=TABLE                     relay-log-info-repository=TABLE

#systemctl restart mysqld                                  #systemctl restart mysqld  

设置主服务器

slave1(认两个主服务器)

mysql> change master to                                             mysql> change master to
master_host='master1',                                                master_host='master2',
master_user='rep',                                                        master_user='rep',
master_password='Zjz@5740',                                    master_password='Zjz@5740',
master_auto_position=1 for channel 'master1';            master_auto_position=1 for channel 'master1';

mysql>start slave;

mysql>show slave status\G;(双yes成功)

slave2同上操作

四、mysql主从 容器版

五、mysql主从同步原理

Maxwell 是一个用于 MySQL 的二进制日志(binlog)监听工具,它将 binlog 的增量更新作为流数据推送到 Kafka、RabbitMQ 等消息系统,或者写入其他形式的数据接收端。它可以处理大规模的数据,并具有容错能力。

Maxwell在工作时会创建一个库(默认名为maxwell),这个库是用来保存Maxwell自身的元数据,例如保存当前读取 binlog 的位置、保存 schema 的更改信息等,以便在重启后能够恢复到之前的状态。

其中maxwell库中主要包含以下几个表:

  1. positions 表:用于记录每一个 server-id 对应 binlog 的位置信息。
  2. schemas 表:记录了每一次 schema 的更改,每一行数据对应一个逻辑数据库的 schema。
  3. columns 表:记录了 schemas 表中每个 schema 对应的列信息。
  4. heartbeats 表:记录了 maxwell 的心跳信息,方便监控 maxwell 进程的工作情况。

使用 MaxWell 的过程中,如果需要间断或者切换数据同步进程,可以通过操作maxwell库中的信息来达到无缝切换的效果。

注意在使用Maxwell时,需要确保MySQL的binlog格式为row,并且打开了binlog_row_image=FULL的配置。

6、总结

1、出现connettion,重启电脑试试

2、有些步骤先停止slave再操作

3、[root@slave1 ~]#  mysql -p'Zjz@5740' < /tmp/2019-08-29-mysql-all.sqlmysql:

[Warning] Using a password on the command line interface can be insecure.

ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

解决办法:mysql> reset master;(进入slave1)    

Query OK, 0 rows affected (0.10 sec)    

mysql> \q 

Bye

4、Slave_IO_Running: No

  Slave_SQL_Running: Yes

报错:Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.'

解决:这个应该是由于你在主库上执行过purge binary logs,然后当从库change master的时候,却要执行那些事务。
你可以在主库上先查找哪些gtid被purge了。
show global variables like 'gtid_purged';
然后拿着这个value,去从库上依次
stop slave;
set global gtid_purged = 'xxx'; # xxx是你主库上查到的value。
start slave;
这样能跳过执行被主库已经purge的事务了。

 

 https://www.iirwt.com/2020/08/1178.html  容器版主从复制

posted @ 2019-08-29 19:56  凡人半睁眼  阅读(710)  评论(0编辑  收藏  举报