MySQLの場合:奇妙な中止された接続エラーのトラブルシューティング

前書き

少し前に、無条件の更新操作の実行を禁止するなど、データベースのセキュリティを向上させる方法を研究したので、デフォルトではMysqlによって有効にならず、my.cnf構成に追加できないsql_safe_updatesパラメーターを有効にすることを考えました。そこで、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パラメータが設定されている場合contentステートメントに問題がある場合、エラーが報告されます。これは、通常のユーザーには問題があるのに対し、rootユーザーには問題がない理由を説明しています。

エラーの原因を理解した後、init_connectの内容を変更し、その内容をinit_connectにコピーする必要があります。mysqlコマンドラインでの実行に問題がなければ、問題ありません。

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も正しく設定されていることがわかります。

総括する

つまり、本番運用については些細なことではありません。本番環境で操作を実行するときは、オンラインにする前に、テスト環境を完全に検証し、影響の範囲を理解する必要があります。テキストで行うと、原因となる可能性があります。オンライン障害。

https://mp.weixin.qq.com/s/LAcSlcWPzGipZv6dgldOxw

おすすめ

転載: blog.csdn.net/qq_40907977/article/details/114654949