max_user_connectionsとmax_connections

2691 ワード

max_user_connectionsは単一ユーザー向け
max_connectionsはすべてのユーザーに対して
1つのスクリプトで修正できます
 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

4回目の接続でrootもtestもエラーが発生し、合計3人を超えたことがわかります.
上の例からも分かるように,バックグラウンドプログラムの実行順序は,スクリプトで発行される順序とは異なる.
ここでi=1は最後の実行なので結果は出ません