如何确定最大可能的线程数?

Pre*_*ton 6 mysql max-connections

什么因素决定了 mysqld 将创建的最大可能线程数?

我只关心对 mysql 的考虑,而不关心操作系统可能施加的限制。此外,我只关心 Linux,但如果您想包含特定于 Windows 的信息,请随意。

据我所知,可以为复制创建线程,innodb 的各个方面,每个客户端连接最多一个 max_connections,还有一个额外的 +1 来处理超级连接。还有什么,或者就是这样?

threads_max =   max_connections  
              + 1 for super  
              + 2 for replication slave threads  
              + ? for InnoDB ?
Run Code Online (Sandbox Code Playgroud)

Mor*_*ker 7

查看当前线程(包括所有前台和后台线程)的最佳方式是使用performance_schema. 例如:

mysql 5.7.3> select thread_id, name, type, PROCESSLIST_ID from performance_schema.threads;
+-----------+----------------------------------------+------------+----------------+
| thread_id | name                                   | type       | PROCESSLIST_ID |
+-----------+----------------------------------------+------------+----------------+
|         1 | thread/sql/main                        | BACKGROUND |           NULL |
|         2 | thread/innodb/io_log_thread            | BACKGROUND |           NULL |
|         3 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         4 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         5 | thread/innodb/io_ibuf_thread           | BACKGROUND |           NULL |
|         6 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         7 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         8 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|         9 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        10 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        11 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        14 | thread/innodb/srv_lock_timeout_thread  | BACKGROUND |           NULL |
|        15 | thread/innodb/srv_error_monitor_thread | BACKGROUND |           NULL |
|        16 | thread/innodb/srv_monitor_thread       | BACKGROUND |           NULL |
|        17 | thread/innodb/srv_master_thread        | BACKGROUND |           NULL |
|        18 | thread/innodb/srv_purge_thread         | BACKGROUND |           NULL |
|        19 | thread/innodb/page_cleaner_thread      | BACKGROUND |           NULL |
|        20 | thread/sql/signal_handler              | BACKGROUND |           NULL |
|        22 | thread/sql/one_connection              | FOREGROUND |              2 |
+-----------+----------------------------------------+------------+----------------+
19 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

我相信后台线程的数量是固定的(实际数量将取决于一些配置设置,例如innodb_read_io_threads和数量innodb_purge_threads等。) FOREGROUND 线程是每个连接一个 + 最重要的潜力thread_cache_size


Dim*_*lov 5

例如在 Linux 上,以较小者为准:

grep -F 'Max process' /proc/$(/sbin/pidof mysqld)/limits|awk '{print $3}'
Run Code Online (Sandbox Code Playgroud)

和:

mysql -e'show global variables like "max_connections"'
Run Code Online (Sandbox Code Playgroud)

更新:正如您正确指出的,我们应该至少将后台线程添加到列表中。更多信息如下。

就您的上次更新而言,对于 InnoDB,我们有 innodb_read_io_threads 和 innodb_write_io_threads

我刚刚在 debian 上检查了一个 MySQL 5.5.34,我们有默认值:

mysql> pager grep -i thread
PAGER set to 'grep -i thread'
mysql> show engine innodb status\G
BACKGROUND THREAD
srv_master_thread loops: 1 1_second, 1 sleeps, 0 10_second, 1 background, 1 flush
srv_master_thread log flush and writes: 2
MySQL thread id 36, OS thread handle 0xa6c32b40, query id 105 localhost root
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Main thread process no. 21508, id 2758552384, state: waiting for server activity
1 row in set (0.00 sec)

mysql>
Run Code Online (Sandbox Code Playgroud)

操作系统看到:

root@ubuntu:~# ps -Lp$(pidof mysqld) |wc -l
18
Run Code Online (Sandbox Code Playgroud)

以 root 身份连接后,线程数保持不变(当我使另一个变得困难时,线程数增加了):

root@ubuntu:~# mysql -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.5.34-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \! ps -Lp$(pidof mysqld) |wc -l
18
mysql> show global status like '%thread%';
+------------------------------------------+-------+
| Variable_name                            | Value |
+------------------------------------------+-------+
| Delayed_insert_threads                   | 0     |
| Performance_schema_thread_classes_lost   | 0     |
| Performance_schema_thread_instances_lost | 0     |
| Slow_launch_threads                      | 0     |
| Threads_cached                           | 0     |
| Threads_connected                        | 1     |
| Threads_created                          | 1     |
| Threads_running                          | 1     |
+------------------------------------------+-------+
8 rows in set (0.01 sec)

mysql> show global variables like '%thread%';
+-----------------------------------------+---------------------------+
| Variable_name                           | Value                     |
+-----------------------------------------+---------------------------+
| innodb_purge_threads                    | 0                         |
| innodb_read_io_threads                  | 4                         |
| innodb_thread_concurrency               | 0                         |
| innodb_thread_sleep_delay               | 10000                     |
| innodb_write_io_threads                 | 4                         |
| max_delayed_threads                     | 20                        |
| max_insert_delayed_threads              | 20                        |
| myisam_repair_threads                   | 1                         |
| performance_schema_max_thread_classes   | 50                        |
| performance_schema_max_thread_instances | 1000                      |
| thread_cache_size                       | 8                         |
| thread_concurrency                      | 10                        |
| thread_handling                         | one-thread-per-connection |
| thread_stack                            | 196608                    |
+-----------------------------------------+---------------------------+
14 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

所以,正如我所说,这可能是特定于版本的,但我们似乎有:

  • srv_master_thread
  • n I/O 线程(innodb_read_io_threads、innodb_write_io_threads、插入缓冲区线程、日志线程)
  • 其他后台线程(在这种情况下为 7:总共 18 - 10(I/O) - 1(主))。

因此,如果您需要进行某种容量规划,我建议在没有用户/应用程序连接的情况下在启动后检查后台线程的数量并添加:

  1. 1 具有超级权限的用户
  2. max_connections 的值
  3. 2 个复制从站