数据库出现MySQL server has gone away这种问题根据我的经验,一是你程序向一个字段或一条语句中用超大的数据存储,但你的mysql的max_allowed_packet又没设置这么大的参考,所在就出现了这样的问题。
先要确认是不是你的空间商出问题,如果你是虚拟空间的话就先如下操作:
1、虚拟主机用户请联系空间商确认 MySQL 服务器是否正常,或者你的程序在运行过程中消耗了太多的服务器资源,请联系空间商进行确认;
2、独立主机用户请优化你的 MySQL 的配置,检查 MySQL 的运行情况,适当的时候增加服务器的配置。
3、因为执行动作过多,造成 MySQL 连接超时,如果是独立主机请修改 MySQL的配置文件中的 wait_timeout 这个值设置大一点.
如果你是自己的服务器可以如下操作:
1、应用程序(比如PHP)长时间的执行批量的MYSQL语句,最常见的就是采集或者新旧数据转化。
解决方案,在my.cnf文件中添加或者修改以下两个变量,代码如下:
- wait_timeout=2880000
- interactive_timeout = 2880000
关于两个变量的具体说明可以google或者看官方手册,如果不能修改my.cnf,则可以在连接数据库的时候设置.
CLIENT_INTERACTIVE,比如:
- sql = "set interactive_timeout=24*3600";
- mysql_real_query(...)
2、执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段,比如,图片数据的处理.
解决方案:在my.cnf文件中添加或者修改以下变量,代码如下:
max_allowed_packet = 10M(也可以设置自己需要的大小)
max_allowed_packet 参数的作用是,用来控制其通信缓冲区的最大长度.
主要可能是因为以下几种原因:
一种可能是发送的SQL语句太长,以致超过了max_allowed_packet的大小,如果是这种原因,你只要修改my.cnf,加大max_allowed_packet的值即可。
还有一种可能是因为某些原因导致超时,比如说程序中获取数据库连接时采用了Singleton的做法,虽然多次连接数据库,但其实使用的都是同一个连接,而且程序中某两次操作数据库的间隔时间超过了wait_timeout(SHOW STATUS能看到此设置),那么就可能出现问题。最简单的处理方式就是把wait_timeout改大,当然你也可以在程序里时不时顺手mysql_ping()一下,这样MySQL就知道它不是一个人在战斗。
解决MySQL server has gone away.
1、应用程序,比如PHP,长时间的执行批量的MYSQL语句,最常见的就是采集或者新旧数据转化.
解决方案,在my.cnf文件中添加或者修改以下两个变量:
- wait_timeout=2880000
- interactive_timeout = 2880000
关于两个变量的具体说明可以google或者看官方手册,如果不能修改my.cnf,则可以在连接数据库的时候设置CLIENT_INTERACTIVE,代码如下:
- sql = "set interactive_timeout=24*3600";
- mysql_real_query(...)
2、执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段,比如,图片数据的处理.
解决方案,在my.cnf文件中添加或者修改以下变量,代码如下:
max_allowed_packet = 10M
也可以设置自己需要的大小.
max_allowed_packet:参数的作用是,用来控制其通信缓冲区的最大长度.
MySQL:诡异的MySQL server has gone away及其解决.
Introduction
Here is a step by step guide, equally valid for your Linux server as well as any local Windows MySQL installation you may be using as a trial installation along with your local Drupal installation.
MySQL comes with a default configuration of the resources it is going to use, specified in "my.ini" (Windows) or "my.cnf" (Linux) during the installation of MySQL. In Windows this file is located by default at C:Program FilesMySQLMySQL Server X.Ymy.ini. In Linux this file is located at /etc/my.cnf to set global options, or /usr/local/var/mysql-data-dir/my.cnf to set server-specific options.
Resources allowed by the default configuration are normally insufficient to run a resource-intensive application. You must modify the following resource specifications if they are available in your original configuration file, or add them to the configuration file if they are not already specified (because some are not present by default):
Important: Remember to keep backup files before you do anything! You will also have to reload the MySQL service after making changes to these configuration files.
MyISAM specifications,代码如下:
- [mysqld]
- port = 3306
- socket = /tmp/mysql.sock
- skip-locking
- key_buffer = 384M
- max_allowed_packet = 64M
- table_cache = 4096
- sort_buffer_size = 2M
- read_buffer_size = 2M
- read_rnd_buffer_size = 64M
- myisam_sort_buffer_size = 64M
- thread_cache_size = 8
- query_cache_size = 32M
- InnoDB specifications
-
- innodb_buffer_pool_size = 384M
- innodb_additional_mem_pool_size = 20M
- innodb_log_file_size = 10M
- innodb_log_buffer_size = 64M
- innodb_flush_log_at_trx_commit = 1
- innodb_lock_wait_timeout = 180
Note:It is assumed here that you are using the InnoDB database tables, as Drupal is a resource intensive application. If you are not using the InnoDB database tables try to change this, in view of the fact that you are getting the Warning: MySQL server has gone away - apparently meaning that your setup is resource intensive. Convert MyISAM Tables to InnoDB. |