I\'m trying to connect to MySQL on localhost using PyMySQL:
import pymysql
conn = pymysql.connect(db=\'base\', user=\'
Two guesses:
Run mysqladmin variables | grep socket
to get where the socket is located, and try setting up a connection like so:
pymysql.connect(db='base', user='root', passwd='pwd', unix_socket="/tmp/mysql.sock")
Run mysqladmin variables | grep port
and verify that the port is 3306. If not, you can set the port manually like so:
pymysql.connect(db='base', user='root', passwd='pwd', host='localhost', port=XXXX)
I solved the issue by replacing localhost
with 127.0.0.1
and changing the password to my MYSQL database password as shown below;
conn = pymysql.connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = 'XXXXXXXXX',
db = 'mysql'
)
This worked for me:
import pymysql
db = pymysql.connect(host="localhost",port=8889,user="root",passwd="root")
cursor=db.cursor()
cursor.execute("SHOW DATABASES")
results=cursor.fetchall()
for result in results:
print (result)
if you want to find the port # go to mysql in terminal, and type:
SHOW VARIABLES WHERE Variable_name = 'hostname';
SHOW VARIABLES WHERE Variable_name = 'port';
You need to add the port to the connection as well. Try this and it works fine.
pymysql(Module Name).connect(host="localhost", user="root", passwd="root", port=8889, db="db_name")
Seems like changing localhost
to 127.0.0.1
fixes the error, at least in my configuration.
If it doesn't, I would look for errors in tcp sockets connection
and, of course, post it as a bug in pymysql
bugtrack.
I met the same question and my solution is as follows:
ssh -fN -L 3307:mysql_host:3306 ssh_user@ssh_host
in my terminal.conn = pymysql.connect(db='base', user='root', passwd='pwd', host='localhost')
This error occurs because database does not support link directly.