From 69393cc6ad5baa18c97f77ba2a02604dd1404fc0 Mon Sep 17 00:00:00 2001 From: SiddhantSawant Date: Tue, 13 Jun 2017 10:51:47 -0400 Subject: [PATCH 1/3] Commiting mysql-connection code for review --- mysql-py/mysql-conn.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/mysql-py/mysql-conn.py b/mysql-py/mysql-conn.py index 87a9245..011f188 100755 --- a/mysql-py/mysql-conn.py +++ b/mysql-py/mysql-conn.py @@ -1,22 +1,17 @@ #!/usr/bin/python - hostname = '127.0.0.1' username = 'root' password = 'redhat' database = 'mysql' -#socket ='/tmp/mysql-master5520.sock' -# Simple routine to run a query on a database and print the results: def Query( conn ) : print "Printing user and hosts from the box\n" cur = conn.cursor() cur.execute( "SELECT user,host FROM user" ) -# for row in cur : row = cur.fetchone() while row is not None: print row[0], row[1] row = cur.fetchone() -# print (row) def index ( conn1 ) : cur = conn1.cursor() @@ -25,7 +20,6 @@ def index ( conn1 ) : for row in cur : print (row) - def data_big ( conn3 ) : cur = conn3.cursor() print "\n\nFind biggest db" @@ -40,8 +34,7 @@ def data_dist ( conn4 ) : for row in cur : print (row) -print "======Using pymysql========" -print " " + import pymysql as my connect = my.connect( host=hostname, user=username, passwd=password, db=database ) Query( connect ) From 2edfbc512ad97578f8df547cb6608545d85bf0f0 Mon Sep 17 00:00:00 2001 From: SiddhantSawant Date: Fri, 28 Jul 2017 06:10:21 -0400 Subject: [PATCH 2/3] Added arguements --- mysql-py/mysql-conn.py | 107 ++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 44 deletions(-) diff --git a/mysql-py/mysql-conn.py b/mysql-py/mysql-conn.py index 011f188..35b97b7 100755 --- a/mysql-py/mysql-conn.py +++ b/mysql-py/mysql-conn.py @@ -1,44 +1,63 @@ -#!/usr/bin/python -hostname = '127.0.0.1' -username = 'root' -password = 'redhat' -database = 'mysql' - -def Query( conn ) : - print "Printing user and hosts from the box\n" - cur = conn.cursor() - cur.execute( "SELECT user,host FROM user" ) - row = cur.fetchone() - while row is not None: - print row[0], row[1] - row = cur.fetchone() - -def index ( conn1 ) : - cur = conn1.cursor() - print "\n\nFind total number of tables, rows, total data in index size for given MySQL Instance\n" - cur.execute( "SELECT count(*) tables,concat(round(sum(table_rows)/1000000,2),'M') rows,concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,round(sum(index_length)/sum(data_length),2) idxfrac FROM information_schema.TABLES;") - for row in cur : - print (row) - -def data_big ( conn3 ) : - cur = conn3.cursor() - print "\n\nFind biggest db" - cur.execute ( "SELECT count(*) tables,table_schema,concat(round(sum(table_rows)/1000000,2),'M') rows, concat(round(sum(data_length)/(1024*1024*1024),2),'G') data, concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx, concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size, round(sum(index_length)/sum(data_length),2) idxfrac FROM information_schema.TABLES GROUP BY table_schema ORDER BY sum(data_length+index_length) DESC LIMIT 10;" ) - for row in cur : - print (row) - -def data_dist ( conn4 ) : - cur = conn4.cursor() - print "\n\nData Distribution by Storage Engines\n" - cur.execute ( "SELECT engine,count(*) tables,concat(round(sum(table_rows)/1000000,2),'M') rows,concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,round(sum(index_length)/sum(data_length),2) idxfrac FROM information_schema.TABLES GROUP BY engine ORDER BY sum(data_length+index_length) DESC LIMIT 10;" ) - for row in cur : - print (row) - - -import pymysql as my -connect = my.connect( host=hostname, user=username, passwd=password, db=database ) -Query( connect ) -index( connect ) -data_big ( connect ) -data_dist ( connect ) -connect.close() +import sys +import logging +import argparse +import MySQLdb as mysql +from warnings import filterwarnings + + +def parse_options(): + parser = argparse.ArgumentParser() + parser.add_argument("-H", "--host", dest="host", help='Enter Hostname/ip to connect') + parser.add_argument("-u", "--user", dest="user", help='Enter username to connect') + parser.add_argument("-p", "--password", dest="password" , help='Specify Password') + parser.add_argument("-P", "--port", dest="port_no", default=3306, type=int,help="MySQL port to connect") + + if len(sys.argv) == 1: + parser.print_help() + sys.exit(1) + return parser.parse_args() + +global options +options = parse_options() + +def get_mysql_conn(host, port_no): + conn = mysql.connect(host=host, port=port_no,read_default_group='client', read_default_file='~/.my.cnf') + filterwarnings('ignore', category=mysql.Warning) + return conn + +def Query(): + cursor = get_mysql_conn(options.host, options.port_no).cursor(mysql.cursors.DictCursor) + sql = "SELECT user,host FROM mysql.user" + cursor.execute(sql) + print "User available in server are \n" + print(cursor.fetchall()) + cursor.close() + +def index(): + cursor = get_mysql_conn(options.host, options.port_no).cursor(mysql.cursors.DictCursor) + sql = "SELECT count(*) tables,concat(round(sum(table_rows)/1000000,2),'M') rows,concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,round(sum(index_length)/sum(data_length),2) idxfrac FROM information_schema.TABLES;" + print "\nPrinting Dup indexes\n" + cursor.execute(sql) + print(cursor.fetchall()) + cursor.close() + + +def data_big(): + cursor = get_mysql_conn(options.host, options.port_no).cursor(mysql.cursors.DictCursor) + print "\nFind biggest db\n" + sql= "SELECT count(*) tables,table_schema,concat(round(sum(table_rows)/1000000,2),'M') rows, concat(round(sum(data_length)/(1024*1024*1024),2),'G') data, concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx, concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size, round(sum(index_length)/sum(data_length),2) idxfrac FROM information_schema.TABLES GROUP BY table_schema ORDER BY sum(data_length+index_length) DESC LIMIT 10;" + cursor.execute(sql) + print(cursor.fetchall()) + cursor.close() + +def data_dist(): + cursor = get_mysql_conn(options.host, options.port_no).cursor(mysql.cursors.DictCursor) + print "\n\nData Distribution by Storage Engines\n" + cursor.execute("SELECT engine,count(*) tables,concat(round(sum(table_rows)/1000000,2),'M') rows,concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,round(sum(index_length)/sum(data_length),2) idxfrac FROM information_schema.TABLES GROUP BY engine ORDER BY sum(data_length+index_length) DESC LIMIT 10;") + print(cursor.fetchall()) + cursor.close() + +Query() +data_big() +index() +data_dist() From c3f981b2e6d9db6fe5b6bbefe5c180a7608b60ef Mon Sep 17 00:00:00 2001 From: SiddhantSawant Date: Fri, 28 Jul 2017 06:32:34 -0400 Subject: [PATCH 3/3] Added Arguments --- mysql-py/mysql-conn.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mysql-py/mysql-conn.py b/mysql-py/mysql-conn.py index 35b97b7..28c5ac2 100755 --- a/mysql-py/mysql-conn.py +++ b/mysql-py/mysql-conn.py @@ -4,7 +4,6 @@ import MySQLdb as mysql from warnings import filterwarnings - def parse_options(): parser = argparse.ArgumentParser() parser.add_argument("-H", "--host", dest="host", help='Enter Hostname/ip to connect') @@ -41,7 +40,6 @@ def index(): print(cursor.fetchall()) cursor.close() - def data_big(): cursor = get_mysql_conn(options.host, options.port_no).cursor(mysql.cursors.DictCursor) print "\nFind biggest db\n"