利用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
2、安装:
[root@localhost down]# tar -zxvf keepalived-1.2.7.tar.gz
[root@localhost down]# cd keepalived-1.2.7
17142202-ddb327b1c5e04f8690d1283379494bc
[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
#启动二进制文件
server-id=1
#服务器ID
17142224-57be9f673b3f4687a7007797cf7b931
设置完毕启动mariaDB服务器
[root@localhost /]# service mariamysql start
17142235-33036aa32a4c405484541a04f94d1a1
2、登录mysql,然后在增加一个账号专门用于同步,如下:
17142243-6d6b07ae57a848f7a8dbab5fb6a3b73
[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'; flu