執行SQL查詢導致磁碟耗盡故障演示

来源:https://www.cnblogs.com/aaron8219/archive/2018/06/04/9135098.html
-Advertisement-
Play Games

a fellow in IMG wechat group 2 met an error about running out of disk space when using MySQL query today,now let's mimic the situation: first,i'll cre ...


 

    a fellow in IMG wechat group 2 met an error about running out of disk space when using MySQL query today,now let's mimic the situation:     first,i'll create a test table and a procedure,then using the procedure to insert 1000W records into test table "test":  
(root@localhost mysql3306.sock)[zlm]08:46:36>create table test(
    -> id int unsigned not null,  --Notice,there is no primary key and any other key here.
    -> name char(50) not null default ''
    -> ) engine=innodb charset=utf8;
Query OK, 0 rows affected (0.01 sec)

(root@localhost mysql3306.sock)[zlm]08:46:45>delimiter $$
(root@localhost mysql3306.sock)[zlm]08:46:54>create procedure pro_test()
    -> begin
    -> declare i int;
    -> set i=10000000;
    -> while i>0 do
    -> insert into test(id,name) values (i,'aaron8219');
    -> set i=i-1;
    -> end while;
    -> end $$
Query OK, 0 rows affected (0.00 sec)

(root@localhost mysql3306.sock)[zlm]08:46:54>delimiter ;
(root@localhost mysql3306.sock)[zlm]08:47:13>call pro_test;
^C^C -- query aborted  --I'm afraid the disk space will be insufficient,so i cancel the procedure by Ctrl+C.
Terminated

 

    it's the incremental change result of the command "df -h" output below at the begining to the point that i cancel insertation:

 

 1 [root@zlm3 08:47:20 /data/mysql/mysql3306]
 2 #df -h
 3 Filesystem               Size  Used Avail Use% Mounted on
 4 /dev/mapper/centos-root  8.4G  5.1G  3.4G  60% /
 5 devtmpfs                 488M     0  488M   0% /dev
 6 tmpfs                    497M     0  497M   0% /dev/shm
 7 tmpfs                    497M  6.6M  491M   2% /run
 8 tmpfs                    497M     0  497M   0% /sys/fs/cgroup
 9 /dev/sda1                497M  118M  379M  24% /boot
10 none                      87G   76G   11G  88% /vagrant
11 
12 [root@zlm3 08:47:22 /data/mysql/mysql3306]
13 #df -h
14 Filesystem               Size  Used Avail Use% Mounted on
15 /dev/mapper/centos-root  8.4G  5.8G  2.6G  70% /
16 devtmpfs                 488M     0  488M   0% /dev
17 tmpfs                    497M     0  497M   0% /dev/shm
18 tmpfs                    497M  6.6M  491M   2% /run
19 tmpfs                    497M     0  497M   0% /sys/fs/cgroup
20 /dev/sda1                497M  118M  379M  24% /boot
21 none                      87G   76G   11G  88% /vagrant
22 
23 [root@zlm3 08:51:27 /data/mysql/mysql3306]
24 #df -h
25 Filesystem               Size  Used Avail Use% Mounted on
26 /dev/mapper/centos-root  8.4G  6.2G  2.3G  74% /
27 devtmpfs                 488M     0  488M   0% /dev
28 tmpfs                    497M     0  497M   0% /dev/shm
29 tmpfs                    497M  6.6M  491M   2% /run
30 tmpfs                    497M     0  497M   0% /sys/fs/cgroup
31 /dev/sda1                497M  118M  379M  24% /boot
32 none                      87G   76G   11G  88% /vagrant
33 
34 [root@zlm3 08:53:06 /data/mysql/mysql3306]
35 #df -h
36 Filesystem               Size  Used Avail Use% Mounted on
37 /dev/mapper/centos-root  8.4G  7.0G  1.4G  84% /  -- The free disk space became 16% (maybe less) when i cancel the operation.
38 devtmpfs                 488M     0  488M   0% /dev
39 tmpfs                    497M     0  497M   0% /dev/shm
40 tmpfs                    497M  6.6M  491M   2% /run
41 tmpfs                    497M     0  497M   0% /sys/fs/cgroup
42 /dev/sda1                497M  118M  379M  24% /boot
43 none                      87G   77G  9.6G  89% /vagrant
44 
45 [root@zlm3 08:57:30 /data/mysql/mysql3306]
46 #ps -ef|grep mysql
47 mysql     6031     1 12 08:00 ?        00:07:05 mysqld --defaults-file=/data/mysql/mysql3306/my.cnf
48 root      6182  6157  0 08:46 pts/0    00:00:00 mysql
49 root      6389  6085  0 08:58 pts/1    00:00:00 grep --color=auto mysql
50 
51 [root@zlm3 08:58:13 /data/mysql/mysql3306]
52 #kill 6182  -- After cancel the operation,i kill the mysql process in this session.
53 
54 [root@zlm3 08:58:22 /data/mysql/mysql3306]
55 #ps -ef|grep mysql
56 mysql     6031     1 12 08:00 ?        00:07:17 mysqld --defaults-file=/data/mysql/mysql3306/my.cnf
57 root      6403  6085  0 08:58 pts/1    00:00:00 grep --color=auto mysql
58 
59 [root@zlm3 08:58:29 /data/mysql/mysql3306]
60 #

 

    in the first session,relogin with mysql client,check the status of the table test:

 

 1 #mysql
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 7
 4 Server version: 5.7.21-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 (root@localhost mysql3306.sock)[(none)]08:58:35>use zlm;
