Home > Database > Mysql Tutorial > body text

What does the number of mysql connections mean?

青灯夜游
Release: 2023-04-04 15:15:53
Original
3818 people have browsed it

The number of mysql connections refers to the maximum number of customer connections that the database can accept at the same time. The number of MySQL connections is a component that must be considered. It can help the database run safely while maximizing the current performance of the database. The number of MySQL connections is an important configuration that can affect the client transmission and processing in the database. The amount of data. If the number of connections is too small, the connection request may be blocked and timeout may occur; if the number of connections is too large, a large amount of memory will be occupied, resulting in low performance.

What does the number of mysql connections mean?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

MySQL connection number introduction

The number of database connections is the maximum number of customer connections that the database can accept at the same time.

The number of MySQL connections is a component that must be considered. It can help the database run safely while maximizing the current performance of the database. It is used to control which clients can connect to the database, also known as "Maximum Connection".

The number of MySQL connections is an important configuration that can affect the amount of data transferred and processed by the client in the database. If the number of MySQL connections is too small, the connection request may be blocked and timeout may occur; if the number of MySQL connections is too large, a large amount of memory will be occupied, resulting in low performance. Therefore, the number of MySQL connections must be set reasonably.

The number of MySQL connections can be adjusted through the MySQL configuration file my.cnf, or dynamically adjusted using the following MySQL commands:

SET GLOBAL max_connections=300;
Copy after login

The above command can set the maximum number of MySQL connections to 300. You can also set this parameter in the corresponding my.cnf file:

max_connections=300
Copy after login

The number of MySQL connections can be checked with the following MySQL command:

SHOW STATUS LIKE 'Max_used_connections';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Max_used_connections    | 123   |
+-------------------------+-------+
Copy after login

The above value 123 indicates the maximum number of simultaneous connections on the VW system There are 123 connections to MySQL. It also helps to evaluate whether the current maximum number of connections in the system is reasonable:

  • If the value is high, it indicates that the system may have a large number of clients accessing MySQL at the same time. At this time, it is best to consider adjusting MySQL The maximum number of connections;

  • If the value is small, it indicates that the available resources of the system cannot be fully utilized. At this time, you can consider increasing the maximum number of MySQL connections.

In general, the number of MySQL connections is a component that must be considered, and it has an important impact on system security and performance. Therefore, it needs to be adjusted appropriately to maximize MySQL performance.

How to check the number of connections in Mysql (total number of connections, active number, maximum number of concurrent connections)

show variables like '%max_connection%'; 查看最大连接数
set global max_connections=1000;        重新设置最大连接数
Copy after login
mysql> show status like  'Threads%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_cached    | 32    |
| Threads_connected | 10    |
| Threads_created   | 50    |
| Threads_rejected  | 0     |
| Threads_running   | 1     |
+-------------------+-------+
5 rows in set (0.00 sec)
Copy after login
  • Threads_connected: This The value refers to the number of open connections.

  • Threads_running: This value refers to the number of activated connections. This value is generally much lower than the connected value.

  • Threads_connected has the same result as show processlist, indicating the current number of connections. To be precise, Threads_running represents the current number of concurrency

query the maximum number of connections currently set in the database

mysql> show variables like '%max_connection%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| extra_max_connections |       |
| max_connections       | 2512  |
+-----------------------+-------+
2 rows in set (0.00 sec)
Copy after login

in/etc/my. Set the maximum number of database connections in cnf

[mysqld]
max_connections = 100
Copy after login
The number of threads of the MySQL server needs to be within a reasonable range, so as to ensure the healthy and smooth operation of the MySQL server . Threads_created indicates the number of threads created. By viewing Threads_created, you can view the process status of the MySQL server.
mysql> show global status like 'Thread%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Threadpool_idle_threads | 0     |
| Threadpool_threads      | 0     |
| Threads_cached          | 29    |
| Threads_connected       | 13    |
| Threads_created         | 50    |
| Threads_rejected        | 0     |
| Threads_running         | 1     |
+-------------------------+-------+
7 rows in set (0.00 sec)
Copy after login

If we set thread_cache_size in the MySQL server configuration file, when the client disconnects, the server's thread processing this client will be cached to respond to the next client Instead of destroying it (provided that the number of caches does not reach the upper limit).

Threads_created indicates the number of threads created. If the value of Threads_created is found to be too large, it indicates that the MySQL server has been creating threads, which is also relatively resource-intensive. You can appropriately increase the thread_cache_size value in the configuration file and query the value of the server thread_cache_size. :

mysql> show variables like 'thread_cache_size';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| thread_cache_size | 100   |
+-------------------+-------+
1 row in set (0.00 sec)
Copy after login

命令:show processlist;

如果是root帐号,你能看到所有用户的当前连接。如果是其它普通帐号,只能看到自己占用的连接。

show processlist命令只列出前100条,如果想全列出请使用show full processlist;

mysql> show processlist;
Copy after login

命令:show status;

mysql>show status like '%变量名%';
Copy after login

变量名如下:

  • Aborted_clients 由于客户没有正确关闭连接已经死掉,已经放弃的连接数量。

  • Aborted_connects 尝试已经失败的MySQL服务器的连接的次数。

  • Connections 试图连接MySQL服务器的次数。

  • Created_tmp_tables 当执行语句时,已经被创造了的隐含临时表的数量。

  • Delayed_insert_threads 正在使用的延迟插入处理器线程的数量。

  • Delayed_writes 用INSERT DELAYED写入的行数。

  • Delayed_errors 用INSERT DELAYED写入的发生某些错误(可能重复键值)的行数。

  • Flush_commands 执行FLUSH命令的次数。

  • Handler_delete 请求从一张表中删除行的次数。

  • Handler_read_first 请求读入表中第一行的次数。

  • Handler_read_key 请求数字基于键读行。

  • Handler_read_next 请求读入基于一个键的一行的次数。

  • Handler_read_rnd 请求读入基于一个固定位置的一行的次数。

  • Handler_update 请求更新表中一行的次数。

  • Handler_write 请求向表中插入一行的次数。

  • Key_blocks_used 用于关键字缓存的块的数量。

  • Key_read_requests 请求从缓存读入一个键值的次数。

  • Key_reads 从磁盘物理读入一个键值的次数。

  • Key_write_requests 请求将一个关键字块写入缓存次数。

  • Key_writes 将一个键值块物理写入磁盘的次数。

  • Max_used_connections 同时使用的连接的最大数目。

  • Not_flushed_key_blocks 在键缓存中已经改变但是还没被清空到磁盘上的键块。

  • Not_flushed_delayed_rows 在INSERT DELAY队列中等待写入的行的数量。

  • Open_tables 打开表的数量。

  • Open_files 打开文件的数量。

  • Open_streams 打开流的数量(主要用于日志记载)

  • Opened_tables 已经打开的表的数量。

  • Questions 发往服务器的查询的数量。

  • Slow_queries 要花超过long_query_time时间的查询数量。

  • Threads_connected 当前打开的连接的数量。

  • Threads_running 不在睡眠的线程数量。

  • Uptime 服务器工作了多长时间,单位秒。

【相关推荐:mysql视频教程

The above is the detailed content of What does the number of mysql connections mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!