Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在我的服务器上启用MySQL慢速查询日志?_Mysql - Fatal编程技术网

如何在我的服务器上启用MySQL慢速查询日志?

如何在我的服务器上启用MySQL慢速查询日志?,mysql,Mysql,如何在服务器上启用慢速查询日志?我在本地主机上通过在my.ini文件中添加log slow querys=[path]启用了它,但不知道如何在我的服务器上添加它。我的服务器基于Linux,具有PHP版本5.2.16。您可以通过运行以下命令临时设置它: set global slow_query_log = 1; set global slow_query_log_file = '/var/log/mysql-slow.log'; 但当mysql重新启动时,您的更改将被撤消 通过将以下内容添加到

如何在服务器上启用慢速查询日志?我在本地主机上通过在
my.ini
文件中添加
log slow querys=[path]
启用了它,但不知道如何在我的服务器上添加它。我的服务器基于Linux,具有PHP版本5.2.16。

您可以通过运行以下命令临时设置它:

set global slow_query_log = 1;
set global slow_query_log_file = '/var/log/mysql-slow.log';
但当mysql重新启动时,您的更改将被撤消

通过将以下内容添加到您的
my.cnf
文件中,您可以对其进行永久设置:

slow-query-log=1
slow-query-log-file=/var/log/mysql-slow.log
my.cnf
的位置因操作系统而异,但通常在
/etc/my.cnf
/etc/mysql/my.cnf
中找到:

保存更改后,需要重新启动MySql。这可能因操作系统而异,但以下是一些常见的示例:

sudo /etc/init.d/mysqld restart


启用慢速查询日志与PHP版本无关。您必须在MySQL服务器中启用它。您可以通过两种方式启用

  • 运行时
  • 在服务器启动期间
  • 如果您的服务器高于5.1.6,则可以在运行时本身设置慢速查询日志。您必须为其执行此查询

    set global log_slow_queries = 1;
    set global slow_query_log_file = <some file name>;
    
    set global log\u slow\u querys=1;
    设置全局慢速查询日志文件=;
    
    或者,您也可以在my.cnf/my.ini选项文件中设置此选项

    log_slow_queries = 1; 
    slow_query_log_file = <some file name>;
    
    log\u slow\u查询=1;
    慢速查询日志文件=;
    
    如果更改了选项文件,则需要重新启动MySQL服务器

    mysql选项文件的位置可以在这里找到

    仅供参考:
    log\u slow\u querys
    已在MySQL 5.6.1中删除,并改用
    slow\u query\u log

    但是为了提高性能,您可以将日志输出(选项
    log\u output
    )设置为
    。 您还可以查看其他慢速查询日志选项,如
    long\u query\u time
    不使用索引的日志查询

    (作为根:)

    然后在/etc/my.conf中的[mysqld]下(或配置文件所在的位置:)

    下一步:

    最后:

    mysql
    mysql> SELECT SLEEP(10);
    mysql> quit
    
    核查:

    cat /var/log/mysql/log-slow-queries.log
    
    /usr/sbin/mysqld, Version: 5.5.42-cll (MySQL Community Server (GPL)). started with:
    Tcp port: 0  Unix socket: (null)
    Time                 Id Command    Argument
    # Time: 150727  0:05:17
    # User@Host: root[root] @ localhost []
    # Query_time: 10.000215  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
    SET timestamp=1437973517;
    select sleep(10);
    

    当我运行此查询时
    设置全局慢速查询\u log=1
    我收到此错误
    #1193-未知系统变量'slow\u query\u log'
    sry它应该是
    log\u slow\u querys
    ,我将进行必要的编辑。我还运行此命令
    设置全局log\u slow\u querys=1
    现在它显示的是
    #1238-变量“log_slow_querys”是一个只读变量
    此查询结果给出的SELECT@@version;我不知道这个条目是如何被接受的,但我下面的条目没有
    log\u slow\u queryies
    [sic]甚至拼写不正确,在MySQL 5.1.29中被弃用。查看我何时运行此查询
    SET GLOBAL slow\u query\u log=1我收到此错误
    #1193-未知系统变量“slow\u query\u log”
    请不要生气。因为两个答案对我都很有帮助,所以我把两个都投了赞成票。我只能接受一个答案,我也会接受你的答案。我接受他的答案是因为他回应了我的怀疑。
    slow_query_log = 1
    log-queries-not-using-indexes
    long_query_time=1
    log-slow-queries=/var/log/mysql/log-slow-queries.log
    
    service mysql restart
    
    mysql
    mysql> SELECT SLEEP(10);
    mysql> quit
    
    cat /var/log/mysql/log-slow-queries.log
    
    /usr/sbin/mysqld, Version: 5.5.42-cll (MySQL Community Server (GPL)). started with:
    Tcp port: 0  Unix socket: (null)
    Time                 Id Command    Argument
    # Time: 150727  0:05:17
    # User@Host: root[root] @ localhost []
    # Query_time: 10.000215  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
    SET timestamp=1437973517;
    select sleep(10);