15 Reading table information for completion of table and column names
16 You can turn off this feature to get a quicker startup with -A
17 
18 Database changed
19 (root@localhost mysql3306.sock)[zlm]08:59:08>show tables;
20 +---------------+
21 | Tables_in_zlm |
22 +---------------+
23 | t1            |
24 | t2            |
25 | t3            |
26 | test          |
27 +---------------+
28 4 rows in set (0.00 sec)
29 
30 (root@localhost mysql3306.sock)[zlm]09:00:49>select table_schema,concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size,
31     -> concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
32     -> from information_schema.tables
33     -> where table_schema='zlm' and table_name='test';
34 +--------------+-----------+------------+
35 | table_schema | data_size | index_size |
36 +--------------+-----------+------------+
37 | zlm          | 656.00MB  | 0.00MB     |  --Now the table "test" has alread been a big table,the size turned into 656Mb.
38 +--------------+-----------+------------+
39 1 row in set (0.10 sec)
40 
41 (root@localhost mysql3306.sock)[zlm]09:01:34>select count(*) from test;
42 +----------+
43 | count(*) |
44 +----------+
45 |  9070823 | --Almost 1000W records around.
46 +----------+
47 1 row in set (19.16 sec)
48 
49 (root@localhost mysql3306.sock)[zlm]09:02:29>show master status;
50 +------------------+-----------+--------------+------------------+-------------------------------------------------+
51 | File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                               |
52 +------------------+-----------+--------------+------------------+-------------------------------------------------+
53 | mysql-bin.000057 | 244082592 |              |                  | 5c77c31b-4add-11e8-81e2-080027de0e0e:1-13527727 |
54 +------------------+-----------+--------------+------------------+-------------------------------------------------+
55 1 row in set (0.06 sec)
56 
57 (root@localhost mysql3306.sock)[zlm]09:12:58>

 

    let's check the binlog file,these files really ocuppied a huge amount disk space:

 

