5.1.12.1 Connection Interfaces连接接口

This section describes aspects of how the MySQL server manages client connections.本节描述MySQL服务器如何管理客户端连接的各个方面。

Network Interfaces and Connection Manager Threads网络接口和连接管理器线程

The server is capable of listening for client connections on multiple network interfaces. Connection manager threads handle client connection requests on the network interfaces that the server listens to:服务器能够监听多个网络接口上的客户端连接。连接管理器线程处理服务器侦听的网络接口上的客户端连接请求:

  • On all platforms, one manager thread handles TCP/IP connection requests.在所有平台上,一个管理器线程处理TCP/IP连接请求。

  • On Unix, the same manager thread also handles Unix socket file connection requests.在Unix上,同一个管理器线程也处理Unix套接字文件连接请求。

  • On Windows, one manager thread handles shared-memory connection requests, and another handles named-pipe connection requests.在Windows上,一个管理器线程处理共享内存连接请求,另一个处理命名管道连接请求。

  • On all platforms, an additional network interface may be enabled to accept administrative TCP/IP connection requests. This interface can use the manager thread that handles ordinary TCP/IP requests, or a separate thread.在所有平台上,可以启用额外的网络接口来接受管理TCP/IP连接请求。此接口可以使用处理“普通”TCP/IP请求的管理器线程,也可以使用单独的线程。

The server does not create threads to handle interfaces that it does not listen to. For example, a Windows server that does not have support for named-pipe connections enabled does not create a thread to handle them.服务器不会创建线程来处理它不监听的接口。例如,未启用命名管道连接支持的Windows服务器不会创建处理它们的线程。

Individual server plugins or components may implement their own connection interface:单个服务器插件或组件可以实现自己的连接接口:

Client Connection Thread Management客户端连接线程管理

Connection manager threads associate each client connection with a thread dedicated to it that handles authentication and request processing for that connection. Manager threads create a new thread when necessary but try to avoid doing so by consulting the thread cache first to see whether it contains a thread that can be used for the connection. When a connection ends, its thread is returned to the thread cache if the cache is not full.连接管理器线程将每个客户端连接与专用于它的线程相关联,该线程处理该连接的身份验证和请求处理。管理器线程在必要时创建一个新线程,但为了避免这样做,请先咨询线程缓存,看看它是否包含可用于连接的线程。当连接结束时,如果缓存未满,则其线程将返回给线程缓存。

In this connection thread model, there are as many threads as there are clients currently connected, which has some disadvantages when server workload must scale to handle large numbers of connections. For example, thread creation and disposal becomes expensive. 在这种连接线程模型中,线程的数量与当前连接的客户端数量一样多,当服务器工作负载必须扩展以处理大量连接时,这有一些缺点。例如,线程创建和处理变得昂贵。Also, each thread requires server and kernel resources, such as stack space. To accommodate a large number of simultaneous connections, the stack size per thread must be kept small, leading to a situation where it is either too small or the server consumes large amounts of memory. Exhaustion of other resources can occur as well, and scheduling overhead can become significant.此外,每个线程都需要服务器和内核资源,如堆栈空间。为了容纳大量的同时连接,每个线程的堆栈大小必须保持较小,从而导致堆栈太小或服务器消耗大量内存的情况。其他资源也可能耗尽,调度开销可能会变得很大。

MySQL Enterprise Edition includes a thread pool plugin that provides an alternative thread-handling model designed to reduce overhead and improve performance. MySQL Enterprise Edition包含一个线程池插件,该插件提供了一种替代线程处理模型,旨在减少开销并提高性能。It implements a thread pool that increases server performance by efficiently managing statement execution threads for large numbers of client connections. 它实现了一个线程池,通过高效管理大量客户端连接的语句执行线程来提高服务器性能。See Section 5.6.3, “MySQL Enterprise Thread Pool”.请参阅第5.6.3节,“MySQL企业线程池”

To control and monitor how the server manages threads that handle client connections, several system and status variables are relevant. 为了控制和监视服务器如何管理处理客户端连接的线程,有几个系统和状态变量是相关的。(See Section 5.1.8, “Server System Variables”, and Section 5.1.10, “Server Status Variables”.)(见第5.1.8节,“服务器系统变量”第5.1.10节,“服务器状态变量”。)

  • The thread_cache_size system variable determines the thread cache size. thread_cache_size系统变量决定线程缓存大小。By default, the server autosizes the value at startup, but it can be set explicitly to override this default. 默认情况下,服务器在启动时自动调整值的大小,但可以显式设置以覆盖此默认值。A value of 0 disables caching, which causes a thread to be set up for each new connection and disposed of when the connection terminates. 值为0会禁用缓存,这会导致为每个新连接设置一个线程,并在连接终止时将其丢弃。To enable N inactive connection threads to be cached, set thread_cache_size to N at server startup or at runtime. 要启用N个非活动连接线程的缓存,请在服务器启动或运行时将thread_cache_size设置为NA connection thread becomes inactive when the client connection with which it was associated terminates.当与其关联的客户端连接终止时,连接线程将变为非活动状态。

  • To monitor the number of threads in the cache and how many threads have been created because a thread could not be taken from the cache, check the Threads_cached and Threads_created status variables.要监视缓存中的线程数以及由于无法从缓存中获取线程而创建的线程数,请检查threads_cachedthreads_created状态变量。

  • When the thread stack is too small, this limits the complexity of the SQL statements the server can handle, the recursion depth of stored procedures, and other memory-consuming actions. 当线程堆栈太小时,这会限一致性务器可以处理的SQL语句的复杂性、存储过程的递归深度和其他占用内存的操作。To set a stack size of N bytes for each thread, start the server with thread_stack set to N.要为每个线程设置N字节的堆栈大小,请将thread_stack设置为N来启动服务器。

