MySQL8新增管理端口,DBA都說好

簡介

用過MySQL數據庫朋友一定對“ERROR 1040 (HY000): Too many connections”,這個報錯不陌生,出現這個報錯的原因有兩種情況,一種是單個用戶的連接數超過“max_user_connections”參數定義值,另外一種情況是,所有應用的連接數超過“max_connections”參數定義值。

如果是第二種情況,MySQL數據庫還提供一個額外連接,這個連接只有super角色的用戶能登錄,例如root用戶,使用root用戶登錄數據庫之後,就可以進行故障定位。但是如果由於管理不規範,應用程序使用了super角色用戶連接數據,當出現“ERROR 1040 (HY000): Too many connections”報錯之後,大家想想,會發生什麼,這個時候DBA使用root用戶都登錄數據庫,就很難做故障定位了,去解決連接不足的問題。

MySQL8新增管理端口,DBA都說好

MySQL8新增管理端口

在MySQL8以前的版本,由於應用用戶和管理用戶共同使用同一個端口服務,沒有進行隔離,如果使用不規範時,很容易造成DBA無法用root用戶連接數據庫,進行故障定位。

在MySQL8的版本,MySQL官方考慮到這個問題,於是就給數據庫管理人員獨立起了一個管理端口服務,這樣應用用戶和管理用戶訪問的端口進行隔離,互不影響。

MySQL8管理端口啟用

要啟用MySQL8管理端口,只需要在my.cnf配置文件中添加3個參數

<code>admin_address=127.0.0.1
admin_port=33306
create_admin_listener_thread=1

mysql> show variables like '%admin%';
+---------------------------------+-------------------------------+
| Variable_name                   | Value                         |
+---------------------------------+-------------------------------+
| admin_address                   | 127.0.0.1                     |
| admin_port                      | 33306                         |
| admin_ssl_ca                    |                               |
| admin_ssl_capath                |                               |
| admin_ssl_cert                  |                               |
| admin_ssl_cipher                |                               |
| admin_ssl_crl                   |                               |
| admin_ssl_crlpath               |                               |
| admin_ssl_key                   |                               |
| admin_tls_ciphersuites          |                               |
| admin_tls_version               | TLSv1,TLSv1.1,TLSv1.2,TLSv1.3 |
| create_admin_listener_thread    | ON                            |
| log_slow_admin_statements       | OFF                           |
| persist_only_admin_x509_subject |                               |
+---------------------------------+-------------------------------+
14 rows in set (0.01 sec)/<code>

下面就來測試一下,當報"ERROR 1040 (HY000): Too many connections"錯誤之後,DBA是否還能使用管理端口連接數據庫

1.模擬會話總數已經達到max_connections參數定義閥值

<code>mysql> show variables like '%connections%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| max_connections        | 3     |
| max_user_connections   | 2     |
| mysqlx_max_connections | 100   |
+------------------------+-------+
3 rows in set (0.01 sec)

mysql> show global status like 'Threads_connected';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 3     |
+-------------------+-------+
1 row in set (0.01 sec)/<code>

2.使用MySQL數據庫額外提供的端口,root連接數據庫

<code>root@19dd973af376:~# 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 32
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> show global status like 'Threads_connected';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 4     |
+-------------------+-------+
1 row in set (0.01 sec)/<code> 

此時,root用戶已經使用了MySQL數據庫的額外連接,如果再使用root用戶連接數據,就會報錯了

<code>root@19dd973af376:~# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1040 (HY000): Too many connections/<code>

3.使用管理端口連接數據庫

<code>root@19dd973af376:~# mysql -uroot -proot -P 33306 -h127.0.0.1
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 33
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> show global status like 'Threads_connected';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 5     |
+-------------------+-------+
1 row in set (0.03 sec)/<code>

可以看到root用戶還可以通過管理端口33306進行登錄了,當前的連接數總數也到了5個,超過了max_connections定義的3個。

關注

1.如果您喜歡這篇文章,請點贊+轉發。

2.如果您特別喜歡,請加關注。


分享到:


相關文章: