標籤:
最近要做個高可用的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 [email protected]‘$PEER_HOST_IP‘ identified by ‘root123‘";
$MYSQL -u$USER -p$PASSWD -e "grant all on *.* to [email protected]‘$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 [email protected]‘%‘ 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==============================
Mysql+keeplived+lvs