max_user_connections與max_connections

max_user_connections針對單一用戶

max_connections針對所有用戶

用一個腳本可以驗正

 cat test.sh

mysql -uroot -p3306 -e "show variables like '%max%connections'"
for i in {1..4}
do
mysql -utest -ptest -e "select sleep(1),sysdate(),user(),'$i'" &
mysql -uroot -p3306 -e "select sleep(1),sysdate(),user(),'$i'" &
done

sh test.sh 

Warning: Using a password on the command line interface can be insecure.
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| max_connections      | 151   |
| max_user_connections | 3     |
+----------------------+-------+
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
[root@o11204 tmp]# Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
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
ERROR 1203 (42000): User test already has more than 'max_user_connections' active connections
+----------+---------------------+----------------+---+
| sleep(1) | sysdate()           | user()         | 3 |
+----------+---------------------+----------------+---+
+----------+---------------------+----------------+---+
|        0 | 2015-03-10 12:32:39 | root@localhost | 3 |
+----------+---------------------+----------------+---+
| sleep(1) | sysdate()           | user()         | 4 |
+----------+---------------------+----------------+---+
|        0 | 2015-03-10 12:32:39 | root@localhost | 4 |
+----------+---------------------+----------------+---+
sleep(1)	sysdate()	user()	3
0	2015-03-10 12:32:39	test@localhost	3
+----------+---------------------+----------------+---+
| sleep(1) | sysdate()           | user()         | 2 |
+----------+---------------------+----------------+---+
|        0 | 2015-03-10 12:32:39 | root@localhost | 2 |
+----------+---------------------+----------------+---+
sleep(1)	sysdate()	user()	2
0	2015-03-10 12:32:39	test@localhost	2
sleep(1)	sysdate()	user()	4
0	2015-03-10 12:32:39	test@localhost	4

可以看到當第四次連接時不管root還是test用戶都報錯,而這時總用戶已超過了3個

而由上面的例子也可以看到,後臺程序執行的先後順序,與腳本里發出的順序不一樣

在這兒 i=1 是最後一次執行,所以沒出結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章