-
Notifications
You must be signed in to change notification settings - Fork 0
Commiting mysql-connection code for review #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
code-sidd
wants to merge
3
commits into
master
Choose a base branch
from
mysql-conn-python
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,61 @@ | ||
#!/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() | ||
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) | ||
|
||
print "======Using pymysql========" | ||
print " " | ||
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;" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Format the query on multiple lines ? |
||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a argument for calling different types of query. And then use them in the main body to call the functions appropriately.