Trying to connect to mysql through python using mysql-connector, but I am getting this error:
Traceback (most recent call last):
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/network.py", line 509, in open_connection
self.sock.connect(sockaddr)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./get_stf_file_names.py", line 26, in update_aws_script_details
database="service_portal"
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/abstracts.py", line 716, in connect
self._open_connection()
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/connection.py", line 206, in _open_connection
self._socket.open_connection()
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/network.py", line 512, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
I have checked if port 3306 is being used by mysqld and it is:
netstat -lnp | grep 3306
which gave:
tcp 0 0 my-ip:3306 0.0.0.0:* LISTEN 12003/mysqld
I also tried this command and it works perfectly:
mysql -h localhost -u root -p
(As in I can connect to the database using this)
I am trying to access the database in the following way:
mydb = mysql.connector.connect(host="localhost",user="****",
passwd="****",
database="**********"
)
cursor = mydb.cursor(dictionary=True)
If I change localhost in the above command to my-ip, then it works fine. Is there anything that I am doing wrong?
Localhost it's an ip 127.0.0.1.
Is your mysql instance listening on that exactly ip or on an external one?