zoukankan      html  css  js  c++  java
  • Mysql配置查询

     查看mysql数据库的线程数:

    show global status like 'Thread%';

    如果我们在MySQL服务器配置文件中设置了thread_cache_size,当客户端断开之后,服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。
    Threads_created表示创建过的线程数,如果发现Threads_created值过大的话,表明MySQL服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,查询服务器thread_cache_size配置:

    show variables like 'thread_cache_size';

    查看mysql连接配置信息:

    show variables like '%connect%';

    最大连接数max_connections这个参数的大小要综合很多因素来考虑,比如使用的平台所支持的线程库数量(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的命令杀死它。

    参考资料:https://www.jb51.net/article/137082.htm

  • 相关阅读:
    使用springamqp发送消息及同步接收消息
    对未登陆的用户进行处理的页面
    查找某些字符是否在另一个字符串里出现的高效算法
    正则表达式
    华中地区高校第七届ACM程序设计大赛——递增序列【2012年5月27日】
    HDOJ2021 ( 发工资咯:) ) 【水题】
    循环冗余校验(CRC)【C语言 位运算】
    HDOJ2028 ( Lowest Common Multiple Plus ) 【水题,lcm】
    HDOJ2015 ( 偶数求和 ) 【水题】
    HDOJ2027 ( 统计元音 ) 【水题】
  • 原文地址:https://www.cnblogs.com/wx170119/p/11313464.html
Copyright © 2011-2022 走看看