美文网首页
MySQL 主主复制

MySQL 主主复制

作者: zhouqz | 来源:发表于2017-09-09 12:34 被阅读0次

环境

服务器

两台 centos6.5 (ip:192.168.1.121、192.168.1.212)

软件

  • mysql5.6
  • haproxy-1.5.14
  • keepalived-1.2.19

步骤

  1. 在两台服务器上分别安装mysql环境(完全相同的安装即可)
  1. 配置mysql主主复制(参考 http://www.cnblogs.com/phpstudy2015-6/p/6485819.html#_label7
    何为主主复制:就是两个mysql都能读能写,数据记录通过二进制传达给对方从而保持数据的一致性。
    实现:
    192.168.1.121(主)+192.168.1.212(从) 主从复制 +
    192.168.1.212(主)+192.168.1.121(从) 主从复制 =
    192.168.1.121+192.168.1.212 主主复制。
    2.1. 修改mysql配置文件
shell> sudo vim /etc/my.cnf

192.168.1.121 mysql配置文件内容:

[mysqld]
server-id=1   #任意自然数n,只要保证两台MySQL主机不重复就可以了。
log-bin=mysql-bin   #开启二进制日志
auto_increment_increment=2   #步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_offset=1   #起始值。一般填第n台主MySQL。此时为第一台主MySQL
binlog-ignore=mysql   #忽略mysql库【我一般都不写】
binlog-ignore=information_schema   #忽略information_schema库【我一般都不写】
#replicate-do-db=aa   #要同步的数据库,默认所有库

192.168.1.212 mysql配置文件内容:

[mysqld]
#mysql负载均衡配置
server-id=2
log-bin=mysql-bin
auto_increment_increment=2 #步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_offset=2 #起始值。一般填第n台主MySQL。此时为第二台主MySQL
#replicate-do-db=aa #要同步的数据库,默认所有库

两台服务器的配置文件修改好后均重启mysql服务。
2.2. 配置192.168.1.121(主)+192.168.1.212(从) 主从复制
使用root用户登录192.168.1.121主机的mysql,创建一个可以从192.168.1.212登录的mysql用户并授权。

mysql> create user 'mysql212'@'192.168.1.212' identified by 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘mysql212’@’192.168.1.121’ IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;

查看192.168.1.121 mysql服务器二进制日志

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000076
         Position: 1173222
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

告知从服务器主服务器的二进制文件名和位置
使用root用户登录192.168.1.212主机的mysql,在192.168.1.121中执行:

mysql> change master to 
    -> master_host='192.168.1.121',
    -> master_user='mysql212',
    -> master_password='password',
    -> master_log_file='mysql-bin.000076',
    -> master_log_pos=1173222;

192.168.1.212服务器开启复制:

mysql> start slave;

查看主从复制是否配置成功

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.121
                  Master_User: mysql212
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000076
          Read_Master_Log_Pos: 1173222
               Relay_Log_File: bogon-relay-bin.000006
                Relay_Log_Pos: 1173385
        Relay_Master_Log_File: mysql-bin.000076
             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: 1173222
              Relay_Log_Space: 1173721
              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
                  Master_UUID: e69cc9bd-109e-11e5-b9e7-00e066233823
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

当看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明状态正常。

2.3. 配置192.168.1.212(主)+192.168.1.121(从) 主从复制
步骤同2.2。

2.4. 测试主主复制配置是否成功。

相关文章

  • mysql 主主复制

    1.环境准备: 主机IP:192.168.1.4, 192.168.1.5 操作系统:centos 7.3 ...

  • MySQL 主主复制

    环境 服务器 两台 centos6.5 (ip:192.168.1.121、192.168.1.212) 软件 ...

  • 2019-10-12

    利用LVS+Keepalived搭建Mysql双主复制高可用负载均衡环境 应用背景: MySQL复制(主主,主从....

  • MySQL应用

    MySQL Replication: 1、主/从架构: 2、主主复制: 3、半同步复制 4、复制过滤器: 5、复制...

  • 23-MYSQL数据库(三)

    MySQL复制 一主一从 一主多从 主从复制原理 MySQL垂直分区 MySQL水平分片(Sharding) 对应...

  • MySQL主从复制 - 入门

    目录 主MySQL安装MySQL配置MySQL账号和状态 从MySQL安装MySQL配置MySQL主从复制 测试主...

  • MySQL主主复制搭建

    一.工作环境及条件 主数据库1(master1):172.25.0.254 主数据库2(master2):172....

  • MySql配置主主复制

    服务器A:192.168.99.24服务器B:10.255.255.24 服务器A mysql配置 服务器B my...

  • mysql主主复制原理及配置方法

    Mysql主主复制原理 主主复制的原理实际上是主从复制的原理,让两台服务器互为主从,就实现了主主复制。以下介绍主从...

  • MMM架构(Multi-Master Replication M

    MMM(pertl语言) 监控了Mysql主从复制健康情况主动主动模式的主主复制主动被动模式的主主复制 开启mys...

网友评论

      本文标题:MySQL 主主复制

      本文链接:https://www.haomeiwen.com/subject/ovmojxtx.html