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 |
转载于:https://blog.51cto.com/wujianwei/1934139