#!/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)
#每天学习一点点, the cumulative.
Python script counts the number of tables in the MySQL library