一.mysql主主复制篇
1.安装mysql
sudo apt-get install mysql-server
2.迁移datadir目录
这一步不是必须的,自己根据磁盘使用情况判断.
(1)迁移datadir目录
sudo mv /var/lib/mysql /home
(2)修改my.cnf配置文件
vi /etc/mysql/my.cnf
datadir=/var/lib/mysql --> datadir=/home/mysql
(3)修改usr.sbin.mysqld文件
sudo vi /etc/apparmor.d/usr.sbin.mysqld
/var/lib/mysql/ r, --> /home/mysql/ r,
/var/lib/mysql/** rwk, --> /home/mysql/** rwk,
(4)重启apparmor服务
sudo /etc/init.d/apparmor restart
(5)重启mysql
sudo service mysql restart
3.增加my.cnf中主主复制需要用到的配置,并重启mysql加载最新配置.
sudo vi /etc/mysql/my.cnf
server-id=1
log_bin=/home/mysql/mysql-bin
//下面这个参数的意义及配置原因请参阅http://www.cnblogs.com/billyxp/p/3470376.html
slave-net-timeout=60
//下面两个参数的意义及配置原因请参阅http://sndapk.blog.51cto.com/5385144/922606
auto-increment-increment=2
auto-increment-offset=1
sudo service mysql restart
4.增加主主复制专用mysql用户并赋予该用户相应权限.
mysql -u root -p
use mysql;
create user replicationusername;
update user set password=password('insert password here') where user='replicationusername';
grant replication slave on *.* to 'replicationusername'@'%';
flush privileges;
5.配置master服务器
假设两台服务器的地址分别为192.168.1.101和192.168.1.102.
(1)在192.168.1.101上的mysql中执行:
change master to master_host='192.168.1.102',master_user='replicationusername',master_password='insert password here';
(1)在192.168.1.102上mysql中执行:
change master to master_host='192.168.1.101',master_user='replicationusername',master_password='insert password here';
6.启动复制服务并验证.
在mysql中执行:
start slave;
show slave status\G;
显示以下信息则表示已成功启动复制服务(注意双yes):
二.keepalived篇
1.安装keepalived
sudo apt-get install keepalived
2.增加keepalived配置文件
sudo vi /etc/keepalived/keepalived.cnf
内容如下:
global_defs {
notification_email {
username@domainname.com
}
notification_email_from username@domainname.com
smtp_server smtp.domain.com
smtp_connect_timeout 30
router_id MySQL-HA
}
vrrp_script check_run {
script "/etc/keepalived/service_check.sh"
interval 3
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 103
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.103
}
track_script {
check_run
}
}
virtual_server 192.168.1.103 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.1.101 3306 {
weight 1
notify_down /etc/keepalived/service_down.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
3.增加mysql服务检测脚本
sudo vi /etc/keepalived/service_check.sh
内容如下:
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_USER='root'
MYSQL_PASSWORD='insert password here'
CHECK_TIME=3
#mysql is working MYSQL_OK is 0 , mysql down MYSQL_OK is 1
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" > /dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=0
else
MYSQL_OK=1
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 0 ] ; then
CHECK_TIME=0
echo "OK"
exit 0
fi
if [ $MYSQL_OK -eq 1 ] && [ $CHECK_TIME -eq 1 ]
then
#/etc/init.d/keepalived stop
echo "failed"
pkill keepalived
exit 1
fi
sleep 1
done
4.增加服务关闭脚本.
sudo vi /etc/keepalived/service_down.sh
内容如下:
#!/bin/sh
pkill keepalived
5.重启keepalived服务.
sudo /etc/init.d/keepavlied restart
使用ip a指令验证是否启动成功,显示如下信息则表示启动成功:
整个过程中有几个需要注意的点:
1.mysql服务要先于keepalived服务启动,因为有检测脚本的存在,如果mysql没有响应了,keepalived服务也会被脚本关闭.
2.keepalived使用需要虚拟IP,所以在阿里云这种不能单独申请IP的地方是无法使用这种服务的.
3./etc/keepalived/下的两个脚本文件需要有可执行权限.