Enable MySQL General log dynamically

Source: Internet
Author: User

This problem is often encountered: the database has a large access volume and needs to be optimized from the SQL aspect. Developers often ask: Can you see which SQL statements are executed frequently? Return: No. You can only view the currently running SQL statements and the SQL statements recorded in the slow log.

Generally, General log is not enabled for performance purposes. Slow log can locate SQL statements with performance problems, while general log records all SQL statements.

For MySQL, If You Want To enable slow log and general log, you need to restart. From MySQL 5.1.6, general query log and slow query log support writing to files or database tables, in addition, logs can be dynamically modified at the global level when they are enabled and output.

Root @ (none) 09:40:33> select version ();
+ ---- +
| Version () |
+ ---- +
| 5.1.37-log |
+ ---- +
1 row in SET (0.02 Sec)

Set the log output mode to a file (if log_output = table is set, the log results will be recorded in the table named gengera_log, and the default engine of this table is CSV ):

Root @ (none) 09:41:11> set global log_output = file;
Query OK, 0 rows affected (0.00 Sec)

Set the log file path for general log:

Root @ (none) 09:45:06> set global general_log_file = '/tmp/General. log ';
Query OK, 0 rows affected (0.00 Sec)

Enable General log:

Root @ (none) 09:45:22> set global general_log = on;
Query OK, 0 rows affected (0.02 Sec)

After a while, close the General log:

Root @ (none) 09:45:31> set global general_log = off;
Query OK, 0 rows affected (0.02 Sec)

Check the tmp/General. log information. You can see which SQL queries, updates, deletes, and inserts frequently. For example, if some tables do not change frequently and the query volume is large, the data can be fully cached. If tables with low requirements on Master/Slave latency, the data can be read to the slave database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.