当前位置:网站首页>MYSQL 的 MASTER到MASTER的主主循环同步
MYSQL 的 MASTER到MASTER的主主循环同步
2022-08-08 11:06:00 【51CTO】
MYSQL 的 MASTER到MASTER的主主循环同步
刚刚抽空做了一下MYSQL的主主同步。
把步骤写下来,至于会出现的什么问题,以后随时更新。这里我同步的数据库是TEST
1、环境描述。
主机:192.168.0.231(A)
主机:192.168.0.232(B)
MYSQL 版本为5.1.21
2、授权用户。
A:
mysql> grant replication slave,file on *.* to'repl1'@'192.168.0.232' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> grant replication slave,file on *.* to'repl2'@'192.168.0.231' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后都停止MYSQL服务器。
3、配置文件。
在两个机器上的my.cnf里面都开启二进制日志 。
A:
user = mysql #所属用户
log-bin=mysql-bin #二进制日志
server-id = 1 #主服务器
binlog-format=row
binlog-do-db=dandan #设置哪些数据库写binlog
binlog-ignore-db=mysql #设置哪些数据库不写binlog
replicate-do-db=dandan
replicate-ignore-db=mysql
log-slave-updates #如果一个MASTER挂掉的话,另外一个马上接管
slave-skip-errors=all
sync_binlog=1 #刷新日志
auto_increment_increment=2 #自动增加的字段每次步进是2
auto_increment_offset=1 #自动增加的字段的初始值是1
B:
user = mysql
log-bin=mysql-bin
server-id = 2
binlog-format=row
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
至于这些参数的说明具体看手册。
红色的部分非常重要,如果一个MASTER挂掉的话,另外一个马上接管。
紫红色的部分指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
4、重新启动MYSQL服务器。
在A和B上执行相同的步骤
[[email protected] ~]# /usr/local/mysql/bin/mysqld_safe&
[1] 4264
[[email protected] ~]# 071213 14:53:20 mysqld_safe Logging to'/usr/local/mysql/data/localhost.localdomain.err'.
/usr/local/mysql/bin/mysqld_safe: line 366: [: -eq: unary operator expected
071213 14:53:20 mysqld_safe Starting mysqld daemon with databases from/usr/local/mysql/data
5、进入MYSQL的SHELL。
A:
mysql> flush tables with read lock\G
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File:mysql-bin.000007
Position: 528
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File:mysql-bin.000004
Position: 595
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份自己的数据,保持两个机器的数据一致。
方法很多。完了后看下一步。
6、在各自机器上执行CHANGE MASTER TO命令。
A:
mysql> change master to
-> master_host='192.168.0.232',
-> master_user='repl2',
-> master_password='123456',
->master_log_file='mysql-bin.000004',
-> master_log_pos=595;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
change master to master_host='192.168.1.109',master_user='repl2',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=120;
B:
mysql> change master to
-> master_host='192.168.0.231',
-> master_user='repl1',
-> master_password='123456',
->master_log_file='mysql-bin.000007',
-> master_log_pos=528;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
change master to master_host='192.168.1.108',master_user='repl1',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=120;
7、查看各自机器上的IO进程和SLAVE进程是否都开启。
A:
mysql> show processlist\G
*************************** 1. row ***************************
Id: 2
User: repl
Host: 192.168.0.232:54475
db: NULL
Command: Binlog Dump
Time: 1590
State: Has sent all binlog to slave; waiting for binlogto be updated
Info: NULL
*************************** 2. row ***************************
Id: 3
User: system user
Host:
db: NULL
Command: Connect
Time: 1350
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 4
User: system user
Host:
db: NULL
Command: Connect
Time: 1149
State: Has read all relay log; waiting for the slaveI/O thread to update it
Info: NULL
*************************** 4. row ***************************
Id: 5
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: show processlist
4 rows in set (0.00 sec)
B:
mysql> show processlist\G
*************************** 1. row ***************************
Id: 1
User: system user
Host:
db: NULL
Command: Connect
Time: 2130
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 1223
State: Has read all relay log; waiting for the slaveI/O thread to update it
Info: NULL
*************************** 3. row ***************************
Id: 4
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 4. row ***************************
Id: 5
User: repl2
Host: 192.168.0.231:50718
db: NULL
Command: Binlog Dump
Time: 1398
State: Has sent all binlog to slave; waiting forbinlog to be updated
Info: NULL
4 rows in set (0.00 sec)
如果红色部分没有出现,检查DATA目录下的错误文件。
8、释放掉各自的锁,然后进行插数据测试。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
插入之前两个机器表的对比:
A:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t22 |
+----------------+
B:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t22 |
+----------------+
从A机器上进行插入
A:
mysql> create table t11_replicas
-> (id int not nullauto_increment primary key,
-> str varchar(255) not null)engine myisam;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t11_replicas(str) values
-> ('This is a master to mastertest table');
Query OK, 1 row affected (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t11_replicas |
| t22 |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id |str |
+----+---------------------------------------+
| 1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)
现在来看B机器:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb |
| t11_replicas |
| t22 |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id |str |
+----+---------------------------------------+
| 1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)
现在反过来从B机器上插入数据:
B:
mysql> insert into t11_replicas(str) values('Thisis a test 2');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str |
+----+---------------------------------------+
| 1 | This is a master to master test table |
| 2 | This is a test2 |
+----+---------------------------------------+
2 rows in set (0.00 sec)
我们来看A
A:
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id |str |
+----+---------------------------------------+
| 1 | This is a master to master test table |
| 2 | This is a test2 |
+----+---------------------------------------+
2 rows in set (0.00 sec)
好了。现在两个表互相为MASTER。
边栏推荐
- 五、树结构
- day01 -Web API介绍—DOM 介绍—获取元素—事件基础—操作元素—排他操作—自定义属性操作—节点操作—案例:动态生成表格—创建元素的三种方式(经典面试题)
- ReentrantLock原理,ReentrantLock和synchronized区别
- Thoroughly understand the differences and application scenarios of session, cookie, sessionStorage, and localStorage (interview orientation)
- 文档数据库中的文档可以有相同的数据结构嘛?
- 基于ftp协议的上传与下载
- "Weekly Translate Go" This time we have something different!-- "How to Code in Go" series launched
- day02 -DOM—高级事件(注册事件、事件监听、删除事件、DOM事件流、事件对象、阻止默认行为、阻止事件冒泡、事件委托)—常用鼠标事件—常用的键盘事件
- Mobile adaptation method of vw/vh - vw/vh instance - analog B stand mobile home page - get style tutorial video
- 图数据库是使用什么作为数据模型的呢?
猜你喜欢
随机推荐
使用类似搭积木的低代码开发方式进行 SAP API 开发
详细讲解修改allure报告自定义的logo和名称中文
上海控安SmartRocket系列产品推介(二):SmartRocket Modeler可视化建模开发工具
小程序使用npm包
模式识别 学习笔记:第八章 特征提取
day01 -Web API介绍—DOM 介绍—获取元素—事件基础—操作元素—排他操作—自定义属性操作—节点操作—案例:动态生成表格—创建元素的三种方式(经典面试题)
关于mysql联合索引的最左前缀原则以及b+tree
neural network classification
300万招标!青岛市医疗保障局主机数据库中间件运行维护服务项目
【访谈】Eotalk Vol.01:Eoapi,我们希望以开源的方式构建 API 生态系统
搞清楚系统到底怎样支撑高并发以及架构图的绘制(面试向)
3D激光SLAM:LIO-SAM整体介绍与安装编译
One article to understand configuration management (CM)
Redis 定长队列的探索和实践
八、排序与搜索
E121: Unable to open and write file solution when vim /etc/profile is written
SCCM2012R2管理之版本更新
模式识别 学习笔记:第七章 特征选择
LeetCode_487_最大连续1的个数Ⅱ
典型的图数据库有哪些呀?