Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect on remote MySQL database through Python

Tags:

I have tried the following script but unfortunately doesn't work. I am using a free MySQL database provider. Any ideas?

import MySQLdb

myDB = MySQLdb.connect(host="208.11.220.249",port=3306,user="XXXXX",passwd="XXXXX",db="XXXXX")
cHandler = myDB.cursor()
cHandler.execute("SHOW DATABASES")
results = cHandler.fetchall()
for items in results:
    print items[0]

Currently, I am getting the following error:

super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user 'XXXXX'@'%' to database 'XXXXX'")
like image 716
user706838 Avatar asked Oct 19 '12 11:10

user706838


People also ask

Can I connect Python to MySQL database?

Python needs a MySQL driver to access the MySQL database. In this tutorial we will use the driver "MySQL Connector". We recommend that you use PIP to install "MySQL Connector". PIP is most likely already installed in your Python environment.

Can I connect database from Python?

To create a connection between the MySQL database and Python, the connect() method of mysql. connector module is used. We pass the database details like HostName, username, and the password in the method call, and then the method returns the connection object.

Can MySQL be accessed remotely?

The MySQL server communicates only from the localhost by default. It can only be accessed by applications running on the same host. Remote access is necessary to remotely access the database from an application running on a different machine or host.


2 Answers

This is what I would do

  1. See if the port is actually open on that machine.
  2. On the machine you are connecting from, open console/cmd/terminal and see if you can connect using mysql -u XXXX -h 208.11.220.249 -p. If your mysql client can not connect, then there is no way you can connect using python
like image 25
Ranjith Ramachandra Avatar answered Oct 15 '22 22:10

Ranjith Ramachandra


 GRANT ALL
 ON  *.*
 TO [email protected]  -- client ip address
 IDENTIFIED BY 'pwd';

Edit

This is SQL that you'd run on the database in order to ensure that the user has access to everything. pwd is the user's password.

Basically, this answer assumes that the connection issue is a credentials issue.

like image 115
solaimuruganv Avatar answered Oct 15 '22 21:10

solaimuruganv