MySQL查詢超時(shí)的設(shè)置方法
成都創(chuàng)新互聯(lián)是專業(yè)的漯河網(wǎng)站建設(shè)公司,漯河接單;提供網(wǎng)站制作、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行漯河網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
為了優(yōu)化OceanBase的query timeout設(shè)置方式,特調(diào)研MySQL關(guān)于timeout的處理,記錄如下。
[plain]
mysql show variables like '%time%';
+----------------------------+-------------------+
| Variable_name | Value |
+----------------------------+-------------------+
| connect_timeout | 10 |
| datetime_format | %Y-%m-%d %H:%i:%s |
| delayed_insert_timeout | 300 |
| flush_time | 1800 |
| innodb_lock_wait_timeout | 50 |
| innodb_old_blocks_time | 0 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lc_time_names | en_US |
| lock_wait_timeout | 31536000 |
| long_query_time | 10.000000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| slow_launch_time | 2 |
| system_time_zone | |
| time_format | %H:%i:%s |
| time_zone | SYSTEM |
| timed_mutexes | OFF |
| timestamp | 1366027807 |
| wait_timeout | 28800 |
+----------------------------+-------------------+
21 rows in set, 1 warning (0.00 sec)
重點(diǎn)解釋其中幾個(gè)參數(shù):
connect_timeout:
The number of seconds that the mysqld server waits for a connect packet before respondingwith Bad handshake. The default value is 10 seconds as of MySQL 5.1.23 and 5 seconds before that. Increasing the connect_timeout value might help if clients frequently encounter errors of the form Lost connection to MySQL server at ‘XXX’, system error: errno.
解釋:在獲取鏈接時(shí),等待握手的超時(shí)時(shí)間,只在登錄時(shí)有效,登錄成功這個(gè)參數(shù)就不管事了。主要是為了防止網(wǎng)絡(luò)不佳時(shí)應(yīng)用重連導(dǎo)致連接數(shù)漲太快,一般默認(rèn)即可。
interactive_timeout:
The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect(). See alsowait_timeout.
解釋:一個(gè)持續(xù)SLEEP狀態(tài)的線程多久被關(guān)閉。線程每次被使用都會(huì)被喚醒為acrivity狀態(tài),執(zhí)行完Query后成為interactive狀態(tài),重新開始計(jì)時(shí)。wait_timeout不同在于只作用于TCP/IP和Socket鏈接的線程,意義是一樣的。
MySQL可以配置連接的超時(shí)時(shí)間,這個(gè)時(shí)間如果做得太長,甚至到了10min,那么很可能發(fā)生這種情況,3000個(gè)鏈接都被占滿而且sleep在哪,新鏈接進(jìn)不來,導(dǎo)致無法正常服務(wù)。因此這個(gè)配置盡量配置一個(gè)符合邏輯的值,60s或者120s等等。
說人話:
命令行下面敲一個(gè)命令后,直至下一個(gè)命令到來之前的時(shí)間間隔為interactive_time,如果這個(gè)時(shí)間間隔超過了interactive_timeout,則連接會(huì)被自動(dòng)斷開,下一個(gè)命令失敗。不過一般的mysql客戶端都有自動(dòng)重連機(jī)制,下一個(gè)命令會(huì)在重連后執(zhí)行。
[sql]
mysql set interactive_timeout = 1;
Query OK, 0 rows affected (0.00 sec)
mysql show session variables like '%timeout%';
+----------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| connect_timeout | 10 |
| interactive_timeout | 1 |
| wait_timeout | 28800 |
+----------------------------+----------+
10 rows in set (0.00 sec)
=====
[sql]
mysql set wait_timeout = 1;
Query OK, 0 rows affected (0.00 sec)
【去泡杯茶,等會(huì)兒】
mysql show session variables like '%timeout%';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 7
Current database: *** NONE ***
+----------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| connect_timeout | 10 |
| interactive_timeout | 28800 |
| wait_timeout | 28800 |
+----------------------------+----------+
10 rows in set (0.01 sec)
wait_timeout:
The number of seconds the server waits for activity on a noninteractive connection (連接上沒有活動(dòng)命令,可能是客戶端喝咖啡去了。)before closing it. Before MySQL 5.1.41, this timeout applies only to TCP/IP connections, not to connections made through Unix socket files, named pipes, or shared memory.
On thread startup, the session wait_timeout value is initialized from the global wait_timeout value or from the global interactive_timeout value, depending on the type of client
這里順帶解釋一下什么是non-interactive connection
Non-Interactive Commands
Just do a quick look up on a table without logging into the client, running the query then logging back out again.
You can instead just type one line using the ' -e ' flag.
[sql]
c:\mysql\bin\mysql -u admin -p myDatabase -e 'SELECT * FROM employee'
net_read_timeout / net_write_timeout
The number of seconds to wait for more data from a connection before aborting the read. Before MySQL 5.1.41, this timeout applies only to TCP/IP connections, not to connections made through Unix socket files, named pipes, or shared memory. When the server is reading from the client, net_read_timeout is the timeout value controlling when to abort. When the server is writing to the client, net_write_timeout is the timeout value controlling when to abort. See also slave_net_timeout.
On Linux, the NO_ALARM build flag affects timeout behavior as indicated in the description of the net_retry_count system variable.
解釋:這個(gè)參數(shù)只對TCP/IP鏈接有效,分別是數(shù)據(jù)庫等待接收客戶端發(fā)送網(wǎng)絡(luò)包和發(fā)送網(wǎng)絡(luò)包給客戶端的超時(shí)時(shí)間,這是在Activity狀態(tài)下的線程才有效的參數(shù)
JDBC setQueryTimeout函數(shù):
為了避免查詢出現(xiàn)死循環(huán),或時(shí)間過長等現(xiàn)象,而導(dǎo)致線程阻塞,在獲得Statement的實(shí)例后,stmt.setQueryTimeout(10); 避免因?yàn)椴樵儗?dǎo)致程序出現(xiàn)線程阻塞。
但昨天發(fā)現(xiàn)程序出現(xiàn)了,“ORA-01013: 用戶請求取消當(dāng)前的操作”的異常。手工執(zhí)行出錯(cuò)SQL語句發(fā)現(xiàn),這個(gè)語句耗時(shí)20多秒。因?yàn)閟etQueryTimeout(10),所以還沒有執(zhí)行完查詢語句就拋出異常了。使用setQueryTimeout(10)時(shí)一定要把時(shí)間設(shè)置的長一些,如60秒以上。只要不導(dǎo)致線程長期阻塞,就可以。太短了容易拋出,“ORA-01013: 用戶請求取消當(dāng)前的操作”的異常
JDBC實(shí)現(xiàn)setQueryTimeout的原理:
[java]
class IfxCancelQueryImpl extends TimerTask
implements IfmxCancelQuery
{
IfxStatement stmt;
Timer t = null;
public void startCancel(IfxStatement paramIfxStatement, int paramInt)
throws Exception
{
this.stmt = paramIfxStatement;
this.t = new Timer(true);
this.t.schedule(this, paramInt * 1000);
}
public void run()
{
try
{
this.stmt.cancel();
this.t.cancel();
}
catch (SQLException localSQLException)
{
this.t.cancel();
throw new Error(localSQLException.getErrorCode() + ":" + localSQLException.getMessage());
}
}
}
大規(guī)模多線程操作事務(wù)的時(shí)候,有時(shí)候打開一個(gè)鏈接,會(huì)進(jìn)行等待,這時(shí)候如果數(shù)據(jù)庫的超時(shí)時(shí)間設(shè)置的過短,就可能會(huì)出現(xiàn),數(shù)據(jù)鏈接自動(dòng)被釋放,當(dāng)然設(shè)置過大也不好,慢SQL或其他因素引起的鏈接過長,導(dǎo)致整個(gè)系統(tǒng)被拖慢,甚至掛掉。SO,適當(dāng)?shù)脑O(shè)置超時(shí)時(shí)間。設(shè)置方法:
SHOW GLOBAL VARIABLES LIKE '%timeout%'
SET GLOBAL wait_timeout=10000
網(wǎng)頁鏈接
MYSQL_OPT_READ_TIMEOUT 是 MySQL c api 客戶端中用來設(shè)置讀取超時(shí)時(shí)間的參數(shù)。在 MySQL 的官方文檔中,該參數(shù)的描述是這樣的:
MYSQL_OPT_READ_TIMEOUT (argument type: unsigned int *)The timeout in seconds for each attempt to read from the server. There are retries if necessary, so the total effective timeout value is three times the option value. You can set the value so that a lost connection can be detected earlier than the TCP/IPClose_Wait_Timeout value of 10 minutes.
也就是說在需要的時(shí)候,實(shí)際的超時(shí)時(shí)間會(huì)是設(shè)定值的 3 倍。但是實(shí)際測試后發(fā)現(xiàn)實(shí)際的超時(shí)時(shí)間和設(shè)置的超時(shí)時(shí)間一致。
而具體什么時(shí)候發(fā)生三倍超時(shí),在文檔中沒有找到。所以對 MySQL 5.7.20 的源碼進(jìn)行了一些分析。
使用 GDB 調(diào)試代碼找了實(shí)際與 mysql server 通信的代碼,如下:
請點(diǎn)擊輸入圖片描述
其中 vio_read() 函數(shù)中,使用 recv 和 poll 來讀取報(bào)文和做讀取超時(shí)。net_should_retry() 函數(shù)只有在發(fā)生 EINTR 時(shí)才會(huì)返回 true。從這段代碼來看是符合測試結(jié)果的,并沒有對讀取進(jìn)行三次重試。只有在讀取操作被系統(tǒng)中斷打斷時(shí)才會(huì)重試,但是這個(gè)重試并沒有次數(shù)限制。
從上面代碼的分析可以看出,代碼的邏輯和文檔的描述不符。于是在一頓搜索后,找到了一個(gè) MySQL 的 BUG(Bug #31163)。該 BUG 報(bào)告了在?MySQL?5.0 中,MySQL c api 讀取的實(shí)際超時(shí)時(shí)間是設(shè)置的三倍,與現(xiàn)有文檔描述相符。于是對 MySQL 5.0.96 的代碼又進(jìn)行分析。
同樣使用 GDB 找到了通信部分的代碼。這次找到了重試三次的代碼,如下:
請點(diǎn)擊輸入圖片描述
這個(gè)版本的 MySQL api 的讀寫超時(shí)是直接使用的 setsockopt 設(shè)置的。第一次循環(huán),在 A 點(diǎn)發(fā)生了第一次超時(shí)(雖然注釋寫的非阻塞,但是客戶端的連接始終是阻塞模式的)。然后在 B 點(diǎn)將該 socket 設(shè)置為阻塞模式,C 點(diǎn)這里重置 retry 次數(shù)。由于設(shè)置了 alarm 第二次以后的循環(huán)會(huì)直接進(jìn)入 D 點(diǎn)的這個(gè)分支,并且判斷循環(huán)次數(shù)。作為客戶端時(shí)net-retry_count 始終是 1,所以重試了兩次,共計(jì)進(jìn)行了 3 次 vioread 后從 E 點(diǎn)退出函數(shù)。
由上面的分析可知,MySQL 文檔對于該參數(shù)的描述已經(jīng)過時(shí),現(xiàn)在的 MYSQL_OPT_READ_TIMEOUT 并不會(huì)出現(xiàn)三倍超時(shí)的問題。而 Bug #31163 中的處理結(jié)果也是將文檔中該參數(shù)的描述更新為實(shí)際讀取超時(shí)時(shí)間是設(shè)定時(shí)間的三倍。也許是 MySQL 的維護(hù)者們在后續(xù)版本更新時(shí)忘記更新文檔吧。
connect_timeout:連接響應(yīng)超時(shí)時(shí)間。服務(wù)器端在這個(gè)時(shí)間內(nèi)如未連接成功,則會(huì)返回連接失敗。
wait_timeout:連接空閑超時(shí)時(shí)間。與服務(wù)器端無交互狀態(tài)的連接,直到被服務(wù)器端強(qiáng)制關(guān)閉而等待的時(shí)間??梢哉J(rèn)為是服務(wù)器端連接空閑的時(shí)間,空閑超過這個(gè)時(shí)間將自動(dòng)關(guān)閉。
interactive_timeout :連接空閑超時(shí)時(shí)間。與服務(wù)器端無交互狀態(tài)的連接,直到被服務(wù)器端強(qiáng)制關(guān)閉而等待的時(shí)間。
interactive_timeout和wait_timeoutu意義雖然相同,但是有使用對象有本質(zhì)的區(qū)別。interactive_timeout針對交互式連接(比如通過mysql客戶端連接數(shù)據(jù)庫),wait_timeout針對非交互式連接(比如一般在PHP中使用PDO連接數(shù)據(jù)庫,當(dāng)然你可以設(shè)置CLIENT_INTERACTIVE選項(xiàng)來改變)。所謂的交互式連接,即在mysql_real_connect()函數(shù)中使用了CLIENT_INTERACTIVE選項(xiàng)。
net_read_timeout :數(shù)據(jù)讀取超時(shí)時(shí)間。在終止讀之前,從一個(gè)連接獲得數(shù)據(jù)而等待的時(shí)間秒數(shù);當(dāng)服務(wù)正在從客戶端讀取數(shù)據(jù)時(shí),net_read_timeout控制何時(shí)超時(shí)。即客戶端執(zhí)行數(shù)據(jù)讀取,等待多少秒仍未執(zhí)行成功時(shí)自動(dòng)斷開連接。
net_write_timeout:數(shù)據(jù)庫寫超時(shí)時(shí)間。和net_read_timeout意義類似,在終止寫之前,等待多少秒把block寫到連接;當(dāng)服務(wù)正在寫數(shù)據(jù)到客戶端時(shí),net_write_timeout控制何時(shí)超時(shí)。
slave-net-timeout:從庫延后同步的時(shí)間,當(dāng)slave認(rèn)為連接master的連接有問題時(shí),就等待N秒,然后斷開連接,重新連接master
slave-net-timeout在主從同步時(shí)從庫上起作用;connect_timeout:在獲取連接階段起作用;interactive_timeout和wait_timeout:在連接空閑階段起作用;net_read_timeout和net_write_timeout:則是在連接執(zhí)行時(shí)起作用。
mysql命令
查看mysql server超時(shí)時(shí)間:
msyql show global variables like '%timeout%';
設(shè)置mysql server超時(shí)時(shí)間(以秒為單位):
msyql set global wait_timeout=10;
msyql set global interactive_timeout=10;
?php
$db-query("SET interactive_timeout = 3600;");
$db-query("SET wait_timeout = 3600;");
?
新聞名稱:MySQL怎么設(shè)置超時(shí) mysql連接超時(shí)時(shí)間查詢
當(dāng)前URL:http://chinadenli.net/article6/dodshig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、外貿(mào)網(wǎng)站建設(shè)、、營銷型網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)