[root@zlm3 08:58:42 /data/mysql/mysql3306/logs]
#ls -l
total 2384300
-rw-r----- 1 mysql mysql      3831 May 28 03:20 mysql-bin.000019
-rw-r----- 1 mysql mysql      9564 May 28 11:37 mysql-bin.000020
-rw-r----- 1 mysql mysql      4761 May 29 11:27 mysql-bin.000021
-rw-r----- 1 mysql mysql       217 May 30 11:29 mysql-bin.000022
-rw-r----- 1 mysql mysql       217 May 31 03:20 mysql-bin.000023
-rw-r----- 1 mysql mysql       613 May 31 03:29 mysql-bin.000024
-rw-r----- 1 mysql mysql      1009 May 31 03:35 mysql-bin.000025
-rw-r----- 1 mysql mysql       217 May 31 03:36 mysql-bin.000026
-rw-r----- 1 mysql mysql       217 May 31 03:37 mysql-bin.000027
-rw-r----- 1 mysql mysql       217 May 31 03:38 mysql-bin.000028
-rw-r----- 1 mysql mysql       217 May 31 03:40 mysql-bin.000029
-rw-r----- 1 mysql mysql      1563 May 31 03:45 mysql-bin.000030
-rw-r----- 1 mysql mysql       217 May 31 06:50 mysql-bin.000031
-rw-r----- 1 mysql mysql       217 May 31 06:59 mysql-bin.000032
-rw-r----- 1 mysql mysql       217 May 31 07:04 mysql-bin.000033
-rw-r----- 1 mysql mysql       217 May 31 07:13 mysql-bin.000034
-rw-r----- 1 mysql mysql       217 May 31 07:15 mysql-bin.000035
-rw-r----- 1 mysql mysql       217 May 31 07:42 mysql-bin.000036
-rw-r----- 1 mysql mysql       461 May 31 08:22 mysql-bin.000037
-rw-r----- 1 mysql mysql       217 May 31 08:25 mysql-bin.000038
-rw-r----- 1 mysql mysql       613 May 31 10:37 mysql-bin.000039
-rw-r----- 1 mysql mysql       369 May 31 10:41 mysql-bin.000040
-rw-r----- 1 mysql mysql       613 May 31 11:28 mysql-bin.000041
-rw-r----- 1 mysql mysql      3141 Jun  1 10:10 mysql-bin.000042
-rw-r----- 1 mysql mysql      5677 Jun  1 11:38 mysql-bin.000043
-rw-r----- 1 mysql mysql       217 Jun  4 07:54 mysql-bin.000044
-rw-r----- 1 mysql mysql       194 Jun  4 07:57 mysql-bin.000045
-rw-r----- 1 mysql mysql       217 Jun  4 07:57 mysql-bin.000046
-rw-r----- 1 mysql mysql       217 Jun  4 11:23 mysql-bin.000047
-rw-r----- 1 mysql mysql 268435609 Jun  5 08:48 mysql-bin.000048
-rw-r----- 1 mysql mysql 268435737 Jun  5 08:50 mysql-bin.000049
-rw-r----- 1 mysql mysql 268435737 Jun  5 08:52 mysql-bin.000050
-rw-r----- 1 mysql mysql 268435737 Jun  5 08:54 mysql-bin.000051
-rw-r----- 1 mysql mysql 268435737 Jun  5 08:55 mysql-bin.000052
-rw-r----- 1 mysql mysql 268435737 Jun  5 08:57 mysql-bin.000053
-rw-r----- 1 mysql mysql 268435737 Jun  5 08:58 mysql-bin.000054
-rw-r----- 1 mysql mysql 268435737 Jun  5 09:00 mysql-bin.000055
-rw-r----- 1 mysql mysql 268435737 Jun  5 09:02 mysql-bin.000056  --From binlog 48 to 56,each one exhausted the max size at 256M.
-rw-r----- 1 mysql mysql  25366220 Jun  5 09:02 mysql-bin.000057  --This is the last binlog file contain the last several records.
-rw-r----- 1 mysql mysql      1716 Jun  5 09:02 mysql-bin.index

[root@zlm3 09:09:43 /data/mysql/mysql3306/logs]
#mysqlbinlog -vv --base64-output=decode-rows mysql-bin.000057| tail -20
#180605  9:03:45 server id 1023306  end_log_pos 244082462 CRC32 0x9db85944     Query    thread_id=5    exec_time=0    error_code=0
SET TIMESTAMP=1528182225/*!*/;
BEGIN
/*!*/;
# at 244082462
#180605  9:03:45 server id 1023306  end_log_pos 244082511 CRC32 0x8bb85bae     Table_map: `zlm`.`test` mapped to number 104
# at 244082511
#180605  9:03:45 server id 1023306  end_log_pos 244082561 CRC32 0x6ede8301     Write_rows: table id 104 flags: STMT_END_F
### INSERT INTO `zlm`.`test`
### SET
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2='aaron8219' /* STRING(150) meta=65174 nullable=0 is_null=0 */
# at 244082561
#180605  9:03:45 server id 1023306  end_log_pos 244082592 CRC32 0xf983b21b     Xid = 30000069  --The "end_log_pos 244082592" is equal with the output result of the postion in "show master status;" command.
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/; --GTID_NEXT='AUTOMATIC' means there're no more sequential binlogs behind.
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

