mysql主主互备架构

企业级mysql集群具备高可用,可扩展性,易管理,低成本的特点。mysql主主互备就是企业中常用的一个解决方案。在这种架构中,虽然互为主从,但同一时刻只有一台mysql 可读写,一台mysqk只能进行读操作

 

1.配置

环境: DB1(master)  mysql-5.1.73-3.el6_5.x86_64   192.168.32.130

        DB2 (slave)    mysql-5.1.73-3.el6_5.x86_64    192.168.32.129

mysql vip : 192.168.32.100

安装

yum -y install mysql mysql-devel

/etc/init.d/mysqld start


修改mysql配置文件

DB1配置

[mysqld]

server-id=1

log-bin=mysql-bin

relay-log=mysql-relay-bin

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=test.%

replicate-wild-ignore-table=information_schema.%


DB2配置

[mysqld]

server-id=2

log-bin=mysql-bin

relay-log=mysql-relay-bin

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=test.%

replicate-wild-ignore-table=information_schema.%

 

server-id:节点标识,主从不能相同,必须全局唯一。log-bin是mysql的binlog日志功能,mysql-bin表示日志文件的命名格式。relay-log定义relay-log日志文件的命名格式。replicate-wild-ignore-table是个复制过滤选项,可以过滤不需要复制的数据库或表。

 

慎用binlog-do-db(记录日志的数据库)和binlog-ignore-db(是不要记录日志的数据库名,多个数据库中间用逗号逗号隔开;),replicate-do-db(可以指定只复制哪个库的数据),replicate-ignore-db( 过滤不是基于 查询的字符串的, 而是基于你used的数据库

 

建议用replicate-wild-ignore-table和replicate-wild-do-table

 

为什么 MySQL的 binlog-do-db 选项是危险的-bingqihan-ChinaUnix博客

http://blog.chinaunix.net/uid-24500107-id-2602925.html

 

创建复制用户并授权

在DB1mysql库中创建复制用户

grant replication slave on *.* to 'repl_user'@'192.168.32.129'identified by 'www.123';

show master status;

在DB2mysql库中将DB1设为自己的主服务器

 

change master tomaster_host='192.168.32.130',master_user='repl_user',master_password='www.123',master_log_file='mysql-bin.000002',master_log_pos=342;

 

这边的master_log_file和master_log_pos是DB1上通过show master status查询到的结果

 

启动slave服务

start slave;

mysql> show slave status\G;

*************************** 1. row***************************

              Slave_IO_State: Waiting for master to send event

                 Master_Host: 192.168.32.130

                 Master_User: repl_user

                 Master_Port: 3306

               Connect_Retry: 60

             Master_Log_File: mysql-bin.000002

         Read_Master_Log_Pos: 342

               Relay_Log_File:mysql-relay-bin.000002

               Relay_Log_Pos: 251

       Relay_Master_Log_File: mysql-bin.000002

            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: mysql.%,test.%,information_schema.%

                  Last_Errno: 0

                  Last_Error:

                Skip_Counter: 0

         Exec_Master_Log_Pos: 342

             Relay_Log_Space: 406

             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 in set (0.00 sec)

 

ERROR:

No query specified

 

这里需要重点关注的是Slave_IO_Running和Slave_SQL_Running。这两个就是slave节点运行的主从复制线程。正常情况下这两个值都是yes

 

在BD2mysql库中创建复制用户

 

grant replication slave on *.* to'repl_user'@'192.168.32.130' identified by 'www.123';

show  masterstatus;

 

在DB1中将DB2设为自己的主服务器

 

change master tomaster_host='192.168.32.129',master_user='repl_user',master_password='www.123',master_log_file='mysql-bin.000005',master_log_pos=267;

 

这边的master_log_file和master_log_pos是DB2上通过show master status查询到的结果

 

启动slave服务

start  slave

show slave status\G;

查看slave运行状态

在DB1上创建数据库到DB2上查看,在DB2上创建数据库到DB1上查看,发现都是同步的

 

show variables like 'server%';查看server_id