python脚本统计 MySQL 库中表的个数

#!/usr/bin/python

import os,sys,pymysql

mysqlbase = '/usr/local/mysql/bin/'

if  len(sys.argv)  == 1:
    print('format: python + Script_name + user + password + ip')
elif sys.argv[1] == '-h':
    print('format: python + Script_name + user + password + ip')
else:
    user = sys.argv[1]
    passw = sys.argv[2]
    ip = sys.argv[3]
    dbnam=os.popen(mysqlbase+'mysql -h'+ip+' -u'+user+' -p'+passw+'   -h'+ip+' -e "show databases"|grep -vE "mysql|Database|information_schema|performance_schema|sys"').read()
#    dbnam=os.popen(mysqlbase+'mysql -h'+ip+' -u'+user+' -p'+passw+'  -S /tmp/mysql3306.sock -h'+ip+' -e "show databases"|grep -vE "mysql|Database|information_schema|performance_schema|sys"').read()
    DBNAME = dbnam.replace('\n',',').rstrip(',').split(',')
    for dbname in DBNAME:
        lists =
        db=pymysql.connect(ip,user,passw,dbname)
        cursor=db.cursor()
        cursor.execute("show tables")
        data=cursor.fetchall()
        for tabname in data:
            lists.append(tabname)
        print(dbname,(len(data)))
        db.close()
 
# python connmysql.py root 123456 192.168.6.218
('cr_debug', 6)
('hh', 3)
('lts', 15)
('toprankdb', 496)
('tr', 1)
('zabbix', 127)
#每天学习一点点,日积月累。

0 个评论

要回复文章请先登录注册