乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      何時(shí)、如何開啟 MySql 日志?

       小豬窩969 2015-09-21
      假如你是一名 web 開發(fā)人員,如果你想調(diào)試你的應(yīng)用或提升其性能的話,那你需要去參考各種日志文件。日志是開始故障排除最好的選擇。就著名的 MySql 數(shù)據(jù)庫(kù)服務(wù)器而言,你需要參考以下日志文件:
      • 錯(cuò)誤日志:它包含了服務(wù)器運(yùn)行時(shí)(當(dāng)然也包括服務(wù)啟動(dòng)和停止時(shí))所發(fā)生的錯(cuò)誤信息
      • 普通查詢?nèi)罩?/strong>:這是一個(gè)記錄 mysqld 在做什么(連接,斷開,查詢)的通用日志
      • 慢查詢?nèi)罩?/strong>:正如其名,它記錄了 "慢" 的查詢 SQL 語(yǔ)句
      本文未涉及到二進(jìn)制日志。二進(jìn)制日志要求非常高的服務(wù)器硬件配置,而且只是在特定場(chǎng)景下(比如,主從復(fù)制,主從安裝,某些數(shù)據(jù)的恢復(fù)操作)有用。否則的話,它就是一名實(shí)實(shí)在在的 "性能殺手"。

      關(guān)于 MySql 日志的官方文檔參考 http://dev./doc/refman/5.7/en/server-logs.html。


      通過(guò) MySql 配置啟用日志

      日志相關(guān)參數(shù)位于 [mysqld] 部分。
      編輯 MySql 配置文件:
      nano /etc/mysql/my.cnf
      以上是 Debian 下的默認(rèn)安裝目錄,其他 Linux 發(fā)布版可能不太一樣,這個(gè)文件中 MySql 服務(wù)器的參數(shù)如下:
      # * Logging and Replication
      #
      # Both location gets rotated by the cronjob.
      # Be aware that this log type is a performance killer.
      # As of 5.1 you can enable the log at runtime!
      #general_log_file        = /var/log/mysql/mysql.log
      #general_log             = 1
      #
      # Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
      #
      # Here you can see queries with especially long duration
      #log_slow_queries       = /var/log/mysql/mysql-slow.log
      #long_query_time = 2
      #log-queries-not-using-indexes
      #
      # The following can be used as easy to replay backup logs or for replication.
      # note: if you are setting up a replication slave, see README.Debian about
      #       other settings you may need to change.
      #server-id              = 1
      #log_bin                        = /var/log/mysql/mysql-bin.log
      expire_logs_days        = 10
      max_binlog_size         = 100M
      #binlog_do_db           = include_database_name
      #binlog_ignore_db       = include_database_name

      MySql 安裝默認(rèn)是不啟用所有日志文件的(Windows 平臺(tái)下的 error 日志除外)。Debian 安裝 MySql 默認(rèn)是將 error 日志發(fā)送給 syslog。

      error 日志

      根據(jù) /etc/mysql/conf.d/mysqld_safe_syslog.cnf 配置,error 日志推送給 syslog:
      [mysqld_safe]
      syslog

      這是推薦的做法
      。如果你由于某種原因,不想講 error 日志推給 syslog,將 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 文件中的上述行注掉,或者直接刪除掉這個(gè)文件,然后在 /etc/mysql/my.cnf 中添加以下行:
      [mysqld_safe]
      log_error=/var/log/mysql/mysql_error.log
       
      [mysqld]
      log_error=/var/log/mysql/mysql_error.log

      一般查詢?nèi)罩?/h2>要啟用一般查詢?nèi)罩?,將相關(guān)行取消注釋(或者添加)即可:
      general_log_file        = /var/log/mysql/mysql.log
      general_log             = 1

      慢查詢?nèi)罩?/h2>要啟用慢查詢?nèi)罩?,將相關(guān)行取消注釋(或者添加)即可:
      log_slow_queries       = /var/log/mysql/mysql-slow.log
      long_query_time = 2
      log-queries-not-using-indexes

      配置修改后重啟 MySql 服務(wù)器

      以上方法要求服務(wù)重啟才能生效:
      service mysql restart
      或者使用 systemd

      systemctl restart mysql.service


      運(yùn)行時(shí)啟用日志

      MySql 5.1 之后我們可以在運(yùn)行時(shí)啟用或者禁用日志。
      運(yùn)行時(shí)啟用日志,登錄 MySql 客戶端(mysql -u root -p)然后執(zhí)行:
      SET GLOBAL general_log = 'ON';
      SET GLOBAL slow_query_log = 'ON';

      運(yùn)行時(shí)禁用日志,登錄 Mysql 客戶端(mysql -u root -p)后執(zhí)行:
      SET GLOBAL general_log = 'OFF';
      SET GLOBAL slow_query_log = 'OFF';

      這種方式適用于所有平臺(tái)并且不需要重啟服務(wù)。


      顯示日志結(jié)果

      error 日志

      按以上辦法設(shè)置以后,你可以通過(guò)以下命令顯示 error 日志:
      tail -f /var/log/syslog
      備注:如果你沒(méi)有配置 error 日志文件,MySql 將把 error 日志保存在數(shù)據(jù)目錄(通常是 /var/lib/mysql)下的一個(gè)名為 {host_name}.err 的文件中。

      普通查詢?nèi)罩?/h2>按以上辦法設(shè)置以后,你可以通過(guò)使用以下命令來(lái)顯示普通日志:
      tail -f /var/log/mysql/mysql.log
      備注:如果你沒(méi)有配置普通日志文件,MySql 將把普通日志保存在數(shù)據(jù)目錄(通常是 /var/lib/mysql)下的一個(gè)名為 {host_name}.log 的文件中。

      慢查詢?nèi)罩?/h2>按以上辦法設(shè)置以后,你可以通過(guò)使用以下命令來(lái)顯示慢查詢?nèi)罩荆?br>tail -f /var/log/mysql/mysql-slow.log

      備注:如果你沒(méi)有配置慢查詢?nèi)罩疚募?,MySql 將把普通日志保存在數(shù)據(jù)目錄(通常是 /var/lib/mysql)下的一個(gè)名為 {host_name}-slow.log 的文件中。


      循環(huán)日志

      別忘了滾動(dòng)日志,否則的話日志文件可能會(huì)變得很龐大。
      Debian(以及 Debian 派生系列諸如 Ubuntu 等)系統(tǒng),MySql 初始安裝之后,循環(huán)日志就已經(jīng)使用了 logrotate:
      nano /etc/logrotate.d/mysql-server
      對(duì)于其他 Linux 發(fā)行版,可能需要做一些改動(dòng):
      1. # - I put everything in one block and added sharedscripts, so that mysql gets  
      2. #   flush-logs'd only once.  
      3. #   Else the binary logs would automatically increase by n times every day.  
      4. # - The error log is obsolete, messages go to syslog now.  
      5. /var/log/mysql.log /var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log {  
      6.         daily  
      7.         rotate 7  
      8.         missingok  
      9.         create 640 mysql adm  
      10.         compress  
      11.         sharedscripts  
      12.         postrotate  
      13.                 test -x /usr/bin/mysqladmin || exit 0  
      14.                 # If this fails, check debian.conf!  
      15.                 MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"  
      16.                 if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then  
      17.                   # Really no mysqld or rather a missing debian-sys-maint user?  
      18.                   # If this occurs and is not a error please report a bug.  
      19.                   #if ps cax | grep -q mysqld; then  
      20.                   if killall -q -s0 -umysql mysqld; then  
      21.                     exit 1  
      22.                   fi  
      23.                 else  
      24.                   $MYADMIN flush-logs  
      25.                 fi  
      26.         endscript  
      27. }  


      檢驗(yàn)服務(wù)器配置

      使用 show variables like '%log%'; 來(lái)檢查服務(wù)器和日志文件相關(guān)的變量:
      root@cosmos ~ # mysql -uroot -p
      Enter password:
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 144332
      Server version: 5.5.31-0+wheezy1 (Debian)
       
      Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
       
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
       
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
       
      mysql> show variables like '%log%';
      +-----------------------------------------+--------------------------------+
      | Variable_name                           | Value                          |
      +-----------------------------------------+--------------------------------+
      | back_log                                | 50                             |
      | binlog_cache_size                       | 32768                          |
      | binlog_direct_non_transactional_updates | OFF                            |
      | binlog_format                           | STATEMENT                      |
      | binlog_stmt_cache_size                  | 32768                          |
      | expire_logs_days                        | 10                             |
      | general_log                             | OFF                            |
      | general_log_file                        | /var/lib/mysql/cosmos.log      |
      | innodb_flush_log_at_trx_commit          | 1                              |
      | innodb_locks_unsafe_for_binlog          | OFF                            |
      | innodb_log_buffer_size                  | 8388608                        |
      | innodb_log_file_size                    | 5242880                        |
      | innodb_log_files_in_group               | 2                              |
      | innodb_log_group_home_dir               | ./                             |
      | innodb_mirrored_log_groups              | 1                              |
      | log                                     | OFF                            |
      | log_bin                                 | OFF                            |
      | log_bin_trust_function_creators         | OFF                            |
      | log_error                               |                                |
      | log_output                              | FILE                           |
      | log_queries_not_using_indexes           | OFF                            |
      | log_slave_updates                       | OFF                            |
      | log_slow_queries                        | OFF                            |
      | log_warnings                            | 1                              |
      | max_binlog_cache_size                   | 18446744073709547520           |
      | max_binlog_size                         | 104857600                      |
      | max_binlog_stmt_cache_size              | 18446744073709547520           |
      | max_relay_log_size                      | 0                              |
      | relay_log                               |                                |
      | relay_log_index                         |                                |
      | relay_log_info_file                     | relay-log.info                 |
      | relay_log_purge                         | ON                             |
      | relay_log_recovery                      | OFF                            |
      | relay_log_space_limit                   | 0                              |
      | slow_query_log                          | OFF                            |
      | slow_query_log_file                     | /var/lib/mysql/cosmos-slow.log |
      | sql_log_bin                             | ON                             |
      | sql_log_off                             | OFF                            |
      | sync_binlog                             | 0                              |
      | sync_relay_log                          | 0                              |
      | sync_relay_log_info                     | 0                              |
      +-----------------------------------------+--------------------------------+
      41 rows in set (0.00 sec)

      服務(wù)器變量相關(guān)官方文檔參考 http://dev./doc/refman/5.7/en/server-options.html。


      何時(shí)啟用日志

      MySql 默認(rèn)安裝的話,所有的日志文件都不會(huì)被啟用的(除了 Windows 平臺(tái)上的 error 日志)。Debian 上安裝默認(rèn)將 error 日志發(fā)給 syslog。
      實(shí)際上,在很多情況下日志文件都可以提供關(guān)鍵問(wèn)題的解決辦法:
      • 總是啟用 error 日志
      • 在這些情況下開啟普通查詢?nèi)罩?最好在運(yùn)行時(shí)):檢查你的應(yīng)用是否正確處理了 MySql 數(shù)據(jù)庫(kù)連接(一個(gè)常見的錯(cuò)誤就是從一個(gè)單一腳本多次連接到 MySql);監(jiān)控來(lái)自你的應(yīng)用的查詢的執(zhí)行情況;測(cè)試 memcached(或者類似的軟件),檢查某查詢是被 db 執(zhí)行還是被 memcached 處理
      • 當(dāng)你的應(yīng)用由于某些原因造成性能下降而你想找到這些慢查詢時(shí),啟用慢查詢?nèi)罩?MySql 最好是在短期內(nèi)這樣配置,比如 2-3 天)


      示例

      以下是一個(gè) MySql 普通日志的示例:
      131021 17:43:50    43 Connect root@localhost as anonymous on pnet_blog
             43 Init DB pnet_blog
             43 Query SELECT count(id) as total_posts FROM posts WHERE date_published is not null AND date_published <= '20131021144350'
             43 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0,10
             44 Connect root@localhost as anonymous on pnet_blog
             44 Query SELECT id, title, impressions FROM tips WHERE date_published IS NOT NULL AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0, 10
             44 Quit
             43 Quit
      131021 17:44:28    45 Connect root@localhost as anonymous on pnet_blog
             45 Init DB pnet_blog
             45 Query SELECT * FROM posts WHERE url='how-and-when-to-enable-mysql-logs'
             45 Query UPDATE posts SET impressions=impressions+1 WHERE id='41'
             45 Query SELECT url, post_title FROM posts WHERE date_published IS NOT NULL AND date_published < '20131020150000' ORDER BY date_published DESC LIMIT 0,1
             45 Query SELECT url, post_title FROM posts WHERE date_published IS NOT NULL AND date_published > '20131020150000' ORDER BY date_published ASC LIMIT 0,1
             45 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144428' AND date_published >= '20130421144428' ORDER BY impressions DESC LIMIT 0,10
             46 Connect root@localhost as anonymous on pnet_blog
             46 Query SELECT id, title, impressions FROM tips WHERE date_published IS NOT NULL AND date_published <= '20131021144428' ORDER BY date_published DESC LIMIT 0, 10
             46 Quit
             45 Quit  
          
      原文鏈接:http://www./blog/how-and-when-to-enable-mysql-logs。   

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多