• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

MySQL thread_stack连接线程的优化

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

MySQL连接不仅能通过网络方式,还可以通过命名管道的方式,不论是哪种方式连接MySQL,在MySQL中都是通过线程的方式管理所有客户端请求的。每一个客户端连接都会有一个与之对应的连接线程。在MySQL中实现了一个Thread Cache池,将空闲的连接线程存放其中,而不是完成请求后就销毁。这样,当有新的连接请求时,MySQL首先会检查Thread Cache中是否存在空闲连接线程,如果存在则取出来直接使用,如果没有空闲连接线程,才创建新的连接线程。具体参数:
Thread_cache_size:Thread Cache池中应该存放的连接线程数。
Thread_stack:每个连接线程被创建时,MySQL给它分配的内存大小。当MySQL创建一个新的连接线程时,需要给它分配一定大小的内存堆栈空间,以便存放客户端的请求的Query及自身的各种状态和处理信息。
查看连接线程相关的系统变量的设置值: show variables like 'thread%';

mysql> show variables like 'thread%';
+-------------------+--------+
| Variable_name | Value |
+-------------------+--------+
| thread_cache_size | 32 |
| thread_stack | 196608 |
+-------------------+--------+
2 rows in set (0.00 sec)

如图,系统设置了Thread Cache池最多将缓存25个连接线程,每个连接线程创建之初,系统分配192KB的内存堆栈给它。
查看系统被连接的次数及当前系统中连接线程的状态值

mysql> show status like 'connections';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections | 620 |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show status like '%thread%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| Delayed_insert_threads | 0 |
| Slow_launch_threads | 0 |
| Threads_cached | 3 |
| Threads_connected | 4 |
| Threads_created | 7 |
| Threads_running | 1 |
+------------------------+-------+
6 rows in set (0.00 sec)

系统启动到现在共接受到客户端的连接620次,共创建了7个连接线程,当前有1个连接线程处于和客户端连接的状态,而3个连接状态的线程中只有一个处于 active 状态,即只有一个正在处理客户端提交的请求,。而在Thread Cache池中共缓存了3个连接线程。

Thread Cache 命中率:
Thread_Cache_Hit = (Connections - Threads_created) / Connections * 100%;
一般在系统稳定运行一段时间后,Thread Cache命中率应该保持在90%左右才算正常。

实际应用:

针对16G/32G的机器,一般设置 512K

当然如果遇到下面的错误提示就应该考虑增加这个值了。

mysql-debug: Thread stack overrun

bug info

报错信息:
java.sql.SQLException: Thread stack overrun: 5456 bytes used of a 131072 byte stack, and 128000 bytes needed. Use 'mysqld --thread_stack=#' to specify a bigger stack.

官方相应信息:

The default (192KB) is large enough for normal operation. If the thread stack size is too small, it limits the complexity of the SQL statements that the server can handle, the recursion depth of stored procedures, and other memory-consuming actions

可以使用

show variables where `variable_name` = 'thread_stack';

查询当前数据库的默认线程栈的大小,一般情况下都能正常使用,但是当查询语句或者存储过程复杂时会报Thread stack overrun错误,此时只要修改默认配置就可以。

解决

windows: 修改mysql安装目录下的my-small.ini或者my.ini设置为256k,或者更大,然后重启服务

[mysqld]
thread_stack = 256k
linux: 同样要修改配置文件,但是!!!,不是安装目录下的配置文件,是/etc/my.cnf,只有这个文件才能生效,然后重启服务service mysql restart

[mysqld]
thread_stack = 256k


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MYSQL 解锁与锁表介绍发布时间:2022-02-08
下一篇:
CentOS下RPM方式安装MySQL5.6教程发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap