Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL error: 2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0"

Tags:

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.

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.

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.

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")
like image 655
Scott R Avatar asked Aug 26 '10 18:08

Scott R


People also ask

How do I fix the lost connection to MySQL server at reading initial communication packet?

Check that MySQL is listening on port 3306 (note: 3306 is the default, but this can be changed) Check the user has rights to connect to the server IP from your address. Make sure you are both providing a password if needed and using the correct password for connecting from the host address you're connecting from.

Can not connect to MySQL server 10060?

The error 10060 is returned by the MySQL client when a connection could not be established with the service on the system you are connecting to. This is commonly caused by a firewall block or network issue preventing the connection.

Can't connect to local MySQL server through?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

What port do MySQL works upon?

Port 3306 is the default port for the classic MySQL protocol ( port ), which is used by the mysql client, MySQL Connectors, and utilities such as mysqldump and mysqlpump.


2 Answers

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.

like image 186
Kean Tan Avatar answered Sep 20 '22 20:09

Kean Tan


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.

However, that's only one of the many possible causes. See VVS's answer in MySQL Error 2013 for a list of troubleshooting resources.

like image 27
Jeremy Brown Avatar answered Sep 21 '22 20:09

Jeremy Brown