Connection Volume Management连接卷管理

To control the maximum number of clients the server permits to connect simultaneously, set the max_connections system variable at server startup or at runtime. It may be necessary to increase max_connections if more clients attempt to connect simultaneously then the server is configured to handle (see Section B.3.2.5, “Too many connections”). 要控一致性务器允许同时连接的最大客户端数量,请在服务器启动或运行时设置max_connections系统变量。如果更多客户端尝试同时连接,则可能需要增加max_connections,然后服务器被配置为处理(见第B.3.2.5节,“连接太多”)。If the server refuses a connection because the max_connections limit is reached, it increments the Connection_errors_max_connections status variable.如果服务器因达到max_connections限制而拒绝连接,则会递增connection_errors_max_connections状态变量。

mysqld actually permits max_connections + 1 client connections. mysqld实际上允许max_connections+1个客户端连接。The extra connection is reserved for use by accounts that have the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege). 额外的连接保留给具有CONNECTION_ADMIN权限(或已弃用的SUPER权限)的帐户使用。By granting the privilege to administrators and not to normal users (who should not need it), an administrator can connect to the server and use SHOW PROCESSLIST to diagnose problems even if the maximum number of unprivileged clients are connected. 通过将权限授予管理员而不是普通用户(他们不需要权限),即使连接了最大数量的无权限客户端,管理员也可以连接到服务器并使用SHOW PROCESSIST诊断问题。See Section 13.7.7.29, “SHOW PROCESSLIST Statement”.请参阅第13.7.7.29节,“SHOW PROCESSLIST语句”

As of MySQL 8.0.14, the server also permits administrative connections on an administrative network interface, which you can set up using a dedicated IP address and port. See Section 5.1.12.2, “Administrative Connection Management”.从MySQL 8.0.14开始,服务器还允许在管理网络接口上进行管理连接,您可以使用专用IP地址和端口进行设置。请参阅第5.1.12.2节,“管理连接管理”

The Group Replication plugin interacts with MySQL Server using internal sessions to perform SQL API operations. Group Replication插件使用内部会话与MySQL Server交互,以执行SQL API操作。In releases to MySQL 8.0.18, these sessions count towards the client connections limit specified by the max_connections server system variable. 在MySQL 8.0.18的版本中,这些会话计入max_connections服务器系统变量指定的客户端连接限制。In those releases, if the server has reached the max_connections limit when Group Replication is started or attempts to perform an operation, the operation is unsuccessful and Group Replication or the server itself might stop. 在这些版本中,如果在启动组复制或尝试执行操作时服务器已达到max_connections限制,则操作不成功,组复制或服务器本身可能会停止。From MySQL 8.0.19, Group Replication's internal sessions are handled separately from client connections, so they do not count towards the max_connections limit and are not refused if the server has reached this limit.从MySQL 8.0.19开始,组复制的内部会话与客户端连接分开处理,因此它们不计入max_connections限制,如果服务器达到此限制,也不会被拒绝。

The maximum number of client connections MySQL supports (that is, the maximum value to which max_connections can be set) depends on several factors:MySQL支持的最大客户端连接数(即max_connections可以设置的最大值)取决于几个因素:

  • The quality of the thread library on a given platform.给定平台上线程库的质量。

  • The amount of RAM available.可用RAM的数量。

  • The amount of RAM is used for each connection.RAM的数量用于每个连接。

  • The workload from each connection.每个连接的工作负载。

  • The desired response time.期望的响应时间。

  • The number of file descriptors available.可用的文件描述符的数量。

Linux or Solaris should be able to support at least 500 to 1000 simultaneous connections routinely and as many as 10,000 connections if you have many gigabytes of RAM available and the workload from each is low or the response time target undemanding.Linux或Solaris应该能够支持至少500到1000个常规的同时连接,如果您有许多GB的RAM可用,并且每个RAM的工作负载低或响应时间目标不高,则可以支持多达10000个连接。

Increasing the max_connections value increases the number of file descriptors that mysqld requires. 增加max_connections值会增加mysqld所需的文件描述符的数量。If the required number of descriptors are not available, the server reduces the value of max_connections. 如果所需数量的描述符不可用,服务器将减少max_connections的值。For comments on file descriptor limits, see Section 8.4.3.1, “How MySQL Opens and Closes Tables”.有关文件描述符限制的注释,请参阅第8.4.3.1节,“MySQL如何打开和关闭表”

Increasing the open_files_limit system variable may be necessary, which may also require raising the operating system limit on how many file descriptors can be used by MySQL. 可能需要增加open_files_limit系统变量,这也可能需要提高MySQL可以使用的文件描述符数量的操作系统限制。Consult your operating system documentation to determine whether it is possible to increase the limit and how to do so. See also Section B.3.2.16, “File Not Found and Similar Errors”.请查阅您的操作系统文档,以确定是否可以增加限制以及如何增加限制。另请参阅第B.3.2.16节,“找不到文件和类似错误”