import pymysql
import MySQLdb
'''
db = MySQLdb.connect(host="13.209.68.158", # your host, usually localhost
user="Woo", # your username
passwd="cbnuroot123", # your password
db="CBNU") # name of the data base
# # you must create a Cursor object. It will let
# # you execute all the queries you need
cur = db.cursor()
# # Use all the SQL you like
cur.execute("SELECT * FROM User")
# # print all the first cell of all the rows
for row in cur.fetchall():
print (row[0])
db.close()
'''
# # Open database connection
db = pymysql.connect(host='13.209.68.158', user='access ID', password='access pw',
db ="Devilum",charset="utf8mb4")
# # prepare a cursor object using cursor() method
cursor = db.cursor()
# # execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# # Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# # disconnect from server
db.close()
Default mysql connection source. I thought it was a problem to use the library, so I tried to annotate and execute both pimysql and MySQLdb separately, but
Traceback (most recent call last):
File "C:\Users\any\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 958, in connect
**kwargs)
File "C:\Users\any\AppData\Local\Programs\Python\Python36\lib\socket.py", line 724, in create_connection
raise err
File "C:\Users\any\AppData\Local\Programs\Python\Python36\lib\socket.py", line 713, in create_connection
sock.connect(sa)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\any\Desktop\python\db2.py", line 25, in <module>
db ='CBNU',charset='utf8mb4')
File "C:\Users\any\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Users\any\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 704, in __init__
self.connect()
File "C:\Users\any\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 1005, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '13.209.68.158' (timed out)")
The following timeout error appears -_-; I thought it was a connection authority problem, so I tried to access it with root and gave it authority, but it still doesn't work What's the problem?crying The corresponding 13.209.68.158 ip is the ec2 server address supported by AWS.
python-3.x mysql aws
I guess it's a service created by rds of aws?
Basic network knowledge is required to use aws well.
By default, aws operate in a vpc environment.
By default, the service is not exposed to the outside world because it operates in a private environment.
You must explicitly open the service to the outside world.
You must open it externally using igw (Internet Gateway).
First, please learn services related to the vpc of aws (nat, subnet, igw).
© 2024 OneMinuteCode. All rights reserved.