Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO: MySQL server has gone away

Tags:

php

mysql

pdo

I have a script that does a lot of legwork nightly.

It uses a PDO prepared statement that executes in a loop.

The first few are running fine, but then I get to a point where they all fail with the error: "MySQL server has gone away".

We run MySQL 5.0.77.

PHP Version 5.2.12

The rest of the site runs fine.

like image 600
Nathan H Avatar asked Feb 09 '10 20:02

Nathan H


People also ask

Can not reconnect to MySQL after 1 attempt's ): MySQL server has gone away?

The MySQL server has gone away error, means that MySQL server (mysqld) timed out and closed the connection. By default, MySQL will close connections after eight hours (28800 seconds) if nothing happens.

Does PDO work with MySQL?

PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries.

What is PDO in MySQL?

PDO (PHP Data Objects) is a general database abstraction layer with support for MySQL among many other databases. It provides prepared statements, and significant flexibility in how data is returned.


1 Answers

Most likely you sent a packet to the server that is longer than the maximum allowed packet.

When you try to insert a BLOB that exceeds your server's maximum packet size, even on a local server you will see the following error message on clientside:

MySQL server has gone away

And the following error message in the server log: (if error logging is enabled)

Error 1153 Got a packet bigger than 'max_allowed_packet' bytes

To fix this, you need to decide what is the size of the largest BLOB that you will ever insert, and set max_allowed_packet in my.ini accordingly, for example:

[mysqld] ... max_allowed_packet = 200M ... 
like image 164
rustyx Avatar answered Sep 24 '22 00:09

rustyx