mysql最佳化之日誌配置詳解

來源:互聯網
上載者:User


如果你選mysql資料庫作為資料持久化的工具,那麼就需要一個合理的日誌配置,這樣有助於排錯和資料備份及恢複!

首先我們可以通過下面的MySQL的sql查詢正在運行中的日誌配置。

剛好我們熟悉一下SHOW VARIABLES LIKE的用法!這個命令是用來查詢MySQL運行時配置的語句,LIKE後面的可以接萬用字元尋找!

還有對應的設定文法叫SET GLOBAL,後面我們會用到。

mysql> SHOW VARIABLES LIKE '%log%';
+-----------------------------------------+---------------------------------+
| Variable_name                           | Value                           |
+-----------------------------------------+---------------------------------+
| back_log                                | 50                              |
| binlog_cache_size                       | 32768                           |
| binlog_direct_non_transactional_updates | OFF                             |
| binlog_format                           | STATEMENT                       |
| expire_logs_days                        | 0                               |
| general_log                             | OFF                             |
| general_log_file                        | /var/run/mysqld/mysqld.log      |
| innodb_flush_log_at_trx_commit          | 1                               |
| innodb_locks_unsafe_for_binlog          | OFF                             |
| innodb_log_buffer_size                  | 1048576                         |
| 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_bin_trust_routine_creators          | OFF                             |
| log_error                               | /var/log/mysqld.log             |
*| 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                         | 1073741824                      |
| max_relay_log_size                      | 0                               |
| relay_log                               |                                 |
| relay_log_index                         |                                 |
| relay_log_info_file                     | relay-log.info                  |
| relay_log_purge                         | ON                              |
| relay_log_space_limit                   | 0                               |
| slow_query_log                          | OFF                             |
| slow_query_log_file                     | /var/run/mysqld/mysqld-slow.log |
| sql_log_bin                             | ON                              |
| sql_log_off                             | OFF                             |
| sql_log_update                          | ON                              |
| sync_binlog                             | 0                               |
+-----------------------------------------+---------------------------------+
38 rows in set (0.00 sec)

請注意log_output一行,次配置決定將日誌輸出到檔案還是table中。

我們可以通過SHOW VARIABLES LIKE 'log_output';查詢此配置。

I. 通過檔案配置

mysql的設定檔位於 /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
# 錯誤記錄檔
log-error=/var/log/mysql/error_mysql.log
# Slow Query
log-slow-queries=/var/log/mysql/slow_mysql.log
long_query_time=2
# 記錄沒有使用索引的查詢
#log-queries-not-using-indexes

# 常規查詢日誌,老高主要用於調試PDO參數綁定的查詢
general_log=1
general_log_file=/var/log/mysql/general_mysql.log
# < MySql5.1.12
#log = /var/log/mysql/general_mysql.log


[mysqld_safe]
# syslog指將日誌記錄至系統日誌
#syslog
pid-file=/var/run/mysqld/mysqld.pid
log_error=/var/log/mysql/error_mysqld.log

接下來需要重啟伺服器即可是配置生效!

II. 熱配置

有時候可能想在不重啟mysql的方式開啟日誌,怎麼辦?

當然我們可以通過MySQL的sql查詢開啟。

以下操作假設你已經以root方式登入進入MySQL互動介面

常規日誌

# 查看常規日誌配置
SHOW VARIABLES LIKE 'general_log%';
下面我們開啟他

# 設定記錄檔路徑,請先保證路徑存在
SET GLOBAL general_log_file='/var/log/mysql/general_mysql.log';
# 開啟日誌
SET GLOBAL general_log=ON;

# 關閉日誌
SET GLOBAL general_log=OFF;
查看常規日誌配置

錯誤記錄檔

聰明的朋友,既然已經知道SET GLOBAL、SHOW VARIABLES LIKE這兩個命令,我相信其他的日誌配置就難不倒你了。

所以在此老高就不??鋁恕?/p>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.