利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟VIP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换。
硬件拓扑如下:
VIP:192.168.1.200
mysql1:192.168.1.201
mysql2:192.168.1.202
操作系统:CentOS release 6.3(32位)
MySQL版本:MariaDB 5.5.31 Stable
下载地址(64位请下载64版本):https://downloads.mariadb.org/f/mariadb-5.5.31/kvm-tarbake-jaunty-x86/mariadb-5.5.31.tar.gz/from/http:/mirrors.scie.in/mariadb
Keepalived版本:Version 1.2.7
下载地址:http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
一、配置Centos运行环境:
执行:
rpm -qa|grep mysql
rpm -e mysql
yum -y remove mysql-server mysql
yum -y remove php-mysql
移除系统自带的mysql
yum -y install yum-fastestmirror
yum -y update
更新系统软件;
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum install -y ntp
ntpdate -d cn.pool.ntp.org
date
设置时区并同步系统时间
#Disable SeLinux
if [ -s /etc/selinux/config ]; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi
ldconfig
关闭安全增强
cat >>/etc/security/limits.conf<<eof
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
eof
cat >>/etc/sysctl.conf<<eof
fs.file-max=65535
eof
修改文件句柄的最大数量
安装一些必备软件
yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bison libaio-devel cmake libnl* libpopt* popt-static openssl-devel
二、安装maridDB:
1、下载相关软件源码包
[root@localhost down]# wget https://downloads.mariadb.org/f/mariadb-5.5.31/kvm-tarbake-jaunty-x86/mariadb-5.5.31.tar.gz/from/http:/mirrors.scie.in/mariadb
2、配置编译器,提高性能
CFLAGS="-O3"
CXX=gcc
CXXFLAGS="-O3 -felide-constructors -fno-exceptions -fno-rtti"
3、开始安装mariadb
添加mysql用户和用户组
[root@localhost down]# groupadd mysql
[root@localhost down]# useradd -s /sbin/nologin -M -g mysql mysql
解压mariadb
[root@localhost down]# tar -zxvf mariadb-5.5.31
[root@localhost down]# cd mariadb-5.5.31
安装到/usr/local/mariamysql目录:
[root@localhost down]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariamysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DBUILD_CONFIG=mysql_release -DFEATURE_SET=community -DWITH_EMBEDDED_SERVER=OFF && make && make install
复制配置文件到/etc/my.cnf
[root@localhost down]# cp /usr/local/mariamysql/support-files/my-huge.cnf /etc/my.cnf
设置mariamysql为系统服务
[root@localhost down]# cp /usr/local/mariamysql/support-files/mysql.server /etc/init.d/mariamysql
初始化mariaDB数据库:
[root@localhost down]# /usr/local/mariamysql/scripts/mysql_install_db --basedir=/usr/local/mariamysql/ --datadir=/usr/local/mariamysql/data/ --user=mysql
至此mariaDB安装完成。
三、keepalived安装:
1、下载源码包:
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
![17142150-9139773d3a504528948cfcda35d251b](/blog/108846/201307/17142150-9139773d3a504528948cfcda35d251b9.png)
2、安装:
[root@localhost down]# tar -zxvf keepalived-1.2.7.tar.gz
[root@localhost down]# cd keepalived-1.2.7
![17142202-ddb327b1c5e04f8690d1283379494bc](/blog/108846/201307/17142202-ddb327b1c5e04f8690d1283379494bcd.png)
[root@localhost keepalived-1.2.7]# ./configure --prefix=/usr/local/keepalived
安装到/usr/local/keepalived目录下;
至此keepalived安装完毕。
四、分别在201和202两台机器上都重复二,三安装好mariaDB和keepalived。
五、配置201数据库服务器:
1、设置mariaDB数据库配置文件:
[root@localhost /]# vi /etc/my.cnf
确保/etc/my.cnf中有如下参数,没有的话需手工添加,并重启mysql服务。
[mysqld]
log-bin=mysql-bin
log-bin=mysql-bin
#启动二进制文件
server-id=1
server-id=1
#服务器ID
![17142224-57be9f673b3f4687a7007797cf7b931](/blog/108846/201307/17142224-57be9f673b3f4687a7007797cf7b931c.png)
设置完毕启动mariaDB服务器
[root@localhost /]# service mariamysql start
![17142235-33036aa32a4c405484541a04f94d1a1](/blog/108846/201307/17142235-33036aa32a4c405484541a04f94d1a1b.png)
2、登录mysql,然后在增加一个账号专门用于同步,如下:
![17142243-6d6b07ae57a848f7a8dbab5fb6a3b73](/blog/108846/201307/17142243-6d6b07ae57a848f7a8dbab5fb6a3b739.png)
[root@localhost /]# /usr/local/mariamysql/bin/mysql -uroot -p #初始密码为空到Enter password:处直接回车即可
MariaDB [(none)]> grant replication slave on *.* to 'backup'@'192.168.1.202' identified by 'backup'; flush privileges;
显示master状态:
MariaDB [(none)]> show master status;
![17142344-7dcc22025896465198ee869755279ab](/blog/108846/201307/17142344-7dcc22025896465198ee869755279abc.png)
记录下File和Position然后在202上面设置从201同步。
六、配置202数据库服务器:
[root@localhost /]# vi /etc/my.cnf
确保/etc/my.cnf中有如下参数,没有的话需手工添加,并重启mysql服务。
[mysqld]
log-bin=mysql-bin
log-bin=mysql-bin
#启动二进制文件
server-id=10(此处要设置的跟201不同)
server-id=10(此处要设置的跟201不同)
#服务器ID
![17142400-4b4ea1d78bb145708f68c9e961dff91](/blog/108846/201307/17142400-4b4ea1d78bb145708f68c9e961dff918.png)
设置完毕启动mariaDB服务器。
[root@localhost /]# service mariamysql start
登录数据库:
[root@localhost /]# /usr/local/mariamysql/bin/mysql -uroot -p
输入:
MariaDB [(none)]> change master to master_host='192.168.1.201',master_user='backup',master_password='backup',master_log_file='mysql-bin.000010',master_log_pos=245;
注意:245对应上面在201上面记下的Position,mysql-bin.000010对应201上面记录的File
![17142412-d6feee7e8985446fb16c1ab131981d3](/blog/108846/201307/17142412-d6feee7e8985446fb16c1ab131981d3a.png)
执行成功后,输入命令显示从库状态:
MariaDB [(none)]> show slave status \G;
![17142425-8fc12d04a2aa4637bfc499466d77087](/blog/108846/201307/17142425-8fc12d04a2aa4637bfc499466d770872.png)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
两项都显示Yes时说明从201同步数据成功。
至此201为主202为从的主从架构数据设置成功!
七、设置201和202互为主从:
1、202机器上增加一个帐号专门用于同步数据:
MariaDB [(none)]> grant replication slave on *.* to 'backup'@'192.168.1.201' identified by 'backup'; flush privileges;
![17142435-0d2323ec5e5a48928df917a3032d0fe](/blog/108846/201307/17142435-0d2323ec5e5a48928df917a3032d0feb.png)
2、显示202做为主库时的状态:
MariaDB [(none)]> show master status;
![17142441-dcbbb2a7e86d4d9cbae8e600914773d](/blog/108846/201307/17142441-dcbbb2a7e86d4d9cbae8e600914773db.png)
3、在201数据库服务器上:
MariaDB [(none)]> change master to master_host='192.168.1.202',master_user='backup',master_password='backup',master_log_file='mysql-bin.000005',master_log_pos=5005;
![17142450-c11afefa097a491188d8ab0a048ec72](/blog/108846/201307/17142450-c11afefa097a491188d8ab0a048ec72e.png)
注意:5005对应上面在202上面记下的Position,mysql-bin.000005对应202上面记录的File
显示状态:
MariaDB [(none)]> show slave status \G;
![17142457-2f5c83c4a84544dc8c3de4df011dff0](/blog/108846/201307/17142457-2f5c83c4a84544dc8c3de4df011dff0e.png)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
两项都显示Yes时说明从202同步数据成功。
至此201、202互为主从设置成功!
可以试试在这两台服务器上任何一台增加一个数据库,并建个表,增加一些数据看看,互为主从同步的状态是否成功!
首先在201上面:
MariaDB [(none)]> create database mysqltest;
MariaDB [(none)]> use mysqltest;
MariaDB [mysqltest]> create table user(id int(5),name char(10));
MariaDB [mysqltest]> insert into user values (00001,'zhangsan');
在202上面验证一下:
MariaDB [(none)]> use mysqltest;
MariaDB [mysqltest]> select * from user;
![17142508-c106ea7f46304b788a30e5097ac0073](/blog/108846/201307/17142508-c106ea7f46304b788a30e5097ac0073f.png)
会发现201上面的数据已经自动同步到202上面了
同样在202上面:
MariaDB [mysqltest]> insert into user values (00002,'wander');
在201上面验证一下:
MariaDB [mysqltest]> select * from user;
![17142519-f5fd0a3f3eb04f82b8db39d02404be1](/blog/108846/201307/17142519-f5fd0a3f3eb04f82b8db39d02404be1d.png)
互为主从结构设置完毕
注意:如果同步不成功,首先要确保服务器3306端口打开的。
centos可以用service iptables stop关闭防火墙。
八、利用keepalived实现高可用
keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换;
1、keepalived设置:
201服务器上面,编辑keeplaived.conf配置文件:
[root@localhost /]# vi /usr/local/keepalived/etc/keepalived/keepalived.conf
![17142530-4245f8a890d645c1a4f1054a8f08799](/blog/108846/201307/17142530-4245f8a890d645c1a4f1054a8f087995.png)
配置文件内容如下:
! Configuration File for keepalived
global_defs {
router_id mysql-ha
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 201
priority 100 #优先级,高优先级竞选为master
advert_int 1
nopreempt #设置为不抢占 注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高,另一台不要设置这个选项
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.200
}
}
virtual_server 192.168.1.200 3306 {
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.1.201 3306 {
weight 1
notify_down /usr/local/keepalived/etc/keepalived/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 2
delay_before_retry 1
}
}
}
~
global_defs {
router_id mysql-ha
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 201
priority 100 #优先级,高优先级竞选为master
advert_int 1
nopreempt #设置为不抢占 注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高,另一台不要设置这个选项
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.200
}
}
virtual_server 192.168.1.200 3306 {
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.1.201 3306 {
weight 1
notify_down /usr/local/keepalived/etc/keepalived/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 2
delay_before_retry 1
}
}
}
~
![17142540-c8a96083b9764616b774decdcf7b45b](/blog/108846/201307/17142540-c8a96083b9764616b774decdcf7b45b9.png)
编辑mysql服务停止后的切换脚本:mysql.sh
[root@localhost /]# vi /usr/local/keepalived/etc/keepalived/mysql.sh
![17142550-cc01b215f96241cd899845e135521ae](/blog/108846/201307/17142550-cc01b215f96241cd899845e135521aed.png)
内容如下:
#!/bin/bash
pkill keepalived
pkill keepalived
![17142558-039eab18e8dc40c3949bf0b45916ddd](/blog/108846/201307/17142558-039eab18e8dc40c3949bf0b45916ddd5.png)
2、启动201上面的keepalived
[root@localhost /]# /usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf -D
查看:
![17142606-8df4887364c047b598bc2f995c51f6c](/blog/108846/201307/17142606-8df4887364c047b598bc2f995c51f6c9.png)
启动成功后会有三个keepalived进程
此是在任一局域机器上面ping 192.168.1.200发现已经可以ping通,并且用192.168.1.200这个IP也能够连接到数据库服务器。
3、在202机器上面重复1、2步骤;
配置keepalived.conf文件的时候注意要把
real_server 192.168.1.201 3306改为 real_server 192.168.1.202 3306
virtual_router_id 201 改为virtual_router_id 202
priority 100改为priority 90
去掉nopreempt
至此MariaDB+Keepalived双主高可用配置MySQL-HA设置完毕。
可以试着把201上面的mariaDB停止
[root@localhost /]# service mariamysql stop;
会发现连接192.168.1.200还是可以连接上去的,keepalived会自动切换到202的服务器上面去。这样,当一台数据库服务器发生故障时,另一台服务器可以立即切换过来,保证高可用。
转载于:https://blog.51cto.com/zhangyc/1371925