zoukankan      html  css  js  c++  java
  • mysql---慢查询

    mysql有些sql会执行很慢,有可能造成服务器负载飙升

    首先查询 确定影响负载的是mysql ,使用top命令,ps命令等

    其次,进入MySQL,使用show full processlist查询执行中的sql语句,看看问题,使用explain 命令 查看状态

    最后找出sql语句杀死或者优化

    centos7上面安装mariadb服务

    yum -y install mariadb-server mariadb-devel

    开启慢查询

    more /etc/my.cnf.d/server.cnf 
    
    [mariadb]
    slow_query_log=ON
    slow_query_log_file=/usr/local/mysql/data/slow.log
    long_query_time=1

    启动mariadb服务

    systemctl start mariadb

    查询mysql的慢查询是否开启,以及多久的时间以上是慢查询

    MariaDB [(none)]> show variables like '%slow_query%';
    +---------------------+--------------------------------+
    | Variable_name       | Value                          |
    +---------------------+--------------------------------+
    | slow_query_log      | ON                             |
    | slow_query_log_file | /usr/local/mysql/data/slow.log |
    +---------------------+--------------------------------+
    2 rows in set (0.00 sec)
    
    
    MariaDB [(none)]> show variables like 'long_query_time';
    +-----------------+----------+
    | Variable_name   | Value    |
    +-----------------+----------+
    | long_query_time | 1.000000 |
    +-----------------+----------+
    1 row in set (0.00 sec)

    #如果没用开启慢查询,可以在命令行开启
    mysql> set global slow_query_log=1;
    Query OK, 0 rows affected (0.00 sec)

     测试慢查询,以及查看日志

    MariaDB [(none)]> select sleep(2);
    +----------+
    | sleep(2) |
    +----------+
    |        0 |
    +----------+
    1 row in set (2.00 sec)
    
    [root@localhost ~]# more /usr/local/mysql/data/slow.log
    /usr/libexec/mysqld, Version: 5.5.60-MariaDB (MariaDB Server). started with:
    Tcp port: 0  Unix socket: /var/lib/mysql/mysql.sock
    Time                 Id Command    Argument
    # Time: 180930 23:51:07
    # User@Host: root[root] @ localhost []
    # Thread_id: 2  Schema:   QC_hit: No
    # Query_time: 2.001017  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
    SET timestamp=1538322667;
    select sleep(2);

     确认慢查询

    MariaDB [(none)]> show full processlist;  #查看state慢查询在进行
    +----+------+-----------+------+---------+------+------------+-----------------------+----------+
    | Id | User | Host      | db   | Command | Time | State      | Info                  | Progress |
    +----+------+-----------+------+---------+------+------------+-----------------------+----------+
    |  3 | root | localhost | NULL | Query   |    9 | User sleep | select sleep(10)      |    0.000 |
    |  4 | root | localhost | NULL | Query   |    0 | NULL       | show full processlist |    0.000 |
    +----+------+-----------+------+---------+------+------------+-----------------------+----------+
    2 rows in set (0.00 sec)
    
    MariaDB [(none)]> show full processlist; #查看state慢查询已经结束,但是用户登陆了
    +----+------+-----------+------+---------+------+-------+-----------------------+----------+
    | Id | User | Host      | db   | Command | Time | State | Info                  | Progress |
    +----+------+-----------+------+---------+------+-------+-----------------------+----------+
    |  3 | root | localhost | NULL | Sleep   |    1 |       | NULL                  |    0.000 |
    |  4 | root | localhost | NULL | Query   |    0 | NULL  | show full processlist |    0.000 |
    +----+------+-----------+------+---------+------+-------+-----------------------+----------+
    2 rows in set (0.00 sec)
  • 相关阅读:
    git命令_保存本地变更+拉取+合并代码+推送代码到远程仓+添加CI触发器变量
    debian_linux_apt-get命令_dpkg命令
    debian_linux系统_访问真实环境rancher_证书问题相关_https相关_使用kubectl命令行查看资源时报错:Unable to connect to the server: x509: certificate signed by unknown authority
    linux_xargs入门介绍_及和for循环命令区别
    技术实践丨GaussDB(DWS)运维管理功能“升级”的原理和使用
    华为云数据安全中心正式公测,8大核心数据安全能力守护你的数据
    华为鲲鹏专家解读:90%代码如何移植到鲲鹏平台
    数据库技术丨GaussDB(DWS)数据同步状态查看方法
    你掉进过“伪敏捷”的陷阱吗?
    拯救深度学习:标注数据不足下的深度学习方法
  • 原文地址:https://www.cnblogs.com/mmyy-blog/p/9732183.html
Copyright © 2011-2022 走看看