注释:脚本(gomysql.py)需要进一步优化,初学者,努力中
首先配置需要执行的dbip.ini列表,格式如下
S1 192.168.0.5 3306 dbusername dbpasswd dbname
S2 192.168.0.6 3306 dbusername dbpasswd dbname
S3 192.168.0.7 3306 dbusername dbpasswd dbname
执行方法:./gomysql.py dbip.ini(参数一,数据库账号IP配置列表,实例如下) db.sql(参数二,执行的sql语句文本)
效果:
# ./readlnedb.py logdbip.ini 20150827/select_22.sql
logdbip.ini
20150827/select_22.sql
确认以上信息[y/n]:
脚本内容如下:
#encoding:utf-8
import sys
import os
import MySQLdb
if len(sys.argv) < 2:
print "error: " + sys.argv[0] + "file1 file2"
sys.exit()
file1=sys.argv[1]
file2=sys.argv[2]
print file1
print file2
#sql = open(file2,"r").read()
#print sql
str = raw_input("确认以上信息[y/n]:")
if str != "y":
exit()
for line in open(file1,"r"):
servername=line.strip().split()[0]
ip=line.strip().split()[1]
port=line.strip().split()[2]
user=line.strip().split()[3]
pswd=line.strip().split()[4]
dbname=line.strip().split()[5]
print servername
db = MySQLdb.connect(ip,user,pswd,dbname)
cursor = db.cursor()
for sql in open(file2,"r"):
cursor.execute(sql)
data = cursor.fetchall()
for row in data:
print row
# print "Data: %s " % row
db.close()