mysql 的max_connections和max_user_connections 的區別


----查看max_user_connections 默認值

  MySQL> show variables like 'max_user_connections';
+----------------------+-------+
 | Variable_name        | Value |
 +----------------------+-------+
 | max_user_connections | 0     |
 +----------------------+-------+

1 row in set (0.00 sec)

---設置 max_user_connections
mysql> set  @@global.max_user_connections=1;

Query OK, 0 rows affected (0.03 sec)
mysql>  select @@max_user_connections;
 +------------------------+
 | @@max_user_connections |
 +------------------------+
 |                      1 |
 +------------------------+
 1 row in set (0.00 sec)
上面參數設置完后窗口,要重新登陸一下

root@dg ~]# mysql -uroot -pmysql
然后再開一個窗口登陸就會報如下錯誤:
root@dg ~]# mysql -uroot -pmysql
 Warning: Using a password on the command line interface can be insecure.

ERROR 1203 (42000): User root already has more than 'max_user_connections' active connections



另外在登陸一個用戶u2,正常登陸,但是在登陸一個u2用戶,就會報錯:
root@dg mysql]# mysql -uu2 -pu2
 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 147
 Server version: 5.6.27-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)
 Copyright (c) 2000, 2015, 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.


max_user_connections 針對用戶設計的




下面我們來看看max_connections 的作用
mysql> select @@max_connections;
 +-------------------+
 | @@max_connections |
 +-------------------+
 |               151 |
 +-------------------+
 1 row in set (0.00 sec)
 mysql>  set  @@global.max_connections=2;
 Query OK, 0 rows affected (0.00 sec)
 mysql> select @@max_connections;
 +-------------------+
 | @@max_connections |
 +-------------------+
 |                 2 |
 +-------------------+
 1 row in set (0.00 sec)
上面參數設置完后窗口,要重新登陸一下
[root@dg mysql]# mysql -uroot -pmysql
在開一個窗口session2正常登陸:
[root@dg mysql]# mysql -uroot -pmysql
再開第三個窗口,session3 登陸的時候報錯
[root@dg ~]# mysql -uroot -pmysql
 Warning: Using a password on the command line interface can be insecure.
 ERROR 1040 (HY000): Too many connections
 [root@dg ~]# mysql -uu2 -pu2
 Warning: Using a password on the command line interface can be insecure.
 ERROR 1040 (HY000): Too many connections


實驗完畢后,改回原來的參數
mysql>  set  @@global.max_user_connections=0;
 Query OK, 0 rows affected (0.00 sec)

 mysql>  select @@max_user_connections;
 +------------------------+
 | @@max_user_connections |
 +------------------------+
 |                      0 |
 +------------------------+
 1 row in set (0.00 sec)
 mysql>  set  @@global.max_connections=200;
 Query OK, 0 rows affected (0.00 sec)
 mysql> select @@max_connections;
 +-------------------+
 | @@max_connections |
 +-------------------+
 |               200 |
 +-------------------+
 1 row in set (0.00 sec)


結論:

max_user_connections:限制每個用戶的session連接個數,例如max_user_connections=1 ,那么用戶u1只能連接的session數為1,如果還有用戶u2,還是可以連接,但是連接數仍然為1
max_connections :是對整個服務器的用戶限制,整個服務器只能開這么多session,而不考慮用戶!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM