前言*在mysql高可用配置中,我们会看到有很多的方法,每种方法都有各自优缺点,那今天我们来看参看一下Mysql+Keepalived高可用配置。
一、安装环境:
系统版本:CentOS6.0 x86_64
Mysql版本:mysql-5.1.61
Mysqlserver_1: 192.9.117.140
Mysqlserver_2: 192.9.117.141
Keepalived-VIP:192.9.117.142
二、正式安装:
在两台服务器上分别安装mysql,这里直接采用yum安装,如下:
- yum install –y mysql mysql-devel mysql-server mysql-libs
安装完后,配置MySQL配置文件,mysql采用主主模式:
1) 192.9.117.140的配置文件如下:vi /etc/my.cnf
- [mysqld]
- datadir=/data/mysql
- socket=/var/lib/mysql/mysql.sock
- user=mysql
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-links=0
- log-bin=mysql-bin
- server-id = 1
- auto_increment_offset=1
- auto_increment_increment=2
- [mysqld_safe]
- log-error=/var/log/mysqld.log
- pid-file=/var/run/mysqld/mysqld.pid
- master-host =192.9.117.141
- master-user=tongbu
- master-pass=123456
- master-port =3306
- master-connect-retry=60
- replicate-do-db =map
2) 192.9.117.141的配置文件如下:vi /etc/my.cnf
- [mysqld]
- datadir=/data/mysql
- socket=/var/lib/mysql/mysql.sock
- user=mysql
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-links=0
- log-bin=mysql-bin
- server-id = 2
- auto_increment_offset=2
- auto_increment_increment=2
- [mysqld_safe]
- log-error=/var/log/mysqld.log
- pid-file=/var/run/mysqld/mysqld.pid
- master-host =192.9.117.140
- master-user=tongbu
- master-pass=123456
- master-port =3306
- master-connect-retry=60
- replicate-do-db =map
- 如上设置bin-log文件,并都设置对方为自己的主服务器,配置同步的数据库为map
三、配置MySQL:
1) 在两台mysql数据库服务器里面设置权限,分别执行如下命令:
- grant replication slave on *.* to 'tongbu'@'%' identified by '123456';
然后在141执行:
- show master status;
- +------------------+----------+--------------+------------------+
- | File
- |Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000002 |
- 106 |
- |
- |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
2) 在192.9.117.140上将192.9.117.141设为自己的主服务器执行如下命令:
- change master to master_host='192.9.117.141',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=106;
- 然后启动start slave;
- 注意这里写的bin-log参数是在141查看到的,即是对方的参数。
3) 然后在140执行:
- show master status;
- +------------------+----------+--------------+------------------+
- | File
- |Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000003 |
- 445|
- |
- |
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
4) 在192.9.117.141上将192.9.117.140设为自己的主服务器执行如下命令:
- change master to master_host='192.9.117.140',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=445;
- 然后启动start slave;
5) MySQL同步测试配置完毕,我们会发现在任何一台mysql上更新同步的数据库里面的数据,都会同步到另一台mysql。
四、安装Keepalived:
- tar zxf keepalived-1.2.1.tar.gz
- cd keepalived-1.2.1 &&./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686 &&make && make install
- DIR=/usr/local/ ;cp $DIR/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived && cp $DIR/sbin/keepalived /usr/sbin/
- if
- [ $? -eq 0 ];then
- echo "Keepalived system server config success!"
- else
- echo "Keepalived system server config failed ,please check keepalived!"
- exit 0
- fi
五、配置keepalived:
1) 创建vi keepalived.conf文件,内容如下:
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- # VIP1
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- lvs_sync_daemon_inteface eth0
- virtual_router_id 151
- priority 90
- advert_int 5
- nopreempt
- authentication {
- auth_type PASS
- auth_pass 2222
- }
- virtual_ipaddress {
- 192.9.117.142
- }
- }
- virtual_server 192.9.117.142 3306 {
- delay_loop 6
- lb_algo wrr
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 192.9.117.140 3306 {
- weight 100
- notify_down /data/sh/mysql.sh
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
- }
2) 141 keepalived同样如上配置,注意keepalived配置文件里面只添加一台mysql服务器ip地址,代表只使用这一台读写。
- 修改为Realserver192.9.117.141,并且设置优先级为90,都是BACKUP模式,并且nopreempt不抢占即可。
- 配置完后启动keepalived测试,可以先停止一台mysql,然后查看本地的keepalived是否停止,并且另外一台keepalived 已经变成了MASTER,如果是那就测试成功。
- 如上需要设置检查脚本/data/sh/mysql.sh,脚本内容为:
- pkill keepalived
- 下图为keepalived.conf部分配置
本文参考:Luwenju Mysql Keepalived 高可用配置,my.cnf还可以优化一下参数。更多精彩内容后期分享!
http://bbs.linuxtone.org/thread-8172-1-1.html