OS:RedHat 6.5_X64
MASTER_A:10.211.55.13
MASTER_B:10.211.55.14
VIP:10.211.55.201
MASTER_A:
mysql> grant replication slave,file on *.* to 'repl1'@'10.211.55.14' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MASTER_B:
mysql> grant replication slave,file on *.* to 'repl2'@'10.211.55.13' identified
by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
停止MySQL服务
service mysqld stop
修改MASTER_A配置文件
[root@master ~]# vi /etc/my.cnf #开启二进制日志,设置id
[mysqld]
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
修改MASTER_B配置文件
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
至于这些参数的说明具体看手册。
log-slave-updates
非常重要,如果一个MASTER 挂掉的话,另外一个马上接管。
sync_binlog
auto_increment_increment
auto_increment_offset
指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
启动MySQL服务
service mysqld start
MASTER_A:
mysql> flush tables with read lock\G
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 528
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
MASTER_B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 595
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份数据,保持两个机器的数据一致。
MASTER_A:
mysql> change master to
-> master_host='10.211.55.14',
-> master_user='repl2',
-> master_password='123456',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=595;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
MASTER_B:
mysql> change master to
-> master_host='10.211.55.13',
-> master_user='repl1',
-> master_password='123456',
-> master_log_file='mysql-bin.000007',
-> master_log_pos=528;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
Slave_IO
和Slave_SQL
是YES
说明主主同步成功
自行测试 增删改
等操作
创建停止keepalived脚本
mkdir touch /usr/local/keepalived/scripts/
touch /usr/local/keepalived/scripts/check_mysql.sh
vi /usr/local/keepalived/scripts/check_mysql.sh
添加以下内容
#!/bin/bash
service keepalived stop
添加执行权限
chmod a+x /usr/local/keepalived/scripts/check_mysql.sh
编辑MASTER_A的keepalived.conf配置文件内容如下:
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP #两台都设置BACKUP
interface eth0 #监听的网卡
virtual_router_id 51 #主备相同
priority 100 #优先级,MASTER_B设置90
advert_int 1
nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.211.55.201
}
}
virtual_server 10.211.55.201 13306 {
delay_loop 2
#lb_algo rr #LVS算法,用不到,我们就关闭了
#lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 50 #同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP
real_server 10.211.55.13 13306 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /usr/local/keepalived/scripts/check_mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔时间
}
}
编辑MASTER_B的keepalived.conf配置文件内容如下:
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP #两台都设置BACKUP
interface eth0 #监听的网卡
virtual_router_id 51 #主备相同
priority 90 #优先级,backup设置90
advert_int 1
#nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.211.55.201
}
}
virtual_server 10.211.55.201 13306 {
delay_loop 2
#lb_algo rr #LVS算法,用不到,我们就关闭了
#lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 50 #同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP
real_server 10.211.55.14 13306 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /usr/local/keepalived/scripts/check_mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔时间
}
}
配置完成后就可以运行看下效果了,分别在主从服务器上启动mysql和keepalived
service mysqld start
service keepalived start
启动之后通过 ip a
命令查看主服务器的网络信息,可以看到在eth0网卡下生成了10.211.55.201这个虚拟ip,并可通过这个ip访问到MySQL
参考资料:
http://lizhenliang.blog.51cto.com/7876557/1362313
http://www.jb51.net/article/38657.htm