实现互为双主搭建MySQL数据库方法

发布时间:2020-05-23 15:00:16 作者:三月
来源:网络 阅读:184

本文主要给大家简单讲讲实现互为双主搭建MySQL数据库方法,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望实现互为双主搭建MySQL数据库方法这篇文章可以给大家带来一些实际帮助。

1.双主实现方案:

1、让表的id 自增,然后主一写1,3,5,,,,,主2写2,,4,6,,,,

2、不让表的id 自增,然后通过web程序去seq云服务器读取id ,写入双主。

双主工作场景:高鬓发写的场景,慎用。

和多实例及主从同步对比:主主数据库多增加的参数:

2.第一个方法实现主主复制:

1、主从同步【主库】M需要增加的参数:

auto_increment_increment        = 2

auto_increment_offset           = 1

log-slave-updates

log-bin = /data/3306/mysql-bin

expire_logs_days = 7

/etc/init.d/mysqldrestart

2、主从同步【从库】S需要增加的参数:

auto_increment_increment        = 2

auto_increment_offset           = 2

log-bin = /data/3307/mysql-bin

log-slave-updates

expire_logs_days = 7

/etc/init.d/mysqld restart

3.参数配置生效验证:

mysql -uroot -p123456 -e "showvariables like 'log_%';"|grep -i "on"

mysql -uroot -p123456 -e "showvariables like 'log_%';"|grep -i "on"

mysql -uroot -p123456 -e "showvariables like 'auto%';"|egrep "2|1|2"

mysql -uroot -p123456 -e "showvariables like 'auto%';"|egrep "2|1|2"

4.对从库mysql-5.6.16-slave1备份:

mysqldump -uroot -p123456 -B -A -F -R -x--master-data=1  --events|gzip>/backup/slave_$(date +%F).sql.gz

scp -rp -P22/backup/slave_2016-08-19.sql.gz root@172.16.1.41:/backup

5.主库导入由从库推送过来的数据:

gzip -d /backup/slave_2016-08-19.sql.gz

mysql -uroot -p123456</backup/slave_2016-08-19.sql

6.在主库执行change master to

mysql -uroot -p123456 -e "CHANGEMASTER TO  MASTER_HOST='172.16.1.42',MASTER_PORT=3306,MASTER_USER='rep',MASTER_PASSWORD='111111';startslave;”

[root@mysql-5 data]# mysql -uroot-p123456 -e "show slave status\G"|grep -i yes

Warning: Using a password on the commandline interface can be insecure.

            Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

172.16.1.42这个ip地址为从库MySQL-5.6.16-slave1的ip地址

到此处实现双主

7.演示:

Master1端插入数据演示:

Master1:172.16.1.41

mysql -uroot -p123456 -e "create databasewjw05"

mysql> use wjw01;

mysql> CREATE table `t1` (`id` bigint(12) NOTNULL auto_increment,`name` varchar(12) NOT NULL,PRIMARY KEY (`id`) );

mysql> INSERT INTO t1(name) values('wangwu');

slave2:172.16.1.43

[root@mysql-5 backup]# mysql -uroot-p123456 -e "select * from wjw01.t1"

Warning: Using a password on the commandline interface can be insecure.

+----+---------+

| id | name    |

+----+---------+

| 1 | wangwu  |

在从库slave2查看 ,数据已经同步过来了

Master2___slave1:172.16.1.42

mysql -uroot -p123456 -e "select *from wjw01.t1"

主库master1 的数据没有同步到主库master2__slave1

Master2__slave1端插入数据演示:

mysql> use wjw01;

mysql> INSERT INTO t1(name) values('oldgirl');

slave2:172.16.1.43查看:

[root@mysql-5 backup]# mysql -uroot -p123456 -e"select * from wjw01.t1"

Warning: Using a password on the command lineinterface can be insecure.

+----+---------+

| id | name   |

+----+---------+

|  1 | wangwu  |

|  2 |oldgirl |

数据同步过去了

Master1:172.16.1.41查看:

[root@mysql-5 data]# mysql -uroot -p123456-e "select * from wjw01.t1"

Warning: Using a password on the commandline interface can be insecure.

+----+---------+

| id | name    |

+----+---------+

|  1| wangwu  |

Master2_slave1端的数据库没有同步到master1端

在master2__slave1端和master1端查看:同步过程中并没有报错,太奇怪了

[root@mysql-5 binlog]#  mysql -uroot -p123456 -e "show slavestatus\G"|grep -i "yes"

Warning: Using a password on the commandline interface can be insecure.

            Slave_IO_Running: Yes

           Slave_SQL_Running: Yes

[root@mysql-5 data]#   mysql -uroot -p123456 -e "show slavestatus\G"|grep -i "yes"

Warning: Using a password on the commandline interface can be insecure.

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

在网上查找找到问题的原因了:

解决办法:(网友提供)

statement模式不足以应付应用,换成mixed试试看吧:

在master2__slave1端操作:

mysql> STOP SLAVE;

Query OK, 0 rows affected (0.02 sec)

mysql> SET GLOBAL binlog_format=MIXED;

Query OK, 0 rows affected (0.00 sec)

mysql> START SLAVE;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G

问题解决。提示:此处修改只是临时生效,一旦重启MySQL服务,就会失效,把参数加在my.cnf配置文件从启MySQL服务,永久生效,

Master2__slave1端的参数正确的设置方法:

[root@mysql-5 binlog]# cat -n /etc/my.cnf |sed -n'42,49p'

    42     ###双主实现方法###

    43     log_bin = /home/mysql/data/binlog/mysql-bin

    44     auto_increment_increment        = 2

    45     auto_increment_offset           = 2

    46     log-slave-updates

    47     expire_logs_days = 7

    48     binlog_format = mixed

    49     ###双主实现方法###

在master1端继续插入数据

mysql>  INSERT INTO t1(name) values('pig');

slave2段查看:

[root@mysql-5 binlog]#  mysql -uroot -p123456 -e "select * fromwjw01.t1"

Warning: Using a password on the command lineinterface can be insecure.

+----+---------+

| id | name   |

+----+---------+

|  1 | wangwu  |

|  2 |oldgirl |

|  3 |pig     |

Master2__slave1端查看:

[root@mysql-5 backup]# mysql -uroot -p123456 -e"select * from wjw01.t1"

Warning: Using a password on the command lineinterface can be insecure.

+----+---------+

| id | name   |

+----+---------+

|  1 | wangwu  |

|  2 |oldgirl |

|  3 |pig     |

Master2__slave1端插入数据:

mysql>  INSERT INTO t1(name) values('apple');

在master1段查看:数据同步过去了

[root@mysql-5 data]# mysql -uroot -p123456 -e"select * from wjw01.t1"

Warning: Using a password on the command lineinterface can be insecure.

+----+---------+

| id | name   |

+----+---------+

|  1 | wangwu  |

|  2 |oldgirl |

|  3 |pig     |

|  4 |apple   |

在slave2段查看:数据同步过去了

[root@mysql-5 data]# mysql -uroot -p123456 -e"select * from wjw01.t1"

Warning: Using a password on the command lineinterface can be insecure.

+----+---------+

| id | name   |

+----+---------+

|  1 | wangwu  |

|  2 | oldgirl|

|  3 |pig     |

|  4 |apple   |

实现互为双主搭建MySQL数据库方法就先给大家讲到这里,对于其它相关问题大家想要了解的可以持续关注我们的行业资讯。我们的板块内容每天都会捕捉一些行业新闻及专业知识分享给大家的。

推荐阅读:
  1. 双网卡实现双主备模式
  2. mongodb 主从 双主

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql数据库 双主搭建 搭建mysql数据库

上一篇:python实现列表对应元素求和的两种方法

下一篇:掌握如何使用Veeam Backup & Replicati

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》