mysql重启敕令:
/etc/init.d/mysql restart
MySQL服务器的线程数须要在一个合理的局限以内,如许才保证MySQL服务器康健安稳地运转。Threads_created示意竖立过的线程数,经由过程检察Threads_created就可以够检察MySQL服务器的历程状况。
mysql> show global status like 'Thread%'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | Threads_cached | 46 | | Threads_connected | 2 | | Threads_created | 570 | | Threads_running | 1 | +-------------------+-------+
假如我们在MySQL服务器设置文件中设置了thread_cache_size,当客户端断开以后,服务器处置惩罚此客户的线程将会缓存起来以响应下一个客户而不是烧毁(条件是缓存数未达上限)。
Threads_created示意竖立过的线程数,假如发明Threads_created值过大的话,表明MySQL服务器一直在竖立线程,这也是比较耗资本,能够恰当增添设置文件中thread_cache_size值,查询服务器
thread_cache_size设置:
mysql> show variables like 'thread_cache_size'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | thread_cache_size | 64 | +-------------------+-------+
示例中的服务器照样挺康健的。
剖析MySQL与衔接数相干的几个参数
MySQL的variables和status是治理保护的利器,就相似Oracle的spfile和v$表。
MySQL经由过程体系变量纪录许多设置信息,比方最大衔接数max_connections:
mysql> show variables like '%connect%'; +--------------------------+-----------------+ | Variable_name | Value | +--------------------------+-----------------+ | character_set_connection | utf8 | | collation_connection | utf8_general_ci | | connect_timeout | 10 | | init_connect | SET NAMES utf8 | | max_connect_errors | 10 | | max_connections | 200 | | max_user_connections | 0 | +--------------------------+-----------------+ 7 rows in set (0.00 sec)
这 个参数是指同时衔接上来的客户端数量,在5.1版本里默许的值是151,那末现实支撑的衔接数是这个值加一,也就是152,由于要为体系治理员登录上来查 看信息保存一个衔接。这个参数的大小要综合许多因夙来斟酌,比方运用的平台所支撑的线程库数量(windows只能支撑到2048)、服务器的设置(迥殊 是内存大小)、每一个衔接占用资本(内存和负载)的若干、体系须要的响应时刻等。平常Linux体系支撑到几百并发是没有任何问题的。能够在global或 session局限内修正这个参数:
mysql> set global max_connections=151; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%connect%'; +--------------------------+-----------------+ | Variable_name | Value | +--------------------------+-----------------+ | character_set_connection | utf8 | | collation_connection | utf8_general_ci | | connect_timeout | 10 | | init_connect | SET NAMES utf8 | | max_connect_errors | 10 | | max_connections | 151 | | max_user_connections | 0 | +--------------------------+-----------------+ 7 rows in set (0.00 sec)
然则要注意的是,衔接数的增添会带来许多连锁反应,须要在现实中防止由此发生的负面影响。
起首我们看一下status的输出:
mysql> status -------------- mysql Ver 14.14 Distrib 5.1.49, for pc-linux-gnu (i686) using readline 5.1 Connection id: 255260 Current database: mysql Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.1.49-log MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 161 days 3 hours 42 min 38 sec Threads: 14 Questions: 160655492 Slow queries: 71 Opens: 8124 Flush tables: 3 Open tables: 64 Queries per second avg: 11.538 --------------
这 里有个Open tables
输出时64,这就是说当前数据库翻开的表的数量是64个,要注意的是这个64并非现实的64个表,由于MySQL是多线程的体系,几个差别 的并发衔接能够翻开同一个表,这就须要为差别的衔接session分派自力的内存空间来存储这些信息以防止争执。因而衔接数的增添会致使MySQL须要的 文件描述符数量的增添。别的关于MyISAM表,还会竖立一个同享的索引文件描述符。
那末在MySQL数据库层面,有几个体系参数决议了可同时翻开的表的数量和要运用的文件描述符,那就是table_open_cache、max_tmp_tables和open_files_limit.
mysql> show variables like 'table_open%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | table_open_cache | 64 | +------------------+-------+ 1 row in set (0.00 sec)
这 里的table_open_cache 参数是64,这就是说一切的MySQL线程一共能同时翻开64个表,我们能够汇集体系的翻开表的数量的汗青纪录和这个参数来对照,决议是不是要增添这个参数 的大小。检察当前的翻开表的数量的方法一个是用上边提到过的status
敕令,别的能够直接查询这个体系变量的值:
mysql> show status like 'open%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | Open_files | 3 | | Open_streams | 0 | | Open_table_definitions | 8 | | Open_tables | 8 | | Opened_files | 91768 | | Opened_table_definitions | 0 | | Opened_tables | 0 | +--------------------------+-------+ 7 rows in set (0.00 sec) mysql> show global status like 'open%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | Open_files | 3 | | Open_streams | 0 | | Open_table_definitions | 10 | | Open_tables | 11 | | Opened_files | 91791 | | Opened_table_definitions | 1211 | | Opened_tables | 8158 | +--------------------------+-------+ 7 rows in set (0.00 sec)
这 里有Open_tables就是当前翻开表的数量,经由过程flush tables敕令能够封闭当前翻开的表。而全局局限内检察的Opened_tables是个汗青累计值。 这个值假如过大,而且假如没有常常的实行flush tables
敕令,能够斟酌增添table_open_cache参数的大小。
接下来看max_tmp_tables 参数:
mysql> show variables like 'max_tmp%'; +----------------+-------+ | Variable_name | Value | +----------------+-------+ | max_tmp_tables | 32 | +----------------+-------+ 1 row in set (0.00 sec)
这个参数指定的是单个客户端衔接能翻开的暂时表数量。检察当前已翻开的暂时表信息:
mysql> show global status like '%tmp%table%'; +-------------------------+-------+ | Variable_name | Value | +-------------------------+-------+ | Created_tmp_disk_tables | 10478 | | Created_tmp_tables | 25860 | +-------------------------+-------+ 2 rows in set (0.00 sec)
也 能够对照这两个值来推断暂时表的竖立位置,平常拔取BLOB和TEXT列、Group by 和 Distinct语句的数据量凌驾512 bytes,或许union的时刻select某列的数据凌驾512 bytes的时刻,就直接在磁盘上竖立暂时表了,别的内存中的暂时表变大的时刻,也能够被MySQL自动转移到磁盘上(由tmp_table_size和 max_heap_table_size参数决议)。
继承本来的议论,增添table_open_cache或 max_tmp_tables 参数的大小后,从操作体系的角度看,mysqld历程须要运用的文件描述符的个数就要响应的增添,这个是由 open_files_limit参数掌握的。然则这个参数是OS限定的,所以我们设定的值并不一定老是见效。假如OS限定MySQL不能修正这个值,那 么置为0。假如是专用的MySQL服务器上,这个值平常要设置的尽量大,就是没有报Too many open files毛病的最大值,如许就可以一劳永逸了。当操作体系没法分派充足的文件描述符的时刻,mysqld历程会在毛病日记里纪录正告信息。
mysql> show variables like 'open_files%';+------------------+-------+| Variable_name | Value |+------------------+-------+| open_files_limit | 1024 |+------------------+-------+1 row in set (0.00 sec) mysql> show variables like 'open_files%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | open_files_limit | 1024 | +------------------+-------+ 1 row in set (0.00 sec)
对应的,有两个状况变量纪录了当前和汗青的文件翻开信息:
mysql> show global status like '%open%file%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Open_files | 3 | | Opened_files | 91799 | +---------------+-------+ 2 rows in set (0.01 sec)
MySQL为每一个衔接分派线程来处置惩罚,能够经由过程threads_connected参数检察当前分派的线程数量:
mysql> show status like '%thread%'; +------------------------+--------+ | Variable_name | Value | +------------------------+--------+ | Delayed_insert_threads | 0 | | Slow_launch_threads | 0 | | Threads_cached | 0 | | Threads_connected | 14 | | Threads_created | 255570 | | Threads_running | 2 | +------------------------+--------+ 6 rows in set (0.00 sec)
比较这个threads_connected参数和前面提到的max_connections参数,也能够作为现在的体系负载的参照,决议是不是须要修正衔接数。
假如检察每一个thread的更细致的信息,能够运用processlist
敕令:
mysql> show processlist; +--------+-----------+--------------------+----------+-------------+----------+----------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +--------+-----------+--------------------+----------+-------------+----------+----------------------------------------------------------------+------------------+ | 8293 | repl | 192.168.0.33:47208 | NULL | Binlog Dump | 11574424 | Has sent all binlog to slave; waiting for binlog to be updated | NULL | | 140991 | mogile | 192.168.0.33:41714 | mogilefs | Sleep | 0 | | NULL | | 140992 | mogile | 192.168.0.33:41715 | mogilefs | Sleep | 3 | | NULL | | 140993 | mogile | 192.168.0.33:41722 | mogilefs | Sleep | 2 | | NULL | | 140994 | mogile | 192.168.0.33:41723 | mogilefs | Sleep | 1 | | NULL | | 140995 | mogile | 192.168.0.33:41724 | mogilefs | Sleep | 3 | | NULL | | 254914 | mogile | 192.168.0.33:43028 | mogilefs | Sleep | 11074 | | NULL | | 254915 | mogile | 192.168.0.33:43032 | mogilefs | Sleep | 11091 | | NULL | | 255144 | mogile | 192.168.0.33:47514 | mogilefs | Sleep | 11090 | | NULL | | 255157 | mogile | 192.168.0.33:47535 | mogilefs | Sleep | 11087 | | NULL | | 255162 | mogile | 192.168.0.33:47549 | mogilefs | Sleep | 11074 | | NULL | | 255260 | root | localhost | mysql | Query | 0 | NULL | show processlist | | 255352 | maopaodev | 192.168.0.78:55399 | maopaodb | Sleep | 3172 | | NULL | | 255353 | maopaodev | 192.168.0.78:55400 | NULL | Sleep | 8926 | | NULL | +--------+-----------+--------------------+----------+-------------+----------+----------------------------------------------------------------+------------------+ 14 rows in set (0.00 sec)
实行这个敕令须要有Process_priv权限,详细的权限分派信息能够检察mysql.user表。
关于影响体系运转的thread,能够狠一点,用kill connection
|query threadid
的敕令杀死它。
相干引荐:
mysql服务器中主从设置引见
怎样修正MYSQL服务器默许字符集
mysql服务器查询慢缘由剖析与解决要领小结
以上就是检察MySQL服务器线程数的要领的细致内容,更多请关注ki4网别的相干文章!