PHP Mysql PDO: General error: 2006 MySQL server has gone away

感情迁移 提交于 2021-02-04 21:10:31

问题


Im currently running a custom php script which when running on my localhost works fine however when running on shared hosting, i receive the following error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2006

MySQL server has gone away' in ...... 205 Stack trace:

0 ........ PDO->prepare('SELECT * FROM o...') #1 {main} thrown in .......... on line 205

(I've replaced the file paths with .......)

Things I have tried:

  1. Adding PDO::ATTR_TIMEOUT => "999999999999999999999", to extend any timeouts.

  2. Checked the max_allowed_packet (every answer ive found says this is the cause) My local machine is currently set to 1048576 (1mb) however my hosting is currently set to 268435456 (268mb)

I would understand if my local machine was 268mb and my hosting was 1mb but this doesn't make any sense that it is the cause of the problem, as this is not the case.

I did try to increase it on my hosting but as its shared I don't have the permissions to change this global variable.

Any other ideas of what I could try?

FYI Here is the script:

//MYSQL PREPARE STATEMENTS
//check to see if a product is in database
$mysql['productcheck'] = $mysql['pdo']->prepare('SELECT * FROM oc_product WHERE sku = ?');
$newproductcount=0;
$x = 0;
while(($maintable[$x]['status']=="active") || ($maintable[$x]['status']=="new") || ($maintable[$x]['status']=="discontinued") || ($maintable[$x]['status']=="archive")) {
    if(($maintable[$x]['status']=="active") || ($maintable[$x]['status']=="new")){
        //check to see if product exsists
        $params[1]=$maintable[$x]['euid'];
        $mysql['productcheck']->execute([$params[1]]);
        if($mysql['productcheck']->rowCount()==0){
            //PRODUCT DOESN'T EXSIST
            echo "Product doesnt exsists!";
            $newproductcount++;
        }
    }
$x++;
}

回答1:


Some other common reasons for the MySQL server has gone away error are:

  • You (or the db administrator) has killed the running thread with a KILL statement or a mysqladmin kill command.

  • You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected.

  • A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host.

  • You got a timeout from the TCP/IP connection on the client side. This may happen if you have been using the commands: mysql_options(..., MYSQL_OPT_READ_TIMEOUT,...) or mysql_options(..., MYSQL_OPT_WRITE_TIMEOUT,...). In this case increasing the timeout may help solve the problem.

  • You have encountered a timeout on the server side and the automatic reconnection in the client is disabled (the reconnect flag in the MYSQL structure is equal to 0).

  • You are using a Windows client and the server had dropped the connection (probably because wait_timeout expired) before the command was issued.

  • The problem on Windows is that in some cases MySQL does not get an error from the OS when writing to the TCP/IP connection to the server, but instead gets the error when trying to read the answer from the connection.

  • The solution to this is to either do a mysql_ping() on the connection if there has been a long time since the last query (this is what Connector/ODBC does) or set wait_timeout on the mysqld server so high that it in practice never times out.

  • You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 4MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section B.5.2.10, “Packet Too Large”.

  • An INSERT or REPLACE statement that inserts a great many rows can also cause these sorts of errors. Either one of these statements sends a single request to the server irrespective of the number of rows to be inserted; thus, you can often avoid the error by reducing the number of rows sent per INSERT or REPLACE.

  • You also get a lost connection if you are sending a packet 16MB or larger if your client is older than 4.0.8 and your server is 4.0.8 and above, or the other way around.

  • It is also possible to see this error if host name lookups fail (for example, if the DNS server on which your server or network relies goes down). This is because MySQL is dependent on the host system for name resolution, but has no way of knowing whether it is working—from MySQL's point of view the problem is indistinguishable from any other network timeout.

  • You may also see the MySQL server has gone away error if MySQL is started with the --skip-networking option.

  • Another networking issue that can cause this error occurs if the MySQL port (default 3306) is blocked by your firewall, thus preventing any connections at all to the MySQL server.

  • You can also encounter this error with applications that fork child processes, all of which try to use the same connection to the MySQL server. This can be avoided by using a separate connection for each child process.

  • You have encountered a bug where the server died while executing the query.

Check this link: Gone Away



来源:https://stackoverflow.com/questions/41133030/php-mysql-pdo-general-error-2006-mysql-server-has-gone-away

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!