Python connect to MySQL database on web server
Question: I wanted to know the process of connecting to a MySQL database that is hosted
on a web server.

I have a basic free webserver for testing on 000webhost on which I created a
MySQL database.

I have the credentials for the database which I will pretend are host -
mysql.webhost000.com user - dummy_user password - dummy_password database -
dummy_database

and I have a python script executing from my local computer with internet
access

    
    
    import MySQLdb
    
    db = MySQLdb.connect(host="mysql.webhost000.com",
                         port=3306,
                         user="dummy_user",
                         passwd="dummy_password",
                         db="dummy_database")
    

I was hoping it would connect as long as I have the right credentials but when
I execute the script it just hangs and once I quit it I see the error

    
    
    Can't connect to MySQL server on 'mysql.webhost000.com' (4)
    

Am I missing some steps?


Answer: There are two possible problems and im not able to recreate the first one. One
is the

    
    
    host="mysql.webhost000.com"
    

is incorrect and throwing an error. The connection could be listed as another
way. The other I noticed is this is usually how I set up my connection script.

import MySQLdb

    
    
    def connect():
        db = MySQLdb.connect(host="mysql.webhost000.com",
                         port=3306,
                         user="dummy_user",
                         passwd="dummy_password",
                         db="dummy_database")
    
        c = conn.cursor()
    
        return c, db
    

