您的位置:首页 > 数据库 > MySQL

mysql主主搭建

2016-05-25 09:26 316 查看
关于mysql-HA,目前有多种解决方案,比如heartbeat、drbd、mmm、共享存储,但是它们各有优缺点。heartbeat、drbd配置较为复杂,需要自己写脚本才能实现mysql自动切换,对于不会脚本语言的人来说,这无疑是一种脑裂问题;对于mmm,生产环境中很少有人用,且mmm 管理端需要单独运行一台服务器上,要是想实现高可用,就得对mmm管理端做HA,这样无疑又增加了硬件开支;对于共享存储,个人觉得mysql数据还是放在本地较为安全,存储设备毕竟存在单点隐患。使用mysql双master+keepalived是一种非常好的解决方案,在mysql-HA环境中,mysql互为主从关系,这样就保证了两台mysql数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现mysql故障时自动切换

下面,我把即将上线的一个生产环境中的架构与大家分享一下,看一下这个架构中,mysql-HA是如何实现的,环境拓扑如下

 

 

 

Mysql-VIP:192.168.1.200

Mysql-master1:192.168.1.201

Mysql-master2:192.168.1.202

 

 

OS版本:CentOS 5.4

Mysql版本:5.0.89

Keepalived版本:1.1.20

 

 

 

一、mysqlmaster-master配置

1、修改mysql配置文件

两台mysql均如要开启binlog日志功能,开启方法:在mysql配置文件[mysqld]段中加上log-bin=mysql-bin选项

两台mysql的server-ID不能一样,默认情况下两台mysql的serverID都是1,需将其中一台修改为2即可

 

2、将192.168.1.201设为192.168.1.202的主服务器

在192.168.1.201上新建授权用户

 

mysql> grant replication slave on *.* to'replication'@'%' identified by 'replication';

Query OK, 0 rows affected (0.00 sec)

 

mysql> show master status;

+------------------+----------+--------------+------------------+

| File

|Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |

 

374 |

|

|

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

 

 

在192.168.1.202上将192.168.1.201设为自己的主服务器

 

mysql> change master to master_host='192.168.1.201',master_user='replication',master_password='replication',master_log_file='mysql-bin.000003',master_log_pos=374;

Query OK, 0 rows affected (0.05 sec)

 

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show slave status\G

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

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.201

Master_User: replication

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

Read_Master_Log_Pos: 374

Relay_Log_File: mysql-master2-relay-bin.000002

Relay_Log_Pos: 235

Relay_Master_Log_File: mysql-bin.000003

Slave_IO_Running:Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replcate_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: 374

Relay_Log_Space: 235

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

1 row in set (0.00 sec)

 

 

 

3、将192.168.1.202设为192.168.1.201的主服务器

在192.168.1.202上新建授权用户

mysql> grant replication slave on *.* to'replication'@'%' identified by 'replication';

 

mysql> show master status;

+------------------+----------+--------------+------------------+

| File

 

| Position | Binlog_Do_DB |Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |

374 |

|

|

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

在192.168.1.201上,将192.168.1.202设为自己的主服务器

 

mysql> change master tomaster_host='192.168.1.202',master_user='replication',master_password='replication',master_log_file='mysql-bin.000003',master_log_pos=374;

Query OK, 0 rows affected (0.05 sec)

 

mysql> start slave;

 

mysql> show slave status\G

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

 

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.202

Master_User: replication

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

Read_Master_Log_Pos: 374

Relay_Log_File: mysql-master1-relay-bin.000002

Relay_Log_Pos: 235

Relay_Master_Log_File: mysql-bin.000003

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_B:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wid_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 374

Relay_Log_Space: 235

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

1 row in set (0.00 sec)

 

 

4、mysql同步测试

如上述均正确配置,现在任何一台mysql上更新数据都会同步到另一台mysql,mysql同步在此不再演示

 

 

 

 

二、keepalived安装及配置

 

1、192.168.1.201服务器上keepalived安装及配置

安装keepalived

#tar zxvf keepalived-1.1.20.tar.gz

#cd keepalived-1.1.20

#./configure --prefix=/usr/local/keepalived--with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686

#make && make install

 

配置keepalived

我们自己在新建一个配置文件,默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件

#mkdir /etc/keepalived

#vi /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

global_defs {

 notification_email {

327568824@qq.com

 }

 notification_email_from 327568824@qq.com

 smtp_server 127.0.0.1

 smtp_connect_timeout 30

 router_id mysql-ha

 }

 vrrp_instance VI_1 {

 state BACKUP  #两台配置此处均是BACKUP

 interface eth0

 virtual_router_id 51

priority 100

 #优先级,另一台改为90

 advert_int 1

nopreempt#不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置

 authentication {

 auth_type PASS

 auth_pass 1111

 }

 virtual_ipaddress{

192.168.1.200

 }

 }

 virtual_server 192.168.1.200 3306 {

 delay_loop 2 #每个2秒检查一次real_server状态

 lb_algo wrr#LVS算法

 lb_kind DR#LVS模式

 persistence_timeout 60#会话保持时间

 protocol TCP

real_server 192.168.1.201 3306 {

 weight 3

 notify_down /usr/local/mysql/bin/mysql.sh #检测到服务down后执行的脚本

 TCP_CHECK {

 connect_timeout 10#连接超时时间

 nb_get_retry 3#重连次数

 delay_before_retry 3#重连间隔时间

 connect_port 3306#健康检查端口

 }

 }

 

 

 

 

编写检测服务down后所要执行的脚本

#vi /usr/local/mysql/bin/mysql.sh

 

#!/bin/sh

pkillkee
4000
palived

#chmod +x /usr/local/mysql/bin/mysql.sh

注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过pkill keepalived强制杀死keepalived进程,从而实现了mysql故障自动转移。另外,我们不用担心两个mysql会同时提供数据更新操作,因为每台mysql上的keepalived的配置里面只有本机mysql的IP+VIP,而不是两台mysql的IP+VIP

 

 

启动keepalived

#/usr/local/keepalived/sbin/keepalived –D

#ps -aux | grep keepalived

 

 

 

测试

●找一台局域网PC,然后去ping

mysql的VIP,这时候mysql的VIP是可以ping的通的

●停止mysql服务,看keepalived健康检查程序是否会触发我们编写的脚本

 

 

 

 

2、192.168.1.202上keepalived安装及配置

 

安装keepalived

#tar zxvf keepalived-1.1.20.tar.gz

#cd keepalived-1.1.20

#./configure --prefix=/usr/local/keepalived--with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686

#make && make install

 

配置keepalived

dr模式因为需要配置网卡:

/sbin/ifconfig lo:0 192.168.1.200 broadcast 192.168.1.255 netmask 255.255.255.255 up

这台配置和上面基本一样,但有三个地方不同:优先级为90、无抢占设置、real_server为本机IP

#mkdir /etc/keepalived

#vi /etc/keepalived/keepalived.conf

——————————————————————————————————————————

 

! Configuration File for keepalived

global_defs {

notification_email {

327568824@qq.com

}

notification_email_from 327568824@qq.com

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id mysql-ha

}

 

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 51

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.1.200

}

}

 

virtual_server 192.168.1.200 3306 {

delay_loop 2

lb_algo wrr

lb_kind DR

persistence_timeout60

protocol TCP

real_server 192.168.1.202 3306 {

weight 3

notify_down /usr/local/mysql/bin/mysql.sh

TCP_CHECK {

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

connect_port 3306

}

}

——————————————————————————————————————————

 

 

 

编写检测服务down后所要执行的脚本

#vi /usr/local/mysql/bin/mysql.sh

#!/bin/sh

pkillkeepalived

#chmod +x /usr/local/mysql/bin/mysql.sh

 

启动keepalived

#/usr/local/keepalived/sbin/keepalived –D

#ps -aux | grep keepalived

 

测试

●停止mysql服务,看keepalived健康检查程序是否会触发我们编写的脚本

 

 

 

 

三、测试

 

●MySQL远程登录测试

我们找一台安装有mysql客户端的windows,然后登录VIP,看是否能登录,在登录之两台mysql服务器都要授权允许从远程登录

 

mysql> grant all privileges on *.* to'root'@'%' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected(0.00 sec)

 

 

使用客户端登录VIP测试

C:\mysql\bin>mysql.exe -uroot -p123456 -h192.168.1.200 -P3306

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 224

Server version: 5.0.89-log Source distribution

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

 

 

●    keepalived故障转移测试

※在windows客户端一直去ping  VIP,然后关闭192.168.1.201上的keepalived,正常情况下VIP就会切换到192.168.1.202上面去

※开启192.168.1.201上的keepalived,关闭192.168.1.202上的keepalived,看是否能自动切换,正常情况下VIP又会属于192.168.1.201

注:keepalived切换速度还是非常块的,整个切换过程只需1-3秒

 

●    mysql故障转移测试

※在192.168.1.201上关闭mysql服务,看VIP是否会切换到192.168.1.202上

※开启192.168.1.201上的mysql和keepalived,然后关闭192.168.1.202上的mysql,看VIP是否会切换到192.168.1.201上

下面是用windows客户端连接的mysql的VIP,在切换时我执行了一个mysql查询命令,从执行show databases到显示出结果时间为3-5秒(大家可以看到上面有个错误提示,不过不用担心,因为我们的keepalived切换大概为3秒左右,这3 秒左右VIP是谁都不属于的)

mysql> show databases;

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    592

Current database: *** NONE ***

 

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

3 rows in set (9.01 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql