MySQL之Master-Master配置 2013-01-08 14:11:57
分类: Mysql/postgreSQL
近期在测试MySQL双主多个slave架构,利用keepavlied实现自动切换Master。主要工作有:
1、MySQL Mater-Maste工作(前期)
2、Keepavlied搭建,还可以结合MySQL Proxy实现读写分离
3、处理Master-Mater-Slave之间的同步关系
首先,先搭建Mater-Master的环境。
一、安装环境:
系统版本:RHEL5.4.x86_64
MySQL版本:mysql-5.0.45
Mysqlserver_1: 192.168.15.178
Mysqlserver_2: 192.168.15.185
二、正式安装:
在两台服务器中分别安装Mysql(略过)。
配置MySQL配置文件:
1)192.168.15.178的配置文件,如下红色标注是必须配置的,其他参数可以自行配置。
2)192.168.15.185的配置文件,如下红色标注是必须配置的,其他参数可以自行配置。
如上设置bin-log文件,并都设置对方为自己的主服务器,配置同步的数据库为map
三、配置MySQL:
导入要同步的数据库,先以一台作为主服务器,同步数据。我以phpcmsv9为例。
1) 在两台mysql数据库服务器里面设置权限,分别执行如下命令:
grant replication slave on *.* to 'backup'@'192.168.15.%' identified by '123456';
在192.168.15.178查看master运行状态,获取binlog日志信息:
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000052 | 151744220 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)
2)在192.168.15.185上将192.168.15.178设为自己的主服务器执行如下命令:
change master to master_host='192.168.15.178',master_user='backup',master_password='123456',master_log_file='mysql-bin.000056',master_log_pos=151744220;
然后启动start slave;
注意这里写的bin-log参数是在178查看到的,即是对方的参数。
3) 然后在192.168.15.78执行:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 98 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
4)在192.168.15.78上将192.168.15.185设为自己的主服务器执行如下命令:
change master to master_host='192.168.15.185',master_user='backup',master_password='123456',master_log_file='mysql-bin.000005',master_log_pos=98;
然后启动start slave;
注意这里写的bin-log参数是在185查看到的,即是对方的参数。
5)MySQL同步测试配置完毕,我们会发现在任何一台mysql上更新同步的数据库里面的数据,都会同步到另一台mysql。
6)测试:
192.168.15.185建表:
mysql> create table UserState(UserStateId int auto_increment not null primary key, UseStateName varchar(50) not null);
Query OK, 0 rows affected (0.01 sec)
mysql> desc UserState;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| UserStateId | int(11) | NO | PRI | NULL | auto_increment |
| UseStateName | varchar(50) | NO | | | |
+--------------+-------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
192.168.15.178查表:
mysql> desc UserState;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| UserStateId | int(11) | NO | PRI | NULL | auto_increment |
| UseStateName | varchar(50) | NO | | | |
+--------------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
ok
分类: Mysql/postgreSQL
承上一篇文章:“MySQL之Master-Master配置 ”已经实现了MySQL双主模式,即互相备份。接下来我们用Keepavlied来实现单点故障切换。
一、配置虚拟IP-VIP
首先我们先配置VIP节点IP地址,把每台物理地址执行到虚拟IP。
这里我们使用的虚拟IP-VIP为: 192.168.15.181
#vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
IPADDR=192.168.15.181
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:d8:b3:ca brd ff:ff:ff:ff:ff:ff
inet 192.168.15.178/24 brd 192.168.15.255 scope global eth0
inet 192.168.15.181/32 scope global eth0
inet 192.168.15.181/24 brd 192.168.15.255 scope global secondary eth0:0
inet6 fe80::20c:29ff:fed8:b3ca/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
二、安装配置keepalived
官网下载地址:http://www.keepalived.org/software
1)安装:
#wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
#tar -zxvf keepalived-1.2.7.tar.gz
#cd keepalived-1.2.7
#./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-128.el5-x86_64/ #kernel目录根据实际修改。
预编译后出现:
Keepalived configuration
------------------------
Keepalived version : 1.2.7
Compiler : gcc
Compiler flags : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
Use VRRP Framework : Yes
Use VRRP VMAC : No
SNMP support : No
Use Debug flags : No
注意如果你没有用到LVS可以把lvs去掉即 --disable-lvs-syncd --disable-lvs
#make && make install
整理管理文件:
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
建立配置文件目录(注意:keepalived的配置文件默认在/etc/keepalived/目录)
#mkdir /etc/keepalived
两台服务器(两个节点)都这样安装即可
2)配置
关于配置文件的说明,可以详见这里:
http://bbs.ywlm.net/thread-845-1-1.html
节点A192.168.15.178配置如下:
#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email
{
}
notification_email_from [email protected]
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id MySQL-ha
}
vrrp_script chk_mysql {
script "killall -0 mysqld" #检测mysqld进程,也可以替换成检测脚本
interval 5
}
vrrp_instance VI_1 {
state master #从库配置此处均是slave
interface eth0
virtual_router_id 51
priority 100 #优先级,另一台改为90
advert_int 1
nopreempt #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.181
}
track_script {
chk_mysql #执行上面的命令
}
}
virtual_server 192.168.15.181 3306 {
delay_loop 6
lb_algo wcl
lb_kind DR
#nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.15.178 3306 {
weight 1
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 10.8.10.185 3306 {
weight 1
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
节点192.168.15.185配置如下:
#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email
{
}
notification_email_from [email protected]
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id MySQL-ha
}
vrrp_script chk_mysql {
script "killall -0 mysqld" #检测mysqld进程,也可以替换成检测脚本
interval 5
}
vrrp_instance VI_1 {
state slave #主库配置此处均是master
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.181
}
track_script {
chk_mysql #执行上面的命令
}
}
virtual_server 192.168.15.181 3306 {
delay_loop 6
lb_algo wcl
lb_kind DR
#nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.15.178 3306 {
weight 1
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 10.8.10.185 3306 {
weight 1
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
3)开启服务
#/usr/local/keepalived/sbin/keepalived -D
#ps -aux | grep keepalived
root 15416 0.0 0.0 61148 736 pts/1 S+ 16:43 0:00 grep keepalived
root 31121 0.0 0.0 35832 492 ? Ss Jan22 0:00 keepalived -D
root 31122 0.0 0.0 37924 1372 ? S Jan22 0:00 keepalived -D
root 31125 0.0 0.0 37924 1000 ? S Jan22 0:03 keepalived -D
三、测试
MySQL远程登录测试
客户端登录VIP,看是否能登录,在登录之两台MySQL服务器都要授权允许从远程登录
mysql> grant all privileges on phpcmsv9.* to 'dbuser_do'@'192.168.10.6' identified by '123456';
No connection. Trying to reconnect...
Connection id: 1168
Current database: phpcmsv9
Query OK, 0 rows affected (0.11 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
客户端登录结果:
此时查询的是192.168.15.185的数据库信息,关闭192.168.15.185 Mysql,继续联系数据库。
此时连接的是192.168.15.178的数据库。
Keepavlied完成后,接下来要测试的就是如果让master服务器和slave 也能够实现自动切换,同步保证数据同步正常。