1

Can anyone help me on this issue while connecting DB over the python 3.5. following error receiving.

import pymysql

# Open database connection
db = pymysql.connect("localhost","testuser","test123","TESTDB", )

# 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()

Errors:

RESTART: C:/Program Files (x86)/Python35-32/db_connect.py Traceback (most recent call last): File "C:\Program Files (x86)\Python35-32\lib\site-packages\pymysql-0.7.11-py3.5.egg\pymysql\connections.py", line 916, in connect **kwargs) File "C:\Program Files (x86)\Python35-32\lib\socket.py", line 712, in create_connection raise err File "C:\Program Files (x86)\Python35-32\lib\socket.py", line 703, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:/Program Files (x86)/Python35-32/db_connect.py", line 4, in db = pymysql.connect("localhost","testuser","test123","TESTDB", ) File "C:\Program Files (x86)\Python35-32\lib\site-packages\pymysql-0.7.11-py3.5.egg\pymysql__init__.py", line 90, in Connect return Connection(*args, **kwargs) File "C:\Program Files (x86)\Python35-32\lib\site-packages\pymysql-0.7.11-py3.5.egg\pymysql\connections.py", line 706, in __ init __ self.connect() File "C:\Program Files (x86)\Python35-32\lib\site-packages\pymysql-0.7.11-py3.5.egg\pymysql\connections.py", line 963, in connect raise exc pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([WinError 10061] No connection could be made because the target machine actively refused it)")

2 Answers 2

1

From the help

The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server.

Check that you have mysql running and try connecting on the appropriate port (default is 3306)

Sign up to request clarification or add additional context in comments.

Comments

0

Your issue might be the extra comma with nothing at the end of your connection string as posted in your statement.

"TESTDB", )

It also may help to use the port # even if its the default.

Explicit is better than implicit.

Update:

According to the Docs when you don't provide a port it defaults to 0 (at least that is how i am reading it. Please correct me if I am wrong.)

class pymysql.connections.Connection(host=None, user=.... port=0,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.