Search code examples
pythonmysqlshared-hosting

How to access MySQL Database on a Bluehost server remotely through Python


I currently have a Python connector script that I run on my Bluehost server locally. In the past, I've tried to run this script remotely, i.e. login remotely with the Python script on my own local machine, but I ended up just running it on the server. In my current situation, I need to run a script every 10 minutes on the server, but I don't have root access, so I can't create a cron job. I can, however, set up a Windows task to run my script every 10 minutes remotely, but when I try, I'm unable to connect to my domain's database. Is there any documentation or work around for this?

Here's my code for connecting to the database remotely:

cnx = mysql.connector.connect(user='database_user', 
    password='database_password',
    host='www.example.com', 
    database='database_name')

When I try to use this script remotely, I get the following error message:

InterfaceError: 2003: Can't connect to the MySQL server on 'example.com:3306' 
(10061 No connection could be made because the target machine actively refused it)

Solution

  • You have to enable access for the IP address of the computer you are trying to access the server from. By default the database server doesn't allow outside connections. You'll do this by logging into your host (Bluehost)

    1. Log into cPanel and click the Remote MySQL icon, under Databases.
    2. Type in the connecting IP address, and click the Add Host button.
    3. Click Add, and you should now be able to connect remotely to your database.