mysql主主同步配置

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
1
2
                                                                         MySQL 主主同步配置 
服务器名    IP    系统    MySQL


主机名 地址 系统 MYSQL-SERVER
blog.sjf.com 11.1.0.200 CentOS-6.8 5.1.73-7
image.sjf.com 11.1.0.19 CentOS-6.8 5.1.73-7


一. 假设要同步的库是 test
1
2
3
4
5
6
7
8
9
我们这里先创建同步账号sjf
交叉授权
blog.sjf.com主机上
grant replication   slave on *.* to  'sjf' @ '11.1.0.204'   identified by  'sjf' ;
flush privileges;
 
image.sjf.com主机上
grant replication  slave on *.* to  'sjf' @ '11.1.0.203'    identified by  'sjf' ;
flush privileges;
二. 修改配置文件/etc/my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
blog上设置
[mysqld] 
innodb_file_per_table=ON
skip_name_resolve=ON
user = mysql
log-bin=mysql-bin
server- id  = 1
binlog- do -db= test
binlog-ignore-db=mysql
replicate- do -db= test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
skip-name-resolve
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
image.sjf.com上的配置
[mysqld] 
innodb_file_per_table=ON
skip_name_resolve =ON
user = mysql
log-bin=mysql-bin
server- id  = 2
binlog- do -db= test
binlog-ignore-db=mysql
replicate- do -db= test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
skip-name-resolve
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
三、分别重启服务器blog image 两台主机上的mysql服务

       (1) 分别在服务器blog、image 上查看做为主服务器状态

1
2
3
4
5
6
7
8
9
10
11
blog:
 
MariaDB [(none)]> flush tables with  read  lock;
Query OK, 0 rows affected (0.00 sec)
MariaDB [ test ]> show master status\G; 
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 435
     Binlog_Do_DB:  test
