时与MySQL服务器的连接丢失

时与MySQL服务器的连接丢失

本文介绍了MySQL错误:2013年,“在“读取初始通信数据包"时与MySQL服务器的连接丢失,系统错误:0";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python的MySQLdb库连接到本地MySQL数据库时遇到问题.该脚本以前一直运行良好,但偶尔会在标题中出现MySQL错误.似乎没有任何关于何时发生错误的解释,并且脚本总是从具有相同参数的同一台计算机上运行.

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.

MySQL服务器使用端口3306在Windows XP SP3上作为服务运行(本地托管的phpMyAdmin工作),并且脚本是从Oracle VM VirtualBox中的Ubuntu 10.04来宾操作系统运行的.

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.

我目前正在解决此问题,方法是打开命令提示符并执行"net stop MySQL",然后执行"net start MySQL".这样,我就可以在导致错误之前再次运行该脚本几次,该错误已通过重新启动MySQL服务进行了修复.

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.

由于我仍在对脚本进行更改,因此有时脚本会引发异常并且不能正常退出,尽管我确实捕获了异常并关闭了游标和连接.

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.

连接到数据库的代码:

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")

推荐答案

sudo vi /etc/mysql/my.cnf

删除

bind-address = 127.0.0.1

然后

sudo reboot now

就是这样.请注意,这将使您的mysql服务器在暴露时不那么安全.

That's it. Be aware that this will make your mysql server less secure as you are exposing it.

这篇关于MySQL错误:2013年,“在“读取初始通信数据包"时与MySQL服务器的连接丢失,系统错误:0";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:10