萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MySQL server has gone away解決辦法

MySQL server has gone away解決辦法

數據庫出現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 .

 


另供參考 解決MySQL server has gone away 

http://www.111cn.net/database/mysql/38459.htm

copyright © 萬盛學電腦網 all rights reserved