Python script counts the number of tables in the MySQL library

Source: Internet
Author: User
Tags python script

#!/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

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.