环境:

    ubuntu18.04.2

    mysql5.7.21

    

---------------master1服务器操作记录---------------
在my.cnf文件的[mysqld]配置区域添加下面内容:
[root@master1 ~]# vim /etc/my.cnf
server-id = 1
log-bin = mysql-bin
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 1
slave-skip-errors = all [root@master1 ~]# cd /usr/local/mysql/support-files
[root@master1 ~]#./mysql.server restart
Shutting down MySQL. SUCCESS!
Starting MySQL.. SUCCESS! 数据同步授权(iptables防火墙开启3306端口)这样I/O线程就可以以这个用户的身份连接到主服务器,并且读取它的二进制日志。
mysql>grant all privileges on *.* to root@'%' identified by "123456";
#grant replication slave,replication client on *.* to root@'192.168.85.%' identified by "123456";
Query OK, 0 rows affected (0.00 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) 最好将库锁住,仅仅允许读,以保证数据一致性;待主主同步环境部署后再解锁;
锁住后,就不能往表里写数据,但是重启mysql服务后就会自动解锁!
mysql> flush tables with read lock; //注意该参数设置后,如果自己同步对方数据,同步前一定要记得先解锁!
Query OK, 0 rows affected (0.00 sec) 查看下log bin日志和pos值位置
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000004 | 430 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec) ---------------master2服务器操作记录---------------
在my.cnf文件的[mysqld]配置区域添加下面内容:
[root@master2 ~]# vim /etc/my.cnf
server-id = 2
log-bin = mysql-bin
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 2
slave-skip-errors = all [root@master1 ~]# cd /usr/local/mysql/support-files
[root@master1 ~]#./mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS! mysql> grant replication slave,replication client on *.* to root@'192.168.85.%' identified by "123465";
Query OK, 0 rows affected (0.00 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec) mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000003 | 430 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec) ---------------master1服务器做同步操作---------------
mysql> unlock tables; //先解锁,将对方数据同步到自己的数据库中
mysql> slave stop;
mysql> change master to master_host='192.168.85.141',master_user='root',master_password='12346',master_log_file='mysql-bin.000003',master_log_pos=430;
Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave;
Query OK, 0 rows affected (0.01 sec) 查看同步状态,如下出现两个“Yes”,表明同步成功!
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 182.148.15.237
Master_User: wang
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 430
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 279
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
.........................
Seconds_Behind_Master: 0
......................... 这样,master1就和master2实现了主从同步,即master1同步master2的数据。 ---------------master2服务器做同步操作---------------
mysql> unlock tables; //先解锁,将对方数据同步到自己的数据库中
mysql> slave stop;
mysql> change master to master_host='192.168.85.140',master_user='root',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=430;
Query OK, 0 rows affected, 2 warnings (0.06 sec) mysql> start slave;
Query OK, 0 rows affected (0.01 sec) mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 182.148.15.238
Master_User: wang
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 430
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 279
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
........................
Seconds_Behind_Master: 0
........................ 这样,master2就和master1实现了主从同步,即master2也同步master1的数据。 以上表明双方已经实现了mysql主主同步。
当运行一段时间后,要是发现同步有问题,比如只能单向同步,双向同步失效。可以重新执行下上面的change master同步操作,只不过这样同步后,只能同步在此之后的更新数据。下面开始进行数据验证: -----------------主主同步效果验证---------------------
1)在master1数据库上写入新数据
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec) mysql> create database huanqiu;
Query OK, 1 row affected (0.01 sec) mysql> use huanqiu;
Database changed mysql> create table if not exists haha (
-> id int(10) PRIMARY KEY AUTO_INCREMENT,
-> name varchar(50) NOT NULL);
Query OK, 0 rows affected (0.04 sec) mysql> insert into haha values(1,"王士博");
Query OK, 1 row affected (0.00 sec) mysql> insert into haha values(2,"郭慧慧");
Query OK, 1 row affected (0.00 sec) mysql> select * from haha;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 王士博 |
| 2 | 郭慧慧 |
+----+-----------+
2 rows in set (0.00 sec) 然后在master2数据库上查看,发现数据已经同步过来了!
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| huanqiu |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec) mysql> use huanqiu;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show tables;
+-------------------+
| Tables_in_huanqiu |
+-------------------+
| haha |
+-------------------+
1 row in set (0.00 sec) mysql> select * from haha;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 王士博 |
| 2 | 郭慧慧 |
+----+-----------+
2 rows in set (0.00 sec) 2)在master2数据库上写入新数据
mysql> create database hehe;
Query OK, 1 row affected (0.00 sec) mysql> insert into huanqiu.haha values(3,"周正"),(4,"李敏");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0 然后在master1数据库上查看,发现数据也已经同步过来了!
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hehe |
| huanqiu |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec) mysql> select * from huanqiu.haha;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 王士博 |
| 2 | 郭慧慧 |
| 3 | 周正 |
| 4 | 李敏 |
+----+-----------+
4 rows in set (0.00 sec) 至此,Mysql主主同步环境已经实现。

---恢复内容结束---

mysql互为主从(双主)配置的更多相关文章

  1. Keepalived与MySQL互为主从自动切换配置

    为解决Mysql数据库单点问题,实现两台MySQL数据库互为主备,双向replication.当一Master出现问题,则将Slave切换为Master继续工作. 环境说明 系统版本:CentOS L ...

  2. MySql互为主从配置文件及配置方法

    # Example MySQL config file for medium systems. # # This is for a system with little memory (32M - 6 ...

  3. MySQL Replication, 主从和双主配置

    MySQL Replication, 主从和双主配置 MySQL的Replication是一种多个MySQL的数据库做主从同步的方案,特点是异步,广泛用在各种对MySQL有更高性能,更高可靠性要求的场 ...

  4. mysql主从之双主配置

    mysql双主配置 mysql双主其实就是互相同步,互为主从 任意一台都能够执行插入动作 生产环境用得非常少,因为还是担心数据一致的问题 生产环境一般来说主从已经够用 172.19.132.121的配 ...

  5. MYSQL 双主配置

    MYSQL1. 版本号:5.7.243. 部署方式:双主部署,两台机器即是主又是备 ,双向拷贝,可以同时写入.4. 安装部署路径: a) /home/softb) 配置路径 /etc/my.cnfc) ...

  6. MySQL双主配置

    MySQL双主配置 准备环境:服务器操作系统为RHEL6.4 x86_64,为最小化安装.主机A和主机B均关闭防火墙和SELINUX ,IP地址分别为192.168.131.129和192.168.1 ...

  7. keepalived主从及双主配置

    高可用有2中方式. 1.Nginx+keepalived 主从配置 这种方案,使用一个vip地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备份机器在主机器不出现故障的时候, ...

  8. MySQL的双主配置

    配置MySQL双主配置,需要先配置MySQL的主从复制,传送门: 0.集群规划 hadoop105 hadoop106 hadoop107 MySQL(master,slave) MySQL(slav ...

  9. centos 下 mysql+keepalived实现双主自由切换

    目录 ip规划 mysql双主配置 keepalived配置 mysql1中keepalived的配置 mysql2中keepalived的配置 VIP漂移检测 本文的目的是搭建一个互为主从的mysq ...

随机推荐

  1. 数据库常用SQL语句(三):子查询

    一.为什么会使用子查询 虽然可以通过连接查询来实现多表查询数据记录,但不建议使用,因为连接查询的性能很差,为什么呢?我们来进行分析,例如 我们要查询部门表t_dept 和雇员表t_employee中的 ...

  2. 搭建自己的技术博客系列(五)hexo博客接入busuanzi插件,展示访问量和网站运行时间

    busuanzi计数脚本 busuanzi官方指引 一.安装脚本(必选) 要使用不蒜子必须在页面中引入busuanzi.js,目前最新版如下. 不蒜子可以给任何类型的个人站点使用,如果你是用的hexo ...

  3. 小程序组件 scroll-view 横向滚动条无效

    小程序组件 scroll-view 中分别有上下竖向滑动和左右横向滑动,在这次项目中刚好需要用到横向滑动,但在测试过程中发现横向滑动没有了效果(静止在那里没移动过,并且换行了),经调试发现: 1.sc ...

  4. 人体行为识别(骨架提取),搭建openpose环境,VS2019(python3.7)+openpose

    这几天开始接触人体行为识别,经过多方对比后,选择了现在最热的人体骨架提取开源库,openpose. 下面就不多说了,直接开始openpose在win10下的配置: 需求如下:1. VS2019    ...

  5. Thinkphp6框架学习:有关数据库的基本操作

    最近Thinkphp6框架出来了,Mysql 8.0也出来了,php版本也升级到了7.4(这里php使用的是php7.3) 为了赶上时代的潮流,连ide(phpstorm)也升级到了2019.2的版本 ...

  6. Google 官方 侧滑 drawerlayout

    一.概述 目前侧滑框架已经很多了,但是我常用的也就那么2个 ,slidingmenu 和sidemenu-android, 但是项目要求使用官方的,所以就看了一下drawerlayout 二.代码 官 ...

  7. 1、单链表的实现(java代码)

    1.创建链结构实体Node /** * 链表结构实体类 */ public class Node { Node next = null; //下一节点 int data; //节点数据 public ...

  8. 树、图、堆、STL(来自菜鸡的"炒鸡"干粮)

    树.图.堆.STL 图论基础 简单图: 没有自环,两个顶点之间最多只有一条边. 完全图: 一个简单图,每两个顶点之间都有一条边.一共有(n-1)*n/2条边. 二分图: 一个简单图,设G=(V,E)是 ...

  9. TestNG(三) 基本注解BeforeMethod和AfterMethod

    package com.course.testng; import org.testng.annotations.*; public class BasicAnnotation { @Test //最 ...

  10. (五)Linux内存管理zone_sizes_init

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...