MySQL案例:一次詭異的Aborted connection錯誤排查

2021-03-02 MySQL資料庫運維技術棧
簡介

  前段時間,研究怎麼去提升資料庫安全,例如禁止執行不帶條件的update操作,於是就想到了去啟用sql_safe_updates參數,這個參數Mysql默認是不啟用的,而且還不能加入到my.cnf配置裡。因此就想到了用init_connect參數,將sql_safe_updates=1放到init_connect參數裡,這樣每個用戶會話連接的時候,就會啟用sql_safe_updates參數了。

  可是用普通連接資料庫之後,使用某個庫之後,就會報錯

mysql> use information_schema;
No connection. Trying to reconnect...
Connection id: 16
Current database: *** NONE ***

ERROR 1184 (08S01): Aborted connection 16 to db: 'unconnected' user: 'jim' host: 'localhost' (init_connect command failed)

報錯分析

分析報錯之前,先復盤一下操作步驟

1.創建普通用戶

mysql> create user 'jim'@'%' identified by 'jim';
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host from mysql.user;
+---+-+
| user | host |
+---+-+
| jim | % |
| repl | % |
| root | % |
| tony | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---+-+
8 rows in set (0.10 sec)

2.使用root用戶登錄資料庫,並設置init_connect參數

mysql> set global init_connect='sql_safe_updates=1';
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'init_connect';
+++
| Variable_name | Value |
+++
| init_connect | sql_safe_updates=1 |
+++
1 row in set (0.00 sec)

3.使用普通用戶jim連接測試

root@18374a493e56:~# mysql -ujim -pjim
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.21

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use information_schema
No connection. Trying to reconnect...
Connection id: 19
Current database: *** NONE ***

ERROR 1184 (08S01): Aborted connection 19 to db: 'unconnected' user: 'jim' host: 'localhost' (init_connect command failed)

4.使用root用戶連接測試

root@18374a493e56:~# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use information_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

從上面的報錯信息也能很快的判斷出,是由於init_connect設置不合理導致的,可是這裡很奇怪的是,普通用戶會報錯,root用戶操作沒有報錯。弄不清楚為什麼,於是就去看官方文檔,看看官方文檔是怎麼描述的。

init_connect參數描述

For users that have the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege), the content of init_connect is not executed. This is done so that an erroneous value for init_connect does not prevent all clients from connecting. For example, the value might contain a statement that has a syntax error, thus causing client connections to fail. Not executing init_connect for users that have the CONNECTION_ADMIN or SUPER privilege enables them to open a connection and fix the init_connect value.

這段話的大概意思是,當用戶具有CONNECTION_ADMIN,SUPER權限用戶登錄時,是不需要執行init_connect參數的內容的,而不具備這些權限的用戶登錄時,需要執行init_connect參數的內容,當init_connect參數的內容語句有問題時,就會報錯了,這就解釋了為什麼root用戶沒有問題,而普通用戶發生了問題。

了解報錯原因之後,需要修改init_connect的內容了,init_connect裡的內容複製出來,如果在mysql command命令行裡執行沒有問題就可以了。

5.重新設置init_connect參數值

mysql> show variables like 'init_connect';
+++
| Variable_name | Value |
+++
| init_connect | sql_safe_updates=1 |
+++
1 row in set (0.01 sec)

mysql> set session sql_safe_updates=1;
Query OK, 0 rows affected (0.00 sec)

mysql> set global init_connect='set session sql_safe_updates=1';
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'init_connect';
++--+
| Variable_name | Value |
++--+
| init_connect | set session sql_safe_updates=1 |
++--+
1 row in set (0.00 sec)

6.使用普通用戶jim再次連接測試

root@18374a493e56:~# mysql -ujim -pjim
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use information_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
mysql> show variables like 'sql_safe_updates';
+---+--+
| Variable_name | Value |
+---+--+
| sql_safe_updates | ON |
+---+--+
1 row in set (0.01 sec)

從測試結果可以看到,已經可以正常使用Mysql資料庫了,而且參數sql_safe_updates也設置正確了。

總結

總之,生產操作無小事,大家在生產上執行任何操作時,一定要在測試環境充分驗證之後,了解影響範圍之後,方可上線操作,如文中操作,很可能會導致一次線上故障。

相關焦點

  • MySQL 大量 Opening tables 案例分析-愛可生
    因為一時間沒法排查出具體原因,所以針對 MySQL 進程打了個快照,方便事後詳細排查。(sql_parse.cc:2792),mysql_parse(sql_parse.cc:5582),dispatch_command(sql_parse.cc:1458),do_command(sql_parse.cc:999),handle_connection(connection_handler_per_thread.cc:300),pfs_spawn_thread(pfs.cc:2190)
  • MySQL報1045(28000)錯誤的解決辦法
    【IT168 技術文檔】  今天不知道怎麼了,在windowns 7上安裝mysql,就是不成功,後來沒有辦法去,在http://dev.mysql.com/downloads/mysql/下了個免安裝的版本,解壓後,用是能用了。
  • mysql資料庫保存異常:state「HY000」;error code「1366」
    錯誤日誌 一、原因分析1、查看服務接口api編碼通過UrlDecode對接口通過這個就可以很明顯的看出來,是因為存儲的數據是十六進位的字符3、查看mysql字符集所支持的字節範圍我們可以知道utf8最大支持3個字節的字符,查看mysql官網發現,mysql5.5.3之後,新增了一個與utf-8類似的字符集,utf8mb4,並且它的編碼支持4個字節每字符
  • MySQL密碼管理
    1.密碼修改set--在當前的用戶登錄後修改set password=password('new_password');mysqladmin-- mysqladmin -uroot -pold_password password new_password[root@jssdb01 ~]# mysqladmin -uroot -prootroot password root123mysqladmin: [Warning] Using a password
  • MySQL分支資料庫MariaDB之CentOS安裝教程
    1.1.4 測試登錄:mysql -u root –p查看埠是否開啟成功:netstat -anpt | grep mysqld1.2 配置UTF字符集編碼1)vi /etc/my.cnf在 [mysqld
  • mysqldump中的幾個坑
    記錄一下在mysqldump使用中遇到的幾個坑。
  • mysql服務啟動1053錯誤如何解決
    mysql啟動服務出現1053錯誤的解決方法  第一種方法:  1.首先,使用組合鍵「win+R」運行「cmd」進入DOS窗口;  2.在DOS窗口中,使用命令「mysqld-nt
  • MySQL 數據校驗工具-愛可生|mysql|perl|伺服器|node01_網易訂閱
    場景  pt-table-checksum 默認情況下可以應對絕對部分場景,官方說,即使上千個庫、上萬億的行,它依然可以很好的工作,這源自於設計很簡單,一次檢查?個表,不需要太多的內存和多餘的操作;必要時,pt-table-checksum 會根據伺服器負載動態改變 chunk 大小,減少從庫的延遲。
  • MySQL資料庫主從複製搭建
    --datadir=/data/3307/data --basedir=/application/mysql mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/application/mysql mysqld --initialize-insecure
  • MySQL 工作、底層原理,看這一篇就夠了!
    它根據MySql AB公司提供的文件訪問層的一個抽象接口來定製一種文件訪問機制(這種訪問機制就叫存儲引擎)SQL 語句執行過程資料庫通常不會被直接使用,而是由其他程式語言通過SQL語句調用mysql,由mysql處理並返回執行結果。那麼Mysql接受到SQL語句後,又是如何處理?
  • MySQL主從複製原理
    >12.從庫會自動purge應用過relay進行定期清理補充說明:一旦主從複製構建成功,主庫當中發生了新的變化,都會通過dump_T發送信號給IO_T,增強主從複製的實時性主從複製監控查看信息命令mysql
  • 乾貨|Java 線上故障排查完整套路!牛掰!
    這邊可以再一段時間後再跑一次命令看看內存增長情況,或者和正常機器比較可疑的內存段在哪裡。TCP 隊列溢出tcp 隊列溢出是個相對底層的錯誤,它可能會造成超時、rst 等更表層的錯誤。因此錯誤也更隱蔽,所以我們單獨說一說。如上圖所示,這裡有兩個隊列:syns queue(半連接隊列)、accept queue(全連接隊列)。
  • MySQL 資料庫的安裝和密碼的設定
    以管理身份運行cmd進入安裝MySQL的bin目錄下,執行mysql -install,成功後執行net start mysql,顯示已成功啟動時,完成。若此時報出1067的錯誤,可能是my.ini配置有問題。執行mysqld -remove,刪除成功後,重新配置my.ini文件,重新執行步驟6.
  • 讓Python幫你搞定MySQL資料庫
    我們在github上下載fifa18球員數據,將這些信息存入到mysql。import csvimport pymysqlclass LoadDataFromCsvToMysql: def __init__(self, csvpath, csvfield, mysqlfield, table, sqlconfig): pass def connectSql(self):
  • 今日份知識分享:mysql創建遠程登入用戶
    用root帳戶(或者其他有權限的帳戶)登錄, mysql -u root -p 回車 輸入密碼 回車
  • 高性能Mysql主從架構的複製原理及配置詳解
    當一個從伺服器連接主伺服器時,它通知主伺服器從伺服器在日誌中讀取的最後一次成功更新的位置。從伺服器接收從那時起發生的任何更新,然後封鎖並等待主伺服器通知新的更新。請注意當你進行複製時,所有對複製中的表的更新必須在主伺服器上進行。否則,你必須要小心,以避免用戶對主伺服器上的表進行的更新與對從伺服器上的表所進行的更新之間的衝突。
  • MySQL的主從備份
    進程讀取主庫的binlog內容存儲到relay log日誌中從庫的SQL進程讀取relay log日誌中內容在從庫中重放MySQL主從配置步驟配置主從資料庫伺服器參數主伺服器配置log_bin = /data/mysql
  • 一千行MySQL學習筆記
    /* 啟動MySQL */net start mysql/* 連接與斷開伺服器 */mysql -h 地址 -P 埠 -u 用戶名 -p 密碼/* 跳過權限驗證登錄MySQL */mysqld --skip-grant-tables-- 修改root密碼密碼加密函數password()update mysql.user
  • 輕鬆搭建MySQL主從複製、讀寫分離(雙機熱備)
    因此,我們利用mysql自帶的REPLICATION來實現mysql多機熱備的功能,mysql版本為5.7進行演示。讀寫分離:就是把對資料庫的讀操作和寫操作分離開,將讀寫壓力分擔到多臺伺服器上,通常用於讀遠大於寫的場景。