sap
sap

Reputation: 319

Operational Error(2003, "Can't connect to MySQL server on '127.3.138.130' (111)")

I am trying to connect MySQL by using ip which I got from PhpMyAdmin using python. But I face Operational Error(2003, "Can't connect to MySQL server on '127.3.138.130' (111)")

I know how to use mysql to connect to localhost

I am following this tutorial

I have written following lines of code in python I am using ubuntu terminal to run python code and I do have mysql-server installed on my laptop.

import MySQLdb
db = MySQLdb.connect('127.3.138.130','my_username','my_password','my_db_name')

So what the problem is ? how to solve this problem ,please explain me in very simple manner. Thanks!

Upvotes: 1

Views: 18139

Answers (2)

Reut Sharabani
Reut Sharabani

Reputation: 31339

Make sure the server listens to outside requests. To do this go to /etc/mysql/my.cnf on the server and edit:

bind-address = 127.0.0.1

To:

bind-address = 0.0.0.0

You may need to use sudo when editing the file.

You will need to restart the server after changing the configuration:

sudo service mysql restart

You can also see what port is the server listening on (the default being 3306) in /etc/mysql/my.cnf. Just look for a line that says:

port = 3306

Make sure you're connecting to through same port, this is how you can specify a port:

db = MySQLdb.connect(
    host = '127.3.138.130', 
    user = 'my_username', 
    passwd = 'my_password', 
    db = 'my_db_name', 
    port = 3306 # should be same as in /etc/mysql/my.cnf on server.
)

Upvotes: 4

Sudip Das
Sudip Das

Reputation: 1208

use 'pymysql' lib...it may help u....

import pymysql
conn = pymysql.connect(host=''127.3.138.130', port=3306, user='root', passwd='password', db='dbname')

Upvotes: 0

Related Questions