北京SEO

MySQL server has gone away解决办法

2019/10/10/17:33:15  阅读:1775  来源:谷歌SEO算法  标签: SEO知识

数据库出现MySQL server has gone away这种问题根据我的经验,一是你程序向一个字段或一条语句中用超大的数据存储,但你的mysql的max_allowed_packet又没设置这么大的参考,所在就出现了这样的问题。

先要确认是不是你的空间商出问题,如果你是虚拟空间的话就先如下操作:

1、虚拟主机用户请联系空间商确认 MySQL 服务器是否正常,或者你的程序在运行过程中消耗了太多的服务器资源,请联系空间商进行确认;

2、独立主机用户请优化你的 MySQL 的配置,检查 MySQL 的运行情况,适当的时候增加服务器的配置。

3、因为执行动作过多,造成 MySQL 连接超时,如果是独立主机请修改 MySQL的配置文件中的 wait_timeout 这个值设置大一点.

如果你是自己的服务器可以如下操作:

1、应用程序(比如PHP)长时间的执行批量的MYSQL语句,最常见的就是采集或者新旧数据转化。

解决方案,在my.cnf文件中添加或者修改以下两个变量,代码如下:

  1. wait_timeout=2880000
  2. interactive_timeout=2880000

关于两个变量的具体说明可以google或者看官方手册,如果不能修改my.cnf,则可以在连接数据库的时候设置.

CLIENT_INTERACTIVE,比如:

  1. sql="setinteractive_timeout=24*3600";
  2. 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文件中添加或者修改以下两个变量:

  1. wait_timeout=2880000
  2. interactive_timeout=2880000

关于两个变量的具体说明可以google或者看官方手册,如果不能修改my.cnf,则可以在连接数据库的时候设置CLIENT_INTERACTIVE,代码如下:

  1. sql="setinteractive_timeout=24*3600";
  2. 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,代码如下:

  1. [mysqld]
  2. port=3306
  3. socket=/tmp/mysql.sock
  4. skip-locking
  5. key_buffer=384M
  6. max_allowed_packet=64M
  7. table_cache=4096
  8. sort_buffer_size=2M
  9. read_buffer_size=2M
  10. read_rnd_buffer_size=64M
  11. myisam_sort_buffer_size=64M
  12. thread_cache_size=8
  13. query_cache_size=32M
  14. InnoDBspecifications
  15. --phpfensi.com
  16. innodb_buffer_pool_size=384M
  17. innodb_additional_mem_pool_size=20M
  18. innodb_log_file_size=10M
  19. innodb_log_buffer_size=64M
  20. innodb_flush_log_at_trx_commit=1
  21. 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.

广告内容

MySQL server has gone away解决办法 MySQL server has gone away解决办法 MySQL server has gone away解决办法

相关阅读

热门评论

小明SEO博客 小明SEO博客

小明SEO博客,新时代SEO博客

总篇数171

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法