[root@zlm3 09:11:21 /data/mysql/mysql3306/logs]
#

 

    now,let's do some testing query below(using distinct/group by):

 

1 (root@localhost mysql3306.sock)[zlm]09:16:31>select count(distinct(id)) from test group by name;
2 ERROR 3 (HY000): Error writing file '/data/mysql/mysql3306/tmp/MYCUMGkU' (Errcode: 28 - No space left on device)
3 (root@localhost mysql3306.sock)[zlm]09:18:34>system ps aux|grep mysql
4 root      6420  0.0  0.2 134112  2292 pts/0    S+   08:58   0:00 mysql
5 root      6667  0.0  0.1 113116  1360 pts/0    S+   09:17   0:00 sh -c  ps aux|grep mysql
6 root      6669  0.0  0.0 112640   944 pts/0    R+   09:17   0:00 grep mysql
7 (root@localhost mysql3306.sock)[zlm]10:22:13>

 

    eventually,the query ended with error "Errcode:28 - no space left on device",obviously the mysqld process has dead now.check the disk space,it's 99% in Use% column,why does the disk space continuously increase?let's see the official document about this bellow:

 

 


8.4.4 Internal Temporary Table Use in MySQL

In some cases, the server creates internal temporary tables while processing statements. Users have no direct control over when this occurs.

The server creates temporary tables under conditions such as these:


來源: https://dev.mysql.com/doc/refman/8.0/en/internal-temporary-tables.html

 

    here's the conclusion,whenever above conditions occured,it will induce MySQL server to do sort operation in memory or in disk(this is too bad thing),hence,in our test above,the disk space was exhausted soon.

 

  1 [root@zlm3 08:58:29 /data/mysql/mysql3306]
  2 #df -h
  3 Filesystem               Size  Used Avail Use% Mounted on
  4 /dev/mapper/centos	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Hadoop是由Apache基金會開發的一個大數據分散式系統基礎架構,最早版本是2003年原Yahoo!DougCutting根據Google發佈的學術論文研究而來。 用戶可以在不瞭解分散式底層細節的情況下,輕鬆地在Hadoop上開發和運行處理海量數據的應用程式。低成本、高可靠、高擴展、高有效、高容 ...
  • 參考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系統和軟體版本是這樣的: 系統環境:win7、64位 MySQL版本:8.0.11 對版本的說明 之所以說是 MySQL5.7 及以上版本,是因為從 MySQL5.7 版 ...
  • 使用DBNEWID Utility 工具可以同時修改資料庫名、DBID,也可以只修改其中一項 官方參考: https://docs.oracle.com/cd/E11882_01/server.112/e22490/dbnewid.htm#SUTIL014 一、同時修改db name 和 dbid ...
  • 在這篇博客“ORACLE當中自定義函數性優化淺析”中,我們介紹了通過標量子查詢緩存來優化函數性能: 標量子查詢緩存(scalar subquery caching)會通過緩存結果減少SQL對函數(Function)的調用次數, ORACLE會在記憶體中構建一個哈希表來緩存標量子查詢的結果。 那麼SQL... ...
  • 出現的錯誤: ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction 解決辦法: 查看sleep的進程 mysql show full processlist; + + + + + + + + + | Id ...
  • 大數據和人工智慧的關係,首先要說什麼是大數據。這些年來,大數據先是被神化,繼而又被妖魔化,到了今天,其實誰也不知道別人所謂的大數據指的是什麼? ...
  • ######資料庫操作前的準備########-- 創建資料庫-- create database test charset=utf8; -- 使用資料庫-- use test; -- students表-- create table students( # 欄位屬性空格分隔、欄位間逗號分隔 id ...
  • Slony是PostgreSQL領域中最廣泛的複製解決方案之一。它不僅是最古老的複製實現之一,它也是一個擁有最廣泛的外部工具支持的工具,比如pgAdmin3。多年來,Slony是在PostgreSQL中複製數據的惟一可行的解決方案。Slony使用邏輯複製;Slony-I一般要求表有主鍵,或者唯一鍵; ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...