发布于 2017-05-08 11:06:52 | 107 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Mysql教程,程序狗速度看过来!

Mysql关系型数据库管理系统

MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司。MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。


当有新的连接请求时,MySQL首先会检查Thread Cache中是否存在空闲连接线程,如果存在则取出来直接使用,如果没有空闲连接线程,才创建新的连接线程

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)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务