Search code examples
pythonmysqlaws-security-groupmysql-connector-python

Python mysql connector change host link to ipaddress


I am coming across an issue with mysql connector.

I have an environment that i can't connect from my local machine.

I use simple command for connection:

from mysql.connector import connect

connection = mysql.connector.connect(user='dbuser', database='dbname', 
                                     host='amazon_link', password='dbpassword')

This works for all environment except one. And this also works in the server as well where the database lives.

I can access the database using my MySqlWorkbench from local machine. But when i try from my script, i get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 95, in __init__
    self.connect(**kwargs)
  File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect
    self._open_connection()
  File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 210, in _open_connection
    self._ssl)
  File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 144, in _do_auth
    self._auth_switch_request(username, password)
  File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 177, in _auth_switch_request
    raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'dbuser'@'10.0.1.72' (using password: YES)

And the ip address that i get for the host link is different from what mysql connector is showing here as well.

Is there a way to avoid mysql connector to change link to url here, as when i use the link instead of ip address in my Workbench i am able to connect. So i doubt it is an issue related to vpn or security groups. Otherwise i wouldn't be able to connect to database via work bench at all.

Any help would be appreciated. Thanks.

And sorry in case i missed something basic here.


Solution

  • Try to use the global IP address of your server instead of the domain name.

    Refer the stackoverflow link for more details: Remotely connect to MySQL with Python mysql.connector