One, configure the MySQL server IP list as follows, can be customized:
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
Second, #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]} <
Let num++
Done
Third, the script operation mode, sh jiaoben.sh parameter 1 parameter 2
Parameter 1=dbip.ini (configuration list for MySQL server)
Parameter 2 = The text of the SQL statement that needs to be run,
Example: For example, my script name is, gomysql.sh need to run SQL in Select.sql Notepad, run the following method:
SH gomysql_gamedb.sh dbip.ini Select.sql
How the shell remotely connects MySQL in bulk