您的位置:首页 > 数据库 > MySQL

自己编写自动同步脚本

2018-03-09 18:34 281 查看
Step1:
运行脚本,将结果保存到sync_date.log下;
执行的时候,将地址修改为slave ip
--databases 指定为需要同步的db_name
user 和password修改为对应的账号密码;





/*
#!/bin/bash

#define sync function
sync(){
a=`pt-table-sync --execute --sync-to-master --lock 1 --print --charset=utf8 --verbose --databases itdb u=copy2,p=jsddwcc,h=10.8.135.156,P=3306`
echo -e "$a"
}

#output sync to log file
path=sync_`date +%Y%m%d.log`

sync > $path

#print sync result 

echo "Sync OK, plese check $path"
*/
Step 2:
运行 sh sync.sh  slave_address  db_name  slave_user_name  slave_password的方式:
一共四个参数,依次进行;





/*
#!/bin/bash

#define sync function
#h is host ,this is slave address
#the charset is setted utf8, if your table is not utf8, remove it
#lock could be 0 - 3, 0=don't lock; 1 lock sync part; 2 lock table ;3 lock server;

slave_add=$1
slave_db=$2
slave_user=$3
slave_pass=$4

sync(){
a=`pt-table-sync --execute --sync-to-master --lock 1 --print --charset=utf8 --verbose --databases $slave_db u=$slave_user,p=$slave_pass,h=$slave_add,P=3306`
echo -e "$a"
}

#output sync to log file
path=sync_`date +%Y%m%d.log`

sync > $path

#print sync result 

echo "Sync OK, plese check $path"

*/
Step3:
当用户未输入4个参数的时候,给出提示;





/*
#!/bin/bash

#define sync function
#h is host ,this is slave address
#the charset is setted utf8, if your table is not utf8, remove it
#lock could be 0 - 3, 0=don't lock; 1 lock sync part; 2 lock table ;3 lock server;

slave_add=$1
slave_db=$2
slave_user=$3
slave_pass=$4

sync(){
a=`pt-table-sync --execute --sync-to-master --lock 1 --print --charset=utf8 --verbose --databases $slave_db u=$slave_user,p=$slave_pass,h=$slave_add,P=3306`
echo -e "$a"
}

if [ $# -ne 4  ]
then
echo 
echo Usage: sh script.sh Slave_Address db_name Slave_username Slave_password;
echo There has 4 parameters. No error in the order of the parameters;
echo Plese try again
echo
else
#output sync to log file
path=sync_`date +%Y%m%d.log`
sync > $path

#print sync result 
echo "Sync OK, plese check $path"
fi

 */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息