forked from divyansh-cyber/AceE6data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mysql_connection.py
More file actions
134 lines (108 loc) Β· 4.88 KB
/
test_mysql_connection.py
File metadata and controls
134 lines (108 loc) Β· 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
"""
MySQL Connection Test Script
This script tests your MySQL connection and shows what data is available.
"""
import mysql.connector
import json
from datetime import datetime
def test_mysql_connection():
"""Test MySQL connection and show database info."""
print("π Testing MySQL Connection")
print("="*50)
# Get connection details
print("Enter your MySQL connection details:")
host = input("Host (default: localhost): ").strip() or "localhost"
port = int(input("Port (default: 3306): ").strip() or "3306")
user = input("Username (default: root): ").strip() or "root"
password = input("Password: ").strip()
try:
# Test connection
print("\nπ Connecting to MySQL...")
connection = mysql.connector.connect(
host=host,
port=port,
user=user,
password=password
)
print("β
Connected to MySQL successfully!")
cursor = connection.cursor()
# Show MySQL version
cursor.execute("SELECT VERSION()")
version = cursor.fetchone()[0]
print(f"π MySQL Version: {version}")
# Show databases
cursor.execute("SHOW DATABASES")
databases = [row[0] for row in cursor.fetchall()]
print(f"π Available databases: {', '.join(databases)}")
# Check if observability_test exists
if 'observability_test' in databases:
print("β
observability_test database found!")
# Switch to the database
cursor.execute("USE observability_test")
# Show tables
cursor.execute("SHOW TABLES")
tables = [row[0] for row in cursor.fetchall()]
print(f"π Tables: {', '.join(tables)}")
# Show table sizes
print("\nπ Table sizes:")
for table in tables:
cursor.execute(f"SELECT COUNT(*) FROM {table}")
count = cursor.fetchone()[0]
print(f" {table}: {count:,} rows")
# Test some queries
print("\nπ Testing some queries:")
# Test a simple query
cursor.execute("SELECT COUNT(*) FROM users WHERE city = 'New York'")
ny_users = cursor.fetchone()[0]
print(f" Users in New York: {ny_users:,}")
# Test a slow query (missing index)
start_time = datetime.now()
cursor.execute("SELECT * FROM users WHERE age BETWEEN 25 AND 35 AND city = 'Los Angeles' LIMIT 10")
results = cursor.fetchall()
end_time = datetime.now()
query_time = (end_time - start_time).total_seconds()
print(f" Query time for age/city filter: {query_time:.3f}s")
# Test another slow query
start_time = datetime.now()
cursor.execute("SELECT * FROM products WHERE name LIKE '%laptop%' LIMIT 10")
results = cursor.fetchall()
end_time = datetime.now()
query_time = (end_time - start_time).total_seconds()
print(f" Query time for product search: {query_time:.3f}s")
else:
print("β οΈ observability_test database not found.")
print("Run: python setup_mysql_database.py to create it.")
# Update config
print("\nπ Updating config.json...")
try:
with open('config.json', 'r') as f:
config = json.load(f)
config['mysql']['host'] = host
config['mysql']['port'] = port
config['mysql']['user'] = user
config['mysql']['password'] = password
config['mysql']['database'] = 'observability_test' if 'observability_test' in databases else 'mysql'
with open('config.json', 'w') as f:
json.dump(config, f, indent=2)
print("β
Updated config.json with your MySQL settings")
except Exception as e:
print(f"β Error updating config: {e}")
cursor.close()
connection.close()
print("\n" + "="*50)
print("π MySQL Connection Test Complete!")
print("="*50)
print("You can now run:")
print(" python p3cli.py --monitor")
print(" python p3cli.py --analyze-queries")
print(" python p3cli.py --ask 'Why is my query slow?'")
except mysql.connector.Error as e:
print(f"β MySQL connection failed: {e}")
print("\nTroubleshooting tips:")
print("1. Make sure MySQL is running")
print("2. Check your username and password")
print("3. Verify the host and port are correct")
print("4. Ensure the user has necessary privileges")
if __name__ == "__main__":
test_mysql_connection()