最近要做个高可用的mysql。用mysql主主复制方式保证两台数据库的数据一致。结合lvs和keepalived一起使用(keepalived+lvs的设置会再另外一篇文章里写)。
搭好环境之后,本人做了以下测试并得到测试结果:
=======================测试步骤===========================
A机:活跃机器(主机)
B机:(备机)
1 在A上写数据,检查A ,B 两机的数据是否同步(test ok)
2 在B上写数据,检查A ,B 两机的数据是否同步(test ok)
3 A关闭数据库,在B上写数据
4 A启动数据库,检查A,B数据是否同步(test ok)
5 分别在A 和 B 上写数据,检查数据同步(test ok)
6 B关闭数据库,在A上写数据,检查数据同步(test ok)
7 B启动数据库,检查数据同步(test ok)
8 分别在A 和 B 上写数据,检查数据同步(test ok)
9 先关闭A机,然后写数据,再关闭B机,然后先启动A机,此时A机的数据没有与B同步。然后启动B机,过一下,B机后来写的数据同步到了A机。(test ok )
10 关闭A机,然后写数据。关闭B机,启动A机,然后写数据,启动B机,数据同步有错,会有主键冲突情况(test failed)
Last_Error: Error 'Duplicate entry '16' for key 'PRIMARY'' on query. Default database: 'radius'. Query: 'INSERT INTO radacct(ACCTSESSIONID,ACCTUNIQUEID,USERNAME,REALM,NASIPADDRESS,NASPORTID, NASPORTTYPE,
acctstarttime, ACCTSTOPTIME,acctsessiontime,acctauthentic,connectinfo_start,CONNECTINFO_STOP,acctinputoctets, acctoutputoctets,
calledstationid, callingstationid, ACCTTERMINATECAUSE,servicetype, framedprotocol, framedipaddress, acctstartdelay,acctstopdelay,accuniqueid)
VALUES('80804e49','0004707ab5d434a1','10.245.151.52','','192.168.12.1','2155892297','Wireless-802.11','2012-08-06
10:45:53','2012-08-06 11:11:45',1491,'','','',3211227,23921543,'hotspot1','64:A0:E7:C9:1A:A9','Idle-
Timeout','','','10.245.151.52',0,0,'0000000000')'
该问题的最好解决办法是保证两台数据库表的primary key 不会出现重复。也可以按下面的方法手工处理或者忽略该错误。
参考文章:
http://google3030.blog.163.com/blog/static/16172446520104282594532/
http://blog.163.com/yaning_softwo/blog/static/378377212011310115526121/
解决方法:
一、如果较少的错误可以手动解决
mysql -uroot -proot123 -e "stop slave;SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;start slave;"
slave stop;
set global sql_slave_skip_counter=n; \\ n=正整数,,看你的错误有几条,就跳过几条
slave start;
二、较多的情况下增加slave_skip_errors = 1062 参数
my.cnf 中增加slave_skip_errors = 1062 参数,忽略1062错误,若忽略多个错误,中间用逗号隔开,忽略所有用all
===================以下是设置================================
http://www.mike.org.cn/articles/mysql-master-slave-sync-conf-detail/
最终整理为一个mysqld.sh 脚本,红色部分根据实际情况修改
执行步骤为:
1 在两台数据库 先执行 sh mysqld.sh init_slave
2 在两台数据库 执行 sh mysqld.sh set_slave
========shell======
#!/bin/sh
source /root/.bash_profile
MYSQL_DIR=/usr/local/mysql
DEFAULTSET=/etc/my.cnf
MYSQL=`which mysql`
USER=root
PASSWD=root123
SERVER_ID=1
PEER_HOST_IP=192.168.4.191
PEER_USER=mysql
PEER_PASSWD=mysql123
start()
{
cd $MYSQL_DIR
PID=`pidof mysqld`
if [ "$PID" == "" ];then
#MYSQL_SAFE=`which mysqld_safe`
./bin/mysqld_safe --defaults-file=$DEFAULTSET & >/dev/null
sleep 5;
fi
PID=`pidof mysqld`
if [ "$PID" != "" ];then
sh /sh_dir/keepalived.sh restart
fi
}
stop()
{
cd $MYSQL_DIR
./bin/mysqladmin -uroot -proot123 shutdown
}
restart()
{
stop
sleep 5
start
}
help()
{
echo "usage: start | stop | restart | show"
echo "init_slave---> add slave config"
echo "set_slave----> set slave"
echo "slave_skip---> skip one count when primary key conflict"
echo "show_m -------> show master info"
echo "show_s -------> show slave info"
}
show()
{
ps -ef |grep mysqld
}
init_slave_conf()
{
echo "" >>/etc/my.cnf
echo "#slave" >>/etc/my.cnf
echo "server-id=$SERVER_ID" >>/etc/my.cnf
echo "binlog-do-db=radius" >>/etc/my.cnf
echo "binlog-ignore-db=mysql,test,information_schema,performance_schema" >>/etc/my.cnf
echo "slave_skip_errors=1062" >>/etc/my.cnf ####根据实际情况设置是否需要忽略改错误
/sh_dir/mysqld.sh restart
sleep 8
$MYSQL -u$USER -p$PASSWD -e "grant all on *.* to root@'$PEER_HOST_IP' identified by 'root123'";
$MYSQL -u$USER -p$PASSWD -e "grant all on *.* to $PEER_USER@'$PEER_HOST_IP' identified by '$PEER_PASSWD'";
$MYSQL -u$USER -p$PASSWD -e "flush privileges;";
}
slave_set()
{
$MYSQL -u$USER -p$PASSWD -e "grant all on radius.* to mysql@'%' identified by 'mysql123'";
PEER_MASTER=/tmp/masterinfo.log
$MYSQL -h $PEER_HOST_IP -u$USER -p$PASSWD -s -r -N -e "show master status;" >$PEER_MASTER
BIN_NAME=`cat $PEER_MASTER | awk '{print $1}'`
BIN_POS=`cat $PEER_MASTER | awk '{print $2}'`
DB_NAME=`cat $PEER_MASTER | awk '{print $3}'`
echo $BIN_NAME $BIN_POS $DB_NAME
SLAVE_SET="change master to
master_host='$PEER_HOST_IP',master_user='$PEER_USER',master_password='$PEER_PASSWD',master_log_file='$BIN_NAME',master_log_pos=$BIN_POS;"
echo $SLAVE_SET
$MYSQL -u$USER -p$PASSWD -e "stop slave;use $DB_NAME;$SLAVE_SET;start slave;show slave status\G"
}
slave_skip()
{
SKIP_COUNT=1
$MYSQL -u$USER -p$PASSWD -e "stop slave;SET GLOBAL SQL_SLAVE_SKIP_COUNTER=$SKIP_COUNT;start slave;"
}
show_master()
{
$MYSQL -u$USER -p$PASSWD -e "show master status\G"
}
show_slave()
{
$MYSQL -u$USER -p$PASSWD -e "show slave status\G"
}
keepalived_check()
{
PID=`/sbin/pidof mysqld`
/usr/bin/wall "====mysql down pid:[$PID]===="
if [ "$PID" == "" ];then
$SHELL_DIR/keepalived.sh stop
fi
}
case "$1" in
start)
start;;
stop)
stop;;
restart)
restart;;
show)
show;;
init_slave)
init_slave_conf;;
set_slave)
slave_set;;
slave_skip)
slave_skip;;
show_m)
show_master;;
show_s)
show_slave;;
*)
help;;
esac
=================end==============================