Mysql kích hoạt nhật ký chung mà không cần khởi động lại
Nếu bạn là nhà phát triển web, bạn cần tham khảo các tệp nhật ký khác nhau để gỡ lỗi ứng dụng của mình hoặc cải thiện hiệu suất của ứng dụng. Nhật ký là nơi tốt nhất để bắt đầu khắc phục sự cố. Liên quan đến máy chủ cơ sở dữ liệu MySQL nổi tiếng (hoặc máy chủ MariaDB), bạn cần tham khảo các tệp nhật ký sau Show
Bài viết này không đề cập đến Nhật ký nhị phân. Điều này đòi hỏi phần cứng máy chủ tiêu chuẩn rất cao và chỉ hữu ích trong những trường hợp đặc biệt (e. g. sao chép, thiết lập chính – phụ, một số hoạt động khôi phục dữ liệu). Îmặt khác, nó chỉ là một “kẻ giết người hiệu suất” Tài liệu chính thức về nhật ký MySQL có tại đây Bật nhật ký từ cấu hình MySQLCác tham số ghi nhật ký được đặt trong phần [mysqld] Chỉnh sửa tệp cấu hình MySQL nano /etc/mysql/my.cnf Đây là thiết lập mặc định cho Ghi nhật ký và Sao chép (trong máy chủ Debian). Trong các bản phân phối khác, cấu trúc có thể khác, nhưng bạn luôn có thể sử dụng các tham số máy chủ MySQL # * 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 Tất cả các tệp nhật ký KHÔNG được bật theo thiết lập MySQL mặc định (ngoại trừ nhật ký lỗi trên Windows). Thiết lập Debian mặc định gửi Nhật ký lỗi tới nhật ký hệ thống. Các tệp nhật ký khác không được bật Nhật ký lỗiNhật ký lỗi chuyển đến nhật ký hệ thống do/etc/mysql/conf. d/mysqld_safe_syslog. cnf, trong đó có những điều sau đây [mysqld_safe] syslog Đây là phương pháp được khuyến khích. Nếu vì lý do nào đó, bạn không muốn Error log vào syslog, hãy bình luận những dòng trên trong /etc/mysql/conf. d/mysqld_safe_syslog. cnf hoặc xóa hoàn toàn tệp này. Sau đó, thêm vào /etc/mysql/my. cnf các dòng sau [mysqld_safe] log_error=/var/log/mysql/mysql_error.log [mysqld] log_error=/var/log/mysql/mysql_error.log Nhật ký truy vấn chungĐể bật Nhật ký truy vấn chung, hãy bỏ ghi chú (hoặc thêm) các dòng có liên quan general_log_file = /var/log/mysql/mysql.log general_log = 1 Nhật ký truy vấn chậmĐể bật Nhật ký truy vấn chậm, hãy bỏ ghi chú (hoặc thêm) các dòng có liên quan log_slow_queries = /var/log/mysql/mysql-slow.log long_query_time = 2 log-queries-not-using-indexes Khởi động lại máy chủ MySQL sau khi thay đổiPhương pháp này yêu cầu khởi động lại máy chủ service mysql restart hoặc, sử dụng systemd systemctl restart mysql.service Kích hoạt nhật ký trong thời gian chạyKể từ MySQL 5. 1 bạn có thể bật và tắt nhật ký khi chạy Để bật nhật ký trong thời gian chạy, hãy đăng nhập vào máy khách mysql ( # * 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_name7 ) và cung cấp SET GLOBAL general_log = 'ON'; SET GLOBAL slow_query_log = 'ON'; Để tắt nhật ký trong thời gian chạy, hãy đăng nhập vào ứng dụng khách mysql ( # * 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_name7) và cung cấp SET GLOBAL general_log = 'OFF'; SET GLOBAL slow_query_log = 'OFF'; Phương pháp này hoạt động trên mọi nền tảng và không yêu cầu khởi động lại máy chủ Hiển thị kết quả nhật kýNhật ký lỗiVới các cài đặt trên, bạn có thể hiển thị Nhật ký lỗi bằng cách sử dụng # * 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_name0 NHẬN XÉT. Nếu bạn không chỉ định tệp nhật ký Lỗi, MySQL sẽ giữ Nhật ký lỗi tại thư mục dữ liệu (thường là /var/lib/mysql trong tệp có tên {host_name}. lỗi) Nhật ký truy vấn chungVới các cài đặt trên, bạn có thể hiển thị Nhật ký chung bằng cách sử dụng # * 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_name1 NHẬN XÉT. Nếu bạn không xác định tệp nhật ký Chung, MySQL sẽ giữ nhật ký Chung tại thư mục dữ liệu (thường là /var/lib/mysql trong tệp có tên {host_name}. nhật ký) Nhật ký truy vấn chậmVới các cài đặt ở trên, bạn có thể hiển thị nhật ký Truy vấn chậm bằng cách sử dụng # * 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_name2 NHẬN XÉT. Nếu bạn không chỉ định tệp nhật ký Truy vấn chậm, MySQL sẽ giữ nhật ký Truy vấn chậm tại thư mục dữ liệu (thường là /var/lib/mysql trong tệp có tên {host_name}-slow. nhật ký) Xoay vòng nhật kýKHÔNG bao giờ quên xoay nhật ký. Nếu không, tệp nhật ký có thể trở nên lớn Trong Debian (và các dẫn xuất của Debian như Ubuntu, v.v.) xoay vòng nhật ký bằng cách sử dụng logrotate đã có sau khi thiết lập máy chủ ban đầu (“cấu hình trước các gói Debian”) # * 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_name3 trong các bản phân phối khác, một số thay đổi có thể cần thiết # * 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_name4 Kiểm tra cấu hình máy chủ của bạnTIỀN BOA. Sử dụng # * 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_name9 để kiểm tra các biến máy chủ của bạn liên quan đến tệp nhật ký # * 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_name5 Tài liệu chính thức về biến máy chủ có sẵn tại đây Khi nào nên bật nhật kýSử dụng thiết lập MySQL mặc định, tất cả các tệp nhật ký KHÔNG được bật (ngoại trừ nhật ký lỗi trên Windows). Thiết lập Debian mặc định gửi Nhật ký lỗi tới nhật ký hệ thống Trên thực tế, có nhiều trường hợp tệp nhật ký có thể cung cấp giải pháp cho các sự cố nghiêm trọng
Thí dụDưới đây là một ví dụ về nhật ký truy vấn chung của MySQL # * 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_name6 Christos Pontikis doanh nhân. Lập trình viên full stack. Nhà sáng lập MediSign Ltd. Tôi có hơn 15 năm kinh nghiệm chuyên môn thiết kế và phát triển các ứng dụng web. Tôi cũng rất có kinh nghiệm trong việc quản lý các dự án (web) Làm cách nào để bật nhật ký trong MySQL?Để bật tệp nhật ký, hãy làm như sau. . Tạo /etc/my. tệp tùy chọn cnf với các định nghĩa sau đây sẽ tự động kích hoạt các tệp nhật ký. [mysqld] nhật ký log-bin nhật ký lỗi nhật ký truy vấn chậm. . Dừng và khởi động máy chủ MySQL để kích hoạt các thay đổi đối với /etc/my Nhật ký chung trong MySQL là gì?Nhật ký truy vấn chung có thể được ghi vào một tệp trên đĩa hoặc có thể được ghi vào bảng general_log trong cơ sở dữ liệu mysql. Để chọn đích đầu ra của nhật ký truy vấn chung, hãy đặt biến hệ thống log_output
Làm cách nào để bật nhật ký gỡ lỗi trong MySQL?Nhập lệnh sau tại dấu nhắc mysql để kích hoạt gỡ lỗi. đặt global general_log = 'on';
Làm cách nào để kiểm tra nhật ký chung trong MySQL?Làm cách nào để hiển thị nhật ký truy vấn trong MySQL? . Cho phép ghi nhật ký truy vấn trên cơ sở dữ liệu. SET toàn cầu general_log = 1; Bây giờ bạn có thể xem nhật ký bằng cách chạy truy vấn này. CHỌN * TỪ mysql. chung_log; Nếu bạn muốn tắt ghi nhật ký truy vấn trên cơ sở dữ liệu, hãy chạy truy vấn này. SET toàn cầu general_log = 0; |