Python MySQL 错误:2013,“在‘读取初始通信数据包’时失去与 MySQL 服务器的连接,系统错误:0”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3578147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL error: 2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0"
提问by Scott R
I'm having an issue connecting to my local MySQL database using Python's MySQLdb library. The script has been working well previously, but I will occasionally get the MySQL error in the title. There seems to be no explanation for when the error occurs, and the script is always run from the same machine with the same arguments.
我在使用 Python 的 MySQLdb 库连接到本地 MySQL 数据库时遇到问题。该脚本以前运行良好,但我偶尔会在标题中看到 MySQL 错误。错误发生的时间似乎没有任何解释,并且脚本总是从具有相同参数的同一台机器上运行。
The MySQL server is running as a service on Windows XP SP3 using port 3306 (locally hosted phpMyAdmin works), and the script is run from an Ubuntu 10.04 guest operating system in Oracle VM VirtualBox.
MySQL 服务器在 Windows XP SP3 上作为服务运行,使用端口 3306(本地托管的 phpMyAdmin 工作),脚本从 Oracle VM VirtualBox 中的 Ubuntu 10.04 来宾操作系统运行。
I am currently working around this issue by opening a command prompt and executing 'net stop MySQL' then 'net start MySQL'. This allows me to run the script a few times again before resulting in the error, which I've been fixing by restarting the MySQL service.
我目前正在通过打开命令提示符并执行“net stop MySQL”然后“net start MySQL”来解决这个问题。这允许我在导致错误之前再次运行脚本几次,我已经通过重新启动 MySQL 服务修复了错误。
As I am still making changes to the script, there are occasions when the script raises an exception and doesn't exit gracefully, though I do catch the exception and close the cursor and connection.
由于我仍在对脚本进行更改,因此有时脚本会引发异常并且没有正常退出,尽管我确实捕获了异常并关闭了游标和连接。
The code to connect to the database:
连接数据库的代码:
def __init__(self):
try:
print "Connecting to the MySQL database..."
self.conn = MySQLdb.connect( host = "192.168.56.1",
user = "guestos",
passwd = "guestpw",
db = "testdb")
self.cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
print "MySQL Connection OK"
except MySQLdb.Error, e:
print "MySQLdb error %d: %s" % (e.args[0],e.args[1])
raise
The full error generated when this happens is as follows:
发生这种情况时产生的完整错误如下:
MySQLdb error 2013: Lost connection to MySQL server at 'reading initial communication packet', system error: 0
Traceback (most recent call last):
File "search.py", line 45, in <module>
dataHandler = DataHandler()
File "/home/guestos_user/workspace/Search/src/data_handler.py", line 25, in __init__
db = "testdb")
File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 170, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")
回答by Manoj Govindan
This bug reportmight be of interest to you. Don't know if this will help you, but some were able to solve it by using the nameof the server rather than the ip addressin the connection properties.
您可能会对这个错误报告感兴趣。不知道这是否对您有帮助,但有些人能够通过使用服务器名称而不是连接属性中的IP 地址来解决它。
回答by Jeremy Brown
I have seen this happen when child processes try to share the same mysql connection id (solution = create new connections for each child process). I'm not sure if this is also possible when sharing connection objects with multiple threads.
我已经看到当子进程尝试共享相同的 mysql 连接 ID(解决方案 = 为每个子进程创建新连接)时会发生这种情况。我不确定在与多个线程共享连接对象时这是否也可能。
However, that's only one of the many possible causes. See VVS's answer in MySQL Error 2013for a list of troubleshooting resources.
然而,这只是众多可能原因之一。有关故障排除资源的列表,请参阅MySQL 错误 2013 中VVS 的回答。
回答by Kean Tan
sudo vi /etc/mysql/my.cnf
delete
删除
bind-address = 127.0.0.1
then
然后
sudo reboot now
That's it. Be aware that this will make your mysql server less secure as you are exposing it.
就是这样。请注意,这会降低您的 mysql 服务器的安全性,因为您正在暴露它。
回答by darrell
I run a windows server and from time to time the php-win.exewill load and stay in the processes list on the windows task manager.
我运行 Windows 服务器,并且不时php-win.exe将加载并保留在 Windows 任务管理器上的进程列表中。
If you know the host file is correct, then kill the php-win.exeprocess and restart iis iisreset
如果你知道主机文件是正确的,那么杀死php-win.exe进程并重新启动iisiisreset
If you are running windows then your problem should be solved.
如果您正在运行 Windows,那么您的问题应该可以解决。
回答by Ashwin A
Could you change the bind-address=localhost and restart MySQL server? Seems like this issue is related to yours: http://forums.mysql.com/read.php?152,355740,355742#msg-355742
您可以更改 bind-address=localhost 并重新启动 MySQL 服务器吗?似乎这个问题与你的有关:http: //forums.mysql.com/read.php?152,355740,355742#msg-355742
Also this-
还有这个——
If MySQL port is wrong result is MySQL client error 2013 "Lost connection ...". Note that this error also occurs if port forwarding is disabled in SSH configuration (the configuration parameter 'AllowTcpForwarding' is set to 'no' in the 'sshd_config' file). It (here) simply tells that there is no connection from SSH to MySQL for some reason. But the mySQL client API 'thinks' there was one connection and that is why is says 'Lost connection ...' and not 'Can't connect...'. There was one successful connection - but not to the MySQL server - to the SSH daemon only! But the MySQL client API is not designed to 'see' the difference!
如果 MySQL 端口错误,结果是 MySQL 客户端错误 2013“Lost connection ...”。请注意,如果在 SSH 配置中禁用端口转发(在 'sshd_config' 文件中将配置参数 'AllowTcpForwarding' 设置为 'no'),也会发生此错误。它(这里)只是告诉我们出于某种原因没有从 SSH 到 MySQL 的连接。但是 mySQL 客户端 API“认为”有一个连接,这就是为什么说“丢失连接...”而不是“无法连接...”。有一个成功的连接 - 但不是连接到 MySQL 服务器 - 仅连接到 SSH 守护进程!但是 MySQL 客户端 API 并不是为了“看到”差异而设计的!
Refer this.
参考这个。
回答by Lucho
Do you have in your MySQL server an acount called guestos@YOURIPADDRESS?
You must have an account to access to your MySQL server from YOURIPADDRESS!
您的 MySQL 服务器中是否有一个名为 的帐户guestos@YOURIPADDRESS?您必须有一个帐户才能访问您的 MySQL 服务器YOURIPADDRESS!
For example:
Your IP address is 192.168.56.2; then you must create and account if not exist to access.
例如:您的 IP 地址是192.168.56.2; 那么您必须创建和帐户(如果不存在)才能访问。
mysql> create user [email protected] identified by 'guestpw';
回答by Pre Seznik
I've had the exact same mysql error (ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0=) and have resolved it by adding a newline to /etc/hosts.deny.
我遇到了完全相同的 mysql 错误(ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0=)并通过向 /etc/hosts.deny 添加换行符来解决它.
回答by Rockallite
Possibility: your database is corrupted.
可能性:您的数据库已损坏。
I encountered this situation when I was running an UPDATEstatement on a specific row of a specific table. (Specifically, I was editing an item in a Django Admin site.) Most of the time the database worked just fine.
我在UPDATE特定表的特定行上运行语句时遇到了这种情况。(具体来说,我正在编辑 Django 管理站点中的一个项目。)大多数时候数据库工作得很好。
I finally resolved the problem by running:
我终于通过运行解决了这个问题:
OPTIMIZE TABLE `your_table`
After that everything was OK, no connection lost.
之后一切正常,没有丢失连接。
Conclusion:
结论:
The problem "Lost connection to MySQL server at 'reading initial communication packet'", sometimes "Can't connect to MySQL server on '127.0.0.1'", could possibly be resolved by running a full database optimization if the database is corrupted. For more info, read this.
如果数据库损坏,可以通过运行完整的数据库优化来解决问题“ Lost connection to MySQL server at 'reading initial communication packet'”,有时是“ Can't connect to MySQL server on '127.0.0.1'”。有关更多信息,请阅读此。
回答by manpreet singh
The problem fixed for me just by restarting my mac. Though there might be a more specific fix for it.
只需重新启动我的 mac,问题就为我解决了。尽管可能有更具体的修复方法。
回答by Faisal Naqaweh
I received a similar error when attempting to connect to my MySQL server remotely through a user with the sufficient permissions.
尝试通过具有足够权限的用户远程连接到我的 MySQL 服务器时,我收到了类似的错误。
After editing the /etc/mysql/my.cnf file to include
编辑 /etc/mysql/my.cnf 文件后包含
[mysqld]
bind-address=xx.xx.xxx.xxx
where xx.xx.xxx.xxx is my local IP address, I began experiencing the exact same error as you. From there, I found an answer regarding this issue (answered by Coffee Converter) which worked for me, and can be found here: Lost connection to MySQL server at 'reading initial communication packet', system error: 0 on a windows machine
其中 xx.xx.xxx.xxx 是我的本地 IP 地址,我开始遇到与您完全相同的错误。从那里,我找到了一个关于这个问题的答案(由 Coffee Converter 回答)对我有用,可以在这里找到:Lost connection to MySQL server at 'reading initial communication packet', system error: 0 on a windows machine
All I did to fix the issue for myself was edit the /etc/hosts.allow to include
我为自己解决问题所做的只是编辑 /etc/hosts.allow 以包含
mysqld: ALL: allow
Works great now! I hope this helped :)
现在效果很好!我希望这有帮助:)

