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 是最后一次执行,所以没出结果

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