Can't connect to MySQL via my application
Linode
Linode Staff
Although I was able to connect to MySQL via the command line (using the mysql
command), my Python application throws the following error:
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '[sever_public_ip]:3306' (111)
1 Reply
evaldez
Linode Staff
By default MySQL is configured to allow connections to 127.0.0.1 (localhost). If you try to connect to it via the public IP, even within the Linode, MySQL won't like that. Here's a quick test I made on a fresh Wordpress Linode:
Using the public IP:
>>> cnx = mysql.connector.connect(user='root', password='random', host='173.230.157.121', database='wordpressdb')
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/connection_cext.py", line 232, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on '173.230.157.121:3306' (111)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/__init__.py", line 272, in connect
return CMySQLConnection(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/connection_cext.py", line 85, in __init__
self.connect(**kwargs)
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/abstracts.py", line 1003, in connect
self._open_connection()
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/connection_cext.py", line 235, in _open_connection
sqlstate=exc.sqlstate)
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '173.230.157.121:3306' (111)
Using localhost:
>>> cnx = mysql.connector.connect(user='root', password='random', host='127.0.0.1', database='wordpressdb')
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/connection_cext.py", line 232, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Access denied for user 'root'@'localhost'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/__init__.py", line 272, in connect
return CMySQLConnection(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/connection_cext.py", line 85, in __init__
self.connect(**kwargs)
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/abstracts.py", line 1003, in connect
self._open_connection()
File "/usr/local/lib/python3.7/dist-packages/mysql/connector/connection_cext.py", line 235, in _open_connection
sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied for user 'root'@'localhost'
Notice the errors are different. When using the public IP, I get the same error as you did. When using the localhost IP (you can use the string "localhost" as well instead of the IP, by the way), MySQL lets me in but rejects my attempt because the password is wrong.