问题是这样的,我在ubuntu上面安装了lampp,现在我已经启动了lampp,
lampp start
显示也一切正常
使用mysql连接正常
mysql -u root -p
使用MySQLdb连接
import MySQLdb db = MySQLdb.connect(db="test")
结果异常
Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__ super(Connection, self).__init__(*args, **kwargs2) _mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
异常显示来看,它去调用mysql默认安装位置的mysql.sock,但是我们使用lampp安装的,因此我们需要指定位置:
import MySQLdb db = MySQLdb.connect(db="test",unix_socket="/opt/lampp/var/mysql/mysql.sock")
ok一切正常了。
我是Huangacky,谢谢。