How the shell remotely connects MySQL in bulk

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.