Mysql connection failure
Environment: ubuntu18.04 + MySQL5.7
报错_mysql_exceptions.OperationalError: (2006, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
Very pit, if using localhost connections MySQL, actually use the UNIX Domain Socket to communicate, that is, the error prompt /tmp/mysql.sock this path. Did not see a bit of this socket, we supposed to do, one solution is from mysql configuration file /etc/mysql/mysql.conf.d/mysqld.cnf
to find the location of socket mysqld, and then to unix_socket
pass parameters to the connection of MySQLdb.
MySQLdb.connect(
host='localhost',
port=3306,
user='root',
password='password',
database='database',
charset='utf-8',
unix_socket='/var/run/mysqld/mysqld.sock'
)