mysql全局变量之max_connections max_user_connections详解
????之前在網上看到很多關于max_conections & max_user_conenctions變量的解釋,但是感覺都有些不太準確,要不就是說的很模糊,因此詳讀了mysql的官方文檔6.3.4?Setting Account Resource Limits章節,并結合實驗進行驗證。
????僅供參考:如有遺誤,望請海涵!
實驗步驟如下:
????1. 啟動mysql
????2. 創建測試環境所需的用戶
????3. 查看及驗證max_connections變量的意義
????4. 查看及驗證max_user_connections變量的意義
啟動mysql,命令如下:
????windows:C:\net start mysql
????linux: #service mysqld start
創建測試環境所需的用戶
????[windows | linux] : mysql -uroot -p
????Enter password:******
????mysql> create user keyman@'127.0.0.1' identified by 'keyman';
????mysql>?create user harry@'127.0.0.1' identified by 'harry';
????mysql> flush privileges;
查看及驗證max_connections變量的含義
????1) 打開4個命令窗口[window1 & window2 & window3 & window4]
????2)window1上執行如下命令
????????[windows | linux] : mysql -uroot -p
????????Enter password:******
????????mysql> select @@max_connections;
????????+-------------------+ ?
????????|? max_connections ?|
????????+-------------------+
????????|????????????????0 ?|
????????+-------------------+
????????//可以看到此時的值(默認值)是0,即不做限制
????????mysql> set @@global.max_connections=2; ? ?
????????mysql> select @@max_connections;
????????+-------------------+ ?
????????|? max_connections ?|
????????+-------------------+
????????| ? ? ? ? ? ? ? ?2 ?|
????????+-------------------+
????????mysql> exit
? ? ????[windows | linux] : mysql -uroot -p
????????Enter password:******
????3) window2上執行如下命令
????????[windows | linux] : mysql -ukeyman -p
????????Enter password:******
????????mysql>
????4) window3上執行如下命令
???? ? ?[windows | linux] : mysql -uharry -p
????????Enter password:*****
????????ERROR 1040(08004):Two many connections
????????//由于max_connections的值設置為2,因此此時無法在進行連接,但是此時仍然不明確
????????//為什么無法連接,繼續測試以進一步驗證無法連接的原因。
????5)window3上執行如下命令
????????注意:4)步驟無法登陸mysql,此步驟可直接在4)的基礎之上執行如下命令,也可以再新打開一個命令窗口。
????????[windows | linux] : mysql -ukeyman -p
????????Enter password:******
????????mysql>
????????//奇跡發生了,keyman用戶竟然連接上了。
????6)window4上執行如下命令
????????[windows | linux] : mysql -ukeyman -p
????????Enter password:******
????????mysql>
????????//奇跡再次發生,keyman用戶的第三個連接竟然又連接上了。
????????
????綜上6步,max_connections變量的意義顯而易見。
查看及驗證max_user_connections變量的含義
????1) 打開三個命令窗口[window1 & window2 & window3 & window4]
????2)window1上執行如下命令
????????[windows | linux] : mysql -uroot -p
????????Enter password:******
????????mysql> select @@max_user_connections;
????????+------------------------+ ?
????????|? max_user_connections ?|
????????+------------------------+
????????| ? ? ? ? ? ? ? ? ? ? 0 ?|
????????+------------------------+
????????//可以看到此時的值(默認值)是0,即不做限制
????????mysql> set @@global.max_user_connections=2; ? ?
????????mysql> select @@max_user_connections;
????????+------------------------+ ?
????????|? max_user_connections ?|
????????+------------------------+
????????| ? ? ? ? ? ? ? ? ? ? 2 ?|
????????+------------------------+
????????mysql> exit
? ? ????[windows | linux] : mysql -uroot -p
????????Enter password:******
????3) window2上執行如下命令
? ? ????[windows | linux] : mysql -uroot -p
????????Enter password:******
????????mysql>
????4) window3上執行如下命令
???? ? ?[windows | linux] : mysql -uroot -p
????????Enter password:******
????????ERROR 1203(42000):User root already has more then 'max_user_connections' active connections
????????//由于max_connections的值設置為2,因此此時無法在進行連接,但是此時仍然不明確
????????//為什么無法連接,繼續測試以進一步驗證無法連接的原因。
????5)window3上執行如下命令
????????注意:4)步驟無法登陸mysql,此步驟可直接在4)的基礎之上執行如下命令,也可以再新打開一個命令窗口。
????????[windows | linux] : mysql -ukeyman -p
????????Enter password:******
????????mysql>
????????//奇跡發生了,keyman用戶竟然連接上了
????6) window4上執行如下命令
? ? ? ? [windows | linux] : mysql -uharry -p
????????Enter password:******
????????mysql>
????????//奇跡再次發生了,harry用戶竟然連接上了
????綜上6步,max_user_connections變量的意義顯而易見。
將max_user_connections變量的值與用戶關聯
? ? 1) 打開三個命令窗口[window1 & window2 & window3 & window4]
????2)window1上執行如下命令
????????[windows | linux] : mysql -uroot -p
????????Enter password:******
????????mysql> select @@max_user_connections;
????????+------------------------+ ?
????????|? max_user_connections ?|
????????+------------------------+
????????| ? ? ? ? ? ? ? ? ? ? 0 ?|
????????+------------------------+
????????//mysql> show grants;命令也可以查看。
????????//可以看到此時的值(默認值)是0,即不做限制
????????mysql> grant usage on *.* to keyman@'127.0.0.1' with max_user_connections 2; ? ?
????????mysql> select @@max_user_connections;
????????+------------------------+ ?
????????|? max_user_connections ?|
????????+------------------------+
????????| ? ? ? ? ? ? ? ? ? ? 2 ?|
????????+------------------------+
????????mysql> exit
? ? ????[windows | linux] : mysql -ukeyman -p
????????Enter password:******
? ? 3) window2上執行如下命令
? ? ????[windows | linux] : mysql -ukeyman -p
????????Enter password:******
????????mysql>
????4) window3上執行如下命令
???? ? ?[windows | linux] : mysql -ukeyman -p
????????Enter password:******
????????ERROR 1203(42000):User keyman already has more then 'max_user_connections' active connections
????????//由于max_connections的值設置為2,因此此時無法在進行連接,但是此時仍然不明確
????????//為什么無法連接,繼續測試以進一步驗證無法連接的原因。
????5)window3上執行如下命令
????????注意:4)步驟無法登陸mysql,此步驟可直接在4)的基礎之上執行如下命令,也可以再新打開一個命令窗口。
????????[windows | linux] : mysql -uroot -p
????????Enter password:******
????????mysql> select @@max_user_connections;
????????+------------------------+ ?
????????|? max_user_connections ?|
????????+------------------------+
????????| ? ? ? ? ? ? ? ? ? ? 0 ?|
????????+------------------------+
????????//經過驗證,在三個窗口中同時登陸root賬戶,沒有任何問題。
????
? ? 如果你堅持看完了這篇文章,那么很榮幸能夠和你分享 最終結論。見證奇跡的時刻:
????????結論1:max_connections變量的意義是 限制當前mysql server中 允許同時連接的不同用
??????????????戶數,并不對相同用戶的多次連接進行限制
????????結論2:max_user_connections變量的意義是 限制當前mysql server中 允許同時連接的相
???????????? ?同用戶的連接數,不對連接的不同用戶數進行限制
????????結論3:對max_connections變量的設置,由于是設置的全局變量,因此均是對所有用戶生效
????????結論4:對max_user_connections的設置,分為以下兩種情況;
????????????1. 針對所有用戶有效
????????????????mysql> set @@global.max_user_connections=2;
????????????2. 針對單個用戶有效
????????????????mysql> grant usage on *.* to keyman@127.0.0.1 with max_user_connections 2;
????????????????//此時的設置將僅對keyman用戶有效。
????????注:與max_user_connections參數類似的還有max_queries_per_hour,max_updates_per_hour
????????????及max_connections_per_hours,見名知意,用法同上。
? ??
轉載于:https://blog.51cto.com/keyman/1652220
總結
以上是生活随笔為你收集整理的mysql全局变量之max_connections max_user_connections详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NSLog (Log信息的输出)
- 下一篇: lamp/lnmp实例