(一)MySQL 串連最佳化

來源:互聯網
上載者:User

標籤:拒絕   存在   測試   sel   客戶機   休眠   客戶   like   上下   

一、MySQL 串連最佳化

1.查看串連參數(show variables)

mysql> show variables like ‘%connect%‘;+-----------------------------------------------+-----------------+| Variable_name                                 | Value           |+-----------------------------------------------+-----------------+| character_set_connection                      | utf8            || collation_connection                          | utf8_general_ci || connect_timeout                               | 10              || disconnect_on_expired_password                | ON              || init_connect                                  |                 || max_connect_errors                            | 100             || max_connections                               | 151             || max_user_connections                          | 0               || performance_schema_session_connect_attrs_size | 512             |+-----------------------------------------------+-----------------+ 

 

2.查看串連狀態(show status)

mysql> show status like ‘%connections%‘;+-----------------------------------+-------+| Variable_name                     | Value |+-----------------------------------+-------+| Connection_errors_max_connections | 0     || Connections                       | 197   || Max_used_connections              | 2     |+-----------------------------------+-------+

  Connection_errors_max_connections 當MySQL的最大並發數大於系統變數(show variables)中max_connections的最大並發數,因此而被拒絕的次數,將會記錄在這個變數裡。如果Connection_error_max_connections值比較大,則說明當前系統並發比較高,要考慮調大max_connections的值。
  Connections表示MySQL從啟動至今,成功建立串連的串連數,這個值是不斷累加的。
  Max_used_connections表示MySQL從啟動至今,同一時刻並發的串連數,取得是最大值。如果這個值大於 max_connections則表明系統經常處於高並發的狀態,應該考慮調大最大並發串連數。

 

3、連接線程參數(thread variabls and status)

mysql> show variables like ‘thread%‘;+--------------------+---------------------------+| Variable_name      | Value                     |+--------------------+---------------------------+| thread_cache_size  | 9                         || thread_concurrency | 10                        || thread_handling    | one-thread-per-connection || thread_stack       | 262144                    |+--------------------+---------------------------+

  thread_cache_size 設定連接線程緩衝的數目。這個緩衝相當於MySQL線程的緩衝池(thread cache pool),將閒置連接線程放入串連池中緩衝起來,而非立即銷毀。當有新的串連請求時,如果串連池中有閒置串連,則直接使用。否則要重新建立線程。建立線程是一個不小的系統開銷。
  thread_handling 預設值是: one-thread-per-connection 表示為每個串連提供或者建立一個線程來處理請求,直至請求完畢,串連銷毀或者存入緩衝池。當值是no-threads 時,表示在始終只提供一個線程來處理串連,一般是單機做測試使用的。
  thread_stack stack 是堆的意思,知道進程和線程都是有唯一的ID的,進程的ID系統會維護,二線程的ID,則由具體的線程庫區維護,當進程或者線程休眠的時候,進程的上下文資訊要在記憶體中開闢出一塊地區,儲存進程的上下文資訊,以便於迅速喚醒程式。預設為MySQL的每個線程設定的堆棧大小為:262144/1024=256k

 

4.查看線程狀態資訊

mysql> show status like ‘Thread%‘;+-------------------+-------+| Variable_name     | Value |+-------------------+-------+| Threads_cached    | 1     || Threads_connected | 1     || Threads_created   | 2     || Threads_running   | 1     |+-------------------+-------+

  Thread_cached 當前線程池的線程數
  Thread_connected 當前的串連數
  Thread_cached: 當前連接線程建立數, 如果這個值過高,可以調整threadcachesize 也就是調整線程緩衝池的大小。
  Thred_runnint: 當前活躍的線程數。

 

5.串連請求堆棧
MySQL在很短的時間內,突然收到很多的串連請求時,MySQL會將不能來得及處理的串連請求儲存在堆棧中,以便MySQL後續處理。back_log參數設定了堆棧的大小,可以通過如下命令查看:

mysql> show variables like ‘back_log‘;+---------------+-------+| Variable_name | Value |+---------------+-------+| back_log      | 80    |+---------------+-------+

6.串連異常

mysql> show status like ‘Aborted%‘;+------------------+-------+| Variable_name    | Value |+------------------+-------+| Aborted_clients  | 0     || Aborted_connects | 219   |+------------------+-------+

  Aborted_clients MySQL 客戶機被異常關閉的次數。
  Aborted_connects 試圖串連到MySQL伺服器而失敗的串連次數。

7.other

mysql> show status like ‘Slow%‘;+---------------------+-------+| Variable_name       | Value |+---------------------+-------+| Slow_launch_threads | 0     || Slow_queries        | 0     |+---------------------+-------+mysql> show variables like ‘slow_launch_time‘;+------------------+-------+| Variable_name    | Value |+------------------+-------+| slow_launch_time | 2     |+------------------+-------+

Slow_lunch_threads 建立線程的時間過長,超過slow_launch_time的設定值,則會記錄。


8.可以通過使用 Connection_error%來查看串連的錯誤狀態資訊:

mysql> show status like ‘Connection_error%‘;+-----------------------------------+-------+| Variable_name                     | Value |+-----------------------------------+-------+| Connection_errors_accept          | 0     || Connection_errors_internal        | 0     || Connection_errors_max_connections | 0     || Connection_errors_peer_address    | 0     || Connection_errors_select          | 0     || Connection_errors_tcpwrap         | 0     |+-----------------------------------+-------+

  Connection_errors_peer_address 尋找MySQL客戶機IP地址是發生的錯誤數。

(一)MySQL 串連最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.