[Python fill the pit connected mysql] using MySQL error ... (2006, "Can not connect to local MySQL server through socket '/tmp/mysql.sock' (2)") localhost connections

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.cnfto find the location of socket mysqld, and then to unix_socketpass 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'
     )

Guess you like

Origin www.cnblogs.com/coyote-waltz/p/11972902.html