Binlog_Ignore_DB: mysql
1 row  in  set  (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
image:
 
MariaDB [(none)]> flush tables with  read  lock;
Query OK, 0 rows affected (0.00 sec)
MariaDB [ test ]> show master status\G; 
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 340
     Binlog_Do_DB:  test
Binlog_Ignore_DB: mysql
1 row  in  set  (0.00 sec)

     (2)分别在服务器blog、image上用change master语句指定同步位置 :

1
2
3
blog
MariaDB [ test ]> change master to master_host= '11.1.0.19' , master_user= 'sjf' , master_password= 'sjf' ,master_log_file= 'mysql-bin.000001' ,master_log_pos=340;
Query OK, 0 rows affected (0.01 sec)
1
2
image
MariaDB [ test ]> change master to master_host= '11.1.0.200' , master_user= 'sjf' , master_password= 'sjf' ,master_log_file= 'mysql-bin.000001' ,master_log_pos=435;

        注:master_log_file,master_log_pos由上面主服务器查出的状态值中确定 master_log_file对    应  File,master_log_pos对应Position


四、分别先取消读锁,启用从服务器模式
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
blog上:
 
MariaDB [ test ]>  start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [ test ]> show slave status\G;
*************************** 1. row ***************************
                Slave_IO_State: Waiting  for  master to send event
                   Master_Host: 11.1.0.19
                   Master_User: sjf
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000001
           Read_Master_Log_Pos: 340
                Relay_Log_File: mariadb-relay-bin.000002
                 Relay_Log_Pos: 529
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
               Replicate_Do_DB:  test
           Replicate_Ignore_DB: mysql
            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: 340
               Relay_Log_Space: 825
               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: 
   Replicate_Ignore_Server_Ids: 
              Master_Server_Id: 2
1 row  in  set  (0.00 sec)
 
image上:
 
MariaDB [ test ]>  start slave;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [ test ]> show slave status\G;
*************************** 1. row ***************************
                Slave_IO_State: Waiting  for  master to send event
                   Master_Host: 11.1.0.200
                   Master_User: sjf
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000001
           Read_Master_Log_Pos: 435
                Relay_Log_File: mariadb-relay-bin.000002
                 Relay_Log_Pos: 529
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
               Replicate_Do_DB:  test
           Replicate_Ignore_DB: mysql
            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: 435
               Relay_Log_Space: 825
               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: 
   Replicate_Ignore_Server_Ids: 
              Master_Server_Id: 1
1 row  in  set  (0.00 sec)


      主要关注以下 2 个参数:

...
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
...

五、测试

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
blog上:
 
MariaDB [ test ]> use  test ;
Database changed
MariaDB [ test ]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb1            |
| tb3            |
| tb4            |
+----------------+
3 rows  in  set  (0.00 sec)
MariaDB [ test ]> create table tb5( id  int);
Query OK, 0 rows affected (0.02 sec)
MariaDB [ test ]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb1            |
| tb3            |
| tb4            |
| tb5            |
+----------------+
 
images上:
 
MariaDB [(none)]> use  test ;
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
当blog生成表5后
MariaDB [ test ]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb2            |
| tb5            |
+----------------+
2 rows  in  set  (0.00 sec)
我们这里创建表6,看下效果
MariaDB [ test ]> create table tb6( id  int);
Query OK, 0 rows affected (0.01 sec)
blog上。
MariaDB [ test ]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb1            |
| tb3            |
| tb5            |
| tb6            |
+----------------+
4 rows  in  set  (0.00 sec)

从上面的示例可以看出我们这个双主模型就这么成功了,这里的设定主服务器的那个配置是关键,记得每次打开服务两边都要start slave这样才能称为双主模型。当然双主模型也不是没有弊端,下次给大家介绍吧。


 本文转自 sjfbjs 51CTO博客,原文链接:http://blog.51cto.com/11886896/1873997
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
23天前
|
关系型数据库 MySQL
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
20 0
|
30天前
|
存储 SQL 关系型数据库
创建并配置RDS实例
在阿里云上创建RDS实例涉及登录控制台、进入RDS管理页面、创建实例、选择数据库引擎和版本、配置实例规格与存储、设定网络与安全组、设置实例信息、确认订单并支付,最后初始化数据库。操作步骤可能因界面更新或数据库引擎不同略有差异。
18 1
|
1月前
|
关系型数据库 MySQL API
Flink CDC产品常见问题之mysql整库同步到starrock时任务挂掉如何解决
Flink CDC(Change Data Capture)是一个基于Apache Flink的实时数据变更捕获库,用于实现数据库的实时同步和变更流的处理;在本汇总中,我们组织了关于Flink CDC产品在实践中用户经常提出的问题及其解答,目的是辅助用户更好地理解和应用这一技术,优化实时数据处理流程。
|
1月前
|
SQL DataWorks 关系型数据库
DataWorks常见问题之dataworks同步Rds任务失败如何解决
DataWorks是阿里云提供的一站式大数据开发与管理平台,支持数据集成、数据开发、数据治理等功能;在本汇总中,我们梳理了DataWorks产品在使用过程中经常遇到的问题及解答,以助用户在数据处理和分析工作中提高效率,降低难度。
|
10天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
76 0
|
16天前
|
缓存 关系型数据库 MySQL
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
|
26天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
14 1
|
29天前
|
弹性计算 关系型数据库 MySQL
rds子网配置
在阿里云中配置RDS子网涉及五个关键步骤:1) 创建或选择VPC作为私有网络环境;2) 在VPC内创建子网并确保IP地址不重叠;3) 关联路由表和安全组以控制流量及访问权限;4) 创建RDS实例时指定VPC和子网;5) 确保ECS实例与RDS在同一VPC或配置相应跨VPC访问,并调整安全组规则。这样可保障RDS与其他资源的通信及网络性能。
17 6
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
195 0
|
1月前
|
关系型数据库 MySQL API
Flink CDC产品常见问题之mysql整库同步到starrock时任务挂掉如何解决
Flink CDC产品常见问题之mysql整库同步到starrock时任务挂掉如何解决