与服务器一起的日子

  • mysql
  • linux
  • 高可用
  • nginx
与服务器一起的日子
冰冷的机器也熄不灭火热的心
  1. 首页
  2. centos
  3. 正文

MySql高可用——双主架构

2017年12月1日 529点热度 0人点赞 0条评论

对于我们需要高速写入的业务,单个数据库服务器很容易出现单点问题,所以我们为了消除单点问题,引入了双主互备。

其简易架构图

mysql双主简易架构图

演示环境

操作系统:centos7

数据库版本:MySQL5.7.19

master01:192.168.1.21

master02:192.168.1.22

操作过程

配置master01的/etc/my.cnf配置文件

server-id= 21				#数据库ID
log_bin   = /var/log/mysql/mysql-bin	#启用二进制日志 如果没有var/log/mysql这个目录,则需要创建(mkdir).
#binlog-do-db		= tudou1			#需要同步的数据库,这里注释掉,直接同步所有数据库
#binlog-do-db		= tudou2
binlog-ignore-db	= mysql				#不能同步的数据库
log-slave-updates						#把从库的写操作记录到binlog中 (缺少之后,双主创建失败)
expire_logs_days	= 30				        #日志文件过期天数,默认是 0,表示不过期
auto-increment-increment= 2				#设定为主服务器的数量,防止auto_increment字段重复
auto-increment-offset	= 1				#自增长字段的初始值,在多台master环境下,不会出现自增长ID重复

重启master01的mysql服务端

service mysqld restart

配置master02的/etc/my.cnf配置文件

server-id           = 22				#数据库ID
log_bin             = /var/log/mysql/mysql-bin.log	#启用二进制日志 如果没有var/log/mysql这个目录,则需要创建.
#binlog-do-db		= tudou1			#需要同步的数据库,这里注释掉,同步所以数据库
#binlog-do-db		= tudou2
binlog-ignore-db	= mysql				#忽略同步的数据库
log-slave-updates						#把从库的写操作记录到binlog中 (缺少之后,双主创建失败)
expire_logs_days	= 30				#日志文件过期天数,默认是 0,表示不过期
auto-increment-increment= 2				#设定为主服务器的数量,防止auto_increment字段重复
auto-increment-offset	= 1				#自增长字段的初始值,在多台master环境下,不会出现自增长ID重复

重启master02的mysql服务端

service mysqld restart

配置master01向master02的复制

master01的操作

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      107 |              | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

master02的操作

CHANGE MASTER TO MASTER_HOST='192.168.1.21',MASTER_PORT=3306,MASTER_USER='root',MASTER_PASSWORD='root',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=107

这时候会出现提示

Query OK, 0 rows affected, 2 warnings (0.00 sec)

出现这个的时候,我们查询slave状态

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.1.22
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 313
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             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: 154
              Relay_Log_Space: 154
              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: 2003
                Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 33
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             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 more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 171201 20:38:58
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

我们看到Slave_IO_Running是no的状态,这时候我们开启slave

mysql> START SLAVE;

在查询的时候就是yes的状态了。

配置master02向master01的复制

请把上面的步骤反过来就可以了

排错

我在进行的配置的时候,Slave_IO_Running出现了Slave_IO_Running: Connecting的错误。

排错思路如下:

  • 网络不通
  • 密码不对
  • pos不对

顺着上面的思路排错的时候,我发现我ip地址写错了,导致这个错误。

效果演示

我们的双主配置完成了,我们现在开始测试一下,多个数据库能不能同步

如图我们在master01上创建了test数据库的test表,立刻同步到了master02上。

mysql双主创建数据库

然后,我们在master02上写入了数据,同步到了master01上。

mysql双主插入数据

标签: 暂无
最后更新:2017年12月1日

jhin

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2024 与服务器一起的日子. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang