一、配置mysql服务器ip列表如下,可自定义:
S1 1.1.1.1 3306 user passwd11 dbname_s1
S2 2.2.2.2 3306 user passwd22 dbname_s2
S3 3.3.3.3 3306 user passwd33 dbname_s3
二、#linux批量连接多服务器的shell脚本如下:
#!/bin/bash
if [[ "$#" -lt "2" ]]
then
echo "error"
echo "for example:$0 dbip.ini 20141001/select.sql"
exit 1
fi
name=(`cat /root/scripts/$1 | awk ‘{print$1}‘`)
ip=(`cat /root/scripts/$1 | awk ‘{print$2}‘`)
port=(`cat /root/scripts/$1 | awk ‘{print$3}‘`)
user=(`cat /root/scripts/$1 | awk ‘{print$4}‘`)
pass=(`cat /root/scripts/$1 | awk ‘{print$5}‘`)
db_name=(`cat /root/scripts/$1 | awk ‘{print$6}‘`)
time=`date -d ‘-1 day‘ +%Y%m%d`
touch /home/data/$time.txt
len=${#ip[*]}
num=0
while [ $num -lt $len ]
do
echo "=========== ${name[$num]} ============="
echo "=========== ${name[$num]} =============" >> /home/data/$time.txt
mysql -u${user[$num]} -p${pass[$num]} -h${ip[$num]} -P${port[$num]} ${db_name[$num]} < $2
let num++
done
三、脚本运行方式,sh jiaoben.sh 参数1 参数2
参数1=dbip.ini(mysql服务器的配置列表)
参数2=需要运行的sql语句的文本,
师例:比如我的脚本名字为,gomysql.sh 需要运行的sql放在select.sql的记事本里面,运行方法如下:
sh gomysql_gamedb.sh dbip.ini select.sql