我导入 MySQLdb 在 python 中使用,但出现以下错误:
File "test.py", line 76, in <module>
db = Database()
File "test.py", line 18, in __init__
self.connection = MySQLdb.connect(host = self.host, user = self.user, passwd = self.passwd, db = self.db)
File "/home/ue/.pythonbrew/pythons/Python-2.7.5/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/home/ue/.pythonbrew/pythons/Python-2.7.5/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, 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)")
Exception AttributeError: "Database instance has no attribute 'connection'" in <bound method Database.__del__ of <__main__.Database instance at 0xb74cf80c>> ignored
我看过其他帖子,但没有一个对我有帮助。我什至可以重新启动 mysql,因为我找不到 /var/run/mysqld/
我认为代码中没有错误,因为它来自 Mike Hibberts Python 视频讲座:http://www.youtube.com/watch?v=FFdNteScYe4&list=PLxxA5z-8B2xm1yUDAh2_pXGWBTePjCa3n
非常感谢您的帮助。
最佳答案
我认为,你应该先使用一些mysql客户端来检查你的mysql数据库。 #2002错误意味着它无法连接到mysql,我遇到过这个问题,因为我还没有启动mysql服务器。
关于python - Python 中的 MySQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20984271/