`
chaijuntao
  • 浏览: 22883 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Mysql双主从同步部署(一)

 
阅读更多

最近因为一次偶然的数据库磁盘坏道故障,所以决定进行mysql同步的HA方案升级,趁着这个机会又重新审视了一把mysql replication的过程。

 

整个方案是基于四个mysql实例,实现两两主从,同时两台master之间进行主主同步,此方案的优势是任何一台master或者slave故障时,都可以切换到另一台master或slave进行服务,当任一台故障实例恢复后,都可以自动恢复和同步数据,整个故障和修复过程的数据同步问题,无需进行人工干预。简单的拓扑关系如下:

 

首先,准备四台虚拟机,安装上版本相同的mysql,保证slave版本大于等于master,为确保兼容性问题,最好是版本相同(至少保证版本号的前两位相同)

资源清单:

Master A : 192.168.4.85  , server_id  1

Slave A : 192.168.4.86  , server_id  2

Master B : 192.168.4.87  , server_id  3

Slave B : 192.168.4.88  , server_id  4

 

Mysql版本号:5.5.43

 

同步库名:test1

 

OK,从拓扑图中可以看出,我们的方案要做的其实是两件事,两个主从同步和一个主主同步。

先来看下主从同步的配置:

主库配置:

 1、修改Master A的配置文件my.conf:

server-id               = 1
log_bin                 = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
binlog_do_db            = test1

 2、添加具有同步权限的账户

GRANT REPLICATION SLAVE ON *.* TO 'user'@'%' IDENTIFIED BY 'password';

3、重启mysql:

service mysql restart

4、显示master日志位置:

mysql >> show master status;

+------------------+-----------+--------------+------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000003 | 107 |              |                  |
+------------------+-----------+--------------+------------------+

 

从库配置

1、修改Slave A的配置文件my.conf:

server-id               = 2
expire_logs_days        = 10
max_binlog_size         = 100M
replicate_do_db         = test1

 2、重启mysql:

service mysql restart

3、设置主服务器信息

(如果主库有存量数据需要同步,则需要先进行dump操作后再进行从库的同步,此处不做详细介绍)
change master to master_host='192.169.4.85',                              
                 master_user='user',                                    
                 master_password=password,
master_log_file='mysql-bin.000003',                         
                             master_log_pos=107;

4、启动同步

mysql >>slave start;

5、显示slave状态,出现以下信息表示同步开启成功,主要看

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

 

 

Mysql >> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 1.1.1.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 107
Relay_Log_File: relay-relay-bin.000012
Relay_Log_Pos: 1148354
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table: gslb.server_threshold,gslb.server,gslb.threshold,gslb.vip,gslb.status,gslb.vip_threshold
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 270500590
Relay_Log_Space: 1148510
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: 10201
1 row in set (0.00 sec)

接下去,在master的test1库中进行建表、插数据等操作后,到slave中能看到同步后的操作结果。

 

 Master B -- Slave B 的主从同步配置同上,至此两个主从同步均已部署完成,接下去,我们就要进行Master A -- Master B的主主同步配置工作,见下节

 

  • 大小: 31.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics