mysql 主从同步,双主同步,如果服务器意外挂机,不同步怎么办

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS Agent(兼容OpenClaw),2核4GB
RDS Agent Manager,2核4GB
简介:

mysql 主从同步,双主同步,如果服务器意外挂机,不同步怎么办

首先主从同步

master 192.168.0.21

slave 192.168.0.22

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#my.cnf master 配置文件
[client]
port            = 3306
socket          =  /tmp/mysql .sock
default-character- set =utf8
[mysqld]
port            = 3306
socket          =  /tmp/mysql .sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
default-character- set =utf8
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=mysql-bin
binlog_format=mixed
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
default-character- set =utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
server- id = 1   #增加3条配置
binlog- do -db=abc   #设置同步数据库,如果有多个数据库,每个数据库一行
binlog-ignore-db = mysql  #设置不要同步的数据库,如有多个数据库,每个数据库一行
log-bin  #日志文件

service mysql restart #重启数据库

1
2
3
4
5
6
7
8
9
进入mysql,创建一个数据库abc:
create database abc;
创建一个用来同步的用户,指定只能在192.168.0.22登录:
grant replication slave on *.* to  'root' @ '192.168.0.22' identified by  '123456' ;
mysql> use abc
Database changed
mysql> create table bb(idint, name varchar(20),age int);
Query OK, 0 rows affected (0.05 sec)
#注意表和数据库格式必须一样
1
2
3
4
5
6
7
#slave
mysql>  create database abc;
Query OK, 1 row affected (0.00 sec)
mysql> use abc
Database changed
mysql> create table bb(idint, name varchar(20),age int);
Query OK, 0 rows affected (0.01 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#slave
#my.cnf 配置
[client]
port            = 3306
socket          =  /tmp/mysql .sock
default-character- set =utf8
[mysqld]
port            = 3306
socket          =  /tmp/mysql .sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
default-character- set =utf8
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=mysql-bin
binlog_format=mixed
server- id = 2    #id
master-host=192.168.0.21   #mysql主ip地址
master-user=root    #授权用户
master-password=123456   #授权密码
master-port=3306     #mysql端口
master-connect-retry=30    #同步
replicate-ignore-db=mysql    #设置不要接收的数据库,如有多个数据库,每个数据库一行
replicate- do -db=abc   #设置要接收的数据库,如有多个数据库,每个数据库一行
log-bin
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
default-character- set =utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#主上查看
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000025 |      377 | abc          |                  |
+------------------+----------+--------------+------------------+
1 row inset(0.00 sec)
# 记录下file和position的值
#slave 上
CHANGE MASTER TO MASTER_LOG_FILE= ' mysql-bin.000025' ,MASTER_LOG_POS=377;
# 根据master状态同步
mysql> slave start;
# 启动slave
mysql> show slave status\G;

发现报错信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.0.21
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:  mysql-bin.000025
Read_Master_Log_Pos: 377
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:  mysql-bin.000025
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 377
Relay_Log_Space: 106
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2013
Last_IO_Error: error connecting to master  'root@192.168.0.21:330                                                                                                                                                             6' - retry- time : 60  retries: 86400
Last_SQL_Errno: 0
Last_SQL_Error:
1 row inset(0.00 sec)
ERROR:
No query specified


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
或者 vi /etc/sysconfig/iptables
-I INPUT -p tcp --dport 3360 -j DROP
-I INPUT -s 119.134.251.49 /32-p  tcp --dport 3306 -j ACCEPT
前面那个问题可能是防火墙问题,这次这个问题,是我以前以前同步过现在在做同步就报错了,给出解决方案。
[root@localhost ~] # service iptables stop
iptables:清除防火墙规则:                                 [确定]
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:正在卸载模块:                                   [确定]
[root@localhost ~] # mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection idis 7
Server version: 5.1.60-log Source distribution
Copyright (c) 2000, 2013, Oracle and /orits  affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and /orits
affiliates. Other names may be trademarks of their respective
owners.
Type  'help;' or  '\h' forhelp. Type  '\c' to clearthe current input statement.
mysql> slave stop;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_LOG_FILE= ' mysql-bin.000025' ,MASTER_LOG_POS=377;
Query OK, 0 rows affected (0.05 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.21
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:  mysql-bin.000025
Read_Master_Log_Pos: 377
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:  mysql-bin.000025
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 377
Relay_Log_Space: 106
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log:  'Could not find first log file name in binary log index file'
Last_SQL_Errno: 0
Last_SQL_Error:
1 row inset(0.00 sec)
ERROR:
No query specified


1
2
3
4
5
6
7
8
#第三个问题
[root@ceshi ~] # mysqlbinlog --start-position=377  /home/data/websrv/mysql/var/mysql-bin.000025
mysqlbinlog: unknown variable  'default-character-set=utf8'
解决:
[client]
port            = 3306
socket          =  /tmp/mysql .sock
#default-character-set=utf8


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@ceshi ~] # mysqlbinlog --start-position=377  /home/data/websrv/mysql/var/mysql-bin.000025
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#131115 22:52:33 server id 1  end_log_pos 106   Start: binlog v 4, server v 5.1.60-log created 131115 22:52:33 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
MTWGUg8BAAAAZgAAAGoAAAABAAQANS4xLjYwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAxNYZSEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
'/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;


看到上面 发现at4

然后我们这样配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# at 4
mysql> slave stop;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST= '192.168.0.21' ,MASTER_PORT=3306,MASTER_USER= 'root' , MASTER_PASSWORD= '123456' ,master_log_file= 'mysql-bin.000025' ,master_log_pos=4;
Query OK, 0 rows affected (0.03 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.0.21
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000025
Read_Master_Log_Pos: 377
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 522
Relay_Master_Log_File: mysql-bin.000025
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 377
Relay_Log_Space: 681
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row inset(0.00 sec)
ERROR:
No query specified
好了解决了 哈哈!这时我们导入数据测试!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
这是主数据
mysql> use abc;
Database changed
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| bb            |
+---------------+
1 row inset(0.00 sec)
mysql> insert into bb VALUES(1,2,3);
Query OK, 1 row affected (0.00 sec)
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| bb            |
+---------------+
1 row inset(0.00 sec)
mysql>  select * from bb;
+------+------+------+
id | name | age  |
+------+------+------+
|    1 | 2    |    3 |
+------+------+------+
1 row inset(0.00 sec)
mysql> insert into bb VALUES(3,4,5);
Query OK, 1 row affected (0.00 sec)
mysql>  select * from bb;
+------+------+------+
id | name | age  |
+------+------+------+
|    1 | 2    |    3 |
|    3 | 4    |    5 |
+------+------+------+
2 rows inset(0.00 sec)
mysql>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
slave数据:
mysql> use abc;
Database changed
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| bb            |
+---------------+
1 row inset(0.00 sec)
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| bb            |
+---------------+
1 row inset(0.00 sec)
mysql>  select * from bb;
+------+------+------+
id | name | age  |
+------+------+------+
|    1 | 2    |    3 |
+------+------+------+
1 row inset(0.00 sec)
mysql>  select * from bb;
+------+------+------+
id | name | age  |
+------+------+------+
|    1 | 2    |    3 |
|    3 | 4    |    5 |
+------+------+------+
2 rows inset(0.01 sec)


1
2
3
4
5
6
7
8
主清除数据
mysql> delete from bb;
Query OK, 2 rows affected (0.00 sec)
mysql>  select * from bb;
Empty  set (0.00 sec)
slave上
mysql>  select * from bb;
Empty  set (0.00 sec)

154257949.jpg

154301920.jpg

154304682.jpg

154306971.jpg


双主其实更简单

先授权

1
2
3
4
5
6
7
8
9
10
主:192.168.0.21
create database abc;
create table bb(idint, name varchar(20),age int);
grant replication slave on *.* to  'root' @ '192.168.0.22' identified by  '123456' ;
flush privileges;
主:192.168.0.22
create database abc;
create table bb(idint, name varchar(20),age int);
grant replication slave on *.* to  'root' @ '192.168.0.21' identified by  '123456' ;
flush privileges;

配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
主:192.168.0.21
server- id = 1
binlog- do -db=abc
binlog-ignore-db = mysql
master-host=192.168.0.22
master-user=root
master-password=123456
master-port=3306
master-connect-retry=60
log-bin
主:192.168.0.22
server- id = 2
binlog-ignore-db = mysql
master-host=192.168.0.21
master-user=root
master-password=123456
master-port=3306
master-connect-retry=60
replicate- do -db=abc
binlog- do -db=abc
log-bin

然后重启

因为我的真实环境是主从,这里只测试了主从,双同步一样的道理,只是把主从反过来而已。

1
2
3
4
5
6
7
8
9
10
11
12
13
#添加开机启动项 然后开机也同步测试
[root@localhost ~] # vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
modprobe ip_conntrack
/usr/local/nagios/bin/nrpe-c  /usr/local/nagios/etc/nrpe .cfg -d
service php-fpm start
/usr/bin/memcachedstart
service mysql restart

注意开机后防火墙端口一定要允许访问3306 否则则不同步!!!!!

160459108.jpg


本文转自 cs312779641 51CTO博客,原文链接:http://blog.51cto.com/chenhao6/1325247


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
9月前
|
存储 缓存 监控
MySQL服务器配置优化:my.cnf参数调优指南
本文深入解析了MySQL核心配置参数及性能优化技巧,涵盖内存结构、调优原则、存储引擎优化、查询性能优化等内容,通过实战案例帮助读者构建高性能MySQL服务器配置,解决常见的性能瓶颈问题。
1322 0
|
关系型数据库 MySQL Linux
在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾
以上就是在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾的步骤。这个过程就像是一场接力赛,数据从MySQL数据库中接力棒一样传递到备份文件,再从备份文件传递到其他服务器,最后再传递回MySQL数据库。这样,即使在灾难发生时,我们也可以快速恢复数据,保证业务的正常运行。
603 28
|
安全 关系型数据库 MySQL
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
|
存储 SQL 关系型数据库
服务器数据恢复—云服务器上mysql数据库数据恢复案例
某ECS网站服务器,linux操作系统+mysql数据库。mysql数据库采用innodb作为默认存储引擎。 在执行数据库版本更新测试时,操作人员误误将在本来应该在测试库执行的sql脚本在生产库上执行,导致生产库上部分表被truncate,还有部分表中少量数据被delete。
377 25
|
安全 关系型数据库 MySQL
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
|
SQL 存储 关系型数据库
MySQL/SqlServer跨服务器增删改查(CRUD)的一种方法
通过上述方法,MySQL和SQL Server均能够实现跨服务器的增删改查操作。MySQL通过联邦存储引擎提供了直接的跨服务器表访问,而SQL Server通过链接服务器和分布式查询实现了灵活的跨服务器数据操作。这些技术为分布式数据库管理提供了强大的支持,能够满足复杂的数据操作需求。
705 12
|
监控 关系型数据库 MySQL
如何解决 MySQL 数据库服务器 CPU 飙升的情况
大家好,我是 V 哥。当 MySQL 数据库服务器 CPU 飙升时,如何快速定位和解决问题至关重要。本文整理了一套实用的排查和优化套路,包括使用系统监控工具、分析慢查询日志、优化 SQL 查询、调整 MySQL 配置参数、优化数据库架构及检查硬件资源等步骤。通过一个电商业务系统的案例,详细展示了从问题发现到解决的全过程,帮助你有效降低 CPU 使用率,提升系统性能。关注 V 哥,掌握更多技术干货。
2097 0
|
9月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
585 158
|
9月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
9月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1528 152

热门文章

最新文章

推荐镜像

更多