mysql查看sql語句執行記錄的例子

來源:互聯網
上載者:User


mysql查看sql語句執行記錄

 cat ~/.mysql_history
對於mysql版本5.1以後的版本,可以通過以下方式開機記錄記錄。能夠記錄下包括應用程式執行的sql語句。

MariaDB [(none)]> SET GLOBAL log_output = 'TABLE';
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> SET GLOBAL general_log = 'ON';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| gfan_log |
| gfan_pay |
| gfanpg |
| gfanrc |
| mysql |
| performance_schema |
| ucenter |
+--------------------+
8 rows in set (0.02 sec)

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)

MariaDB [mysql]> select * from general_log limit 10;
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------+
| 2014-11-12 14:29:49.810999 | root[root] @ localhost [] | 1304 | 0 | Query | show databases |
| 2014-11-12 14:29:51.951747 | root[root] @ localhost [] | 1304 | 0 | Query | SELECT DATABASE() |
| 2014-11-12 14:29:51.973180 | root[root] @ localhost [] | 1304 | 0 | Init DB | mysql |
| 2014-11-12 14:29:51.975048 | root[root] @ localhost [] | 1304 | 0 | Query | show databases |
| 2014-11-12 14:29:51.975689 | root[root] @ localhost [] | 1304 | 0 | Query | show tables |
| 2014-11-12 14:29:51.976347 | root[root] @ localhost [] | 1304 | 0 | Field List | columns_priv |
| 2014-11-12 14:29:51.976800 | root[root] @ localhost [] | 1304 | 0 | Field List | db |
| 2014-11-12 14:29:51.977431 | root[root] @ localhost [] | 1304 | 0 | Field List | event |
| 2014-11-12 14:29:51.978052 | root[root] @ localhost [] | 1304 | 0 | Field List | func |
| 2014-11-12 14:29:51.978224 | root[root] @ localhost [] | 1304 | 0 | Field List | general_log |
+----------------------------+---------------------------+-----------+-----------+--------------+-------------------+
10 rows in set (0.01 sec)

MariaDB [mysql]>


If you want to output to the log file:
SET GLOBAL log_output = “FILE”;
SET GLOBAL general_log_file = “/path/to/your/logfile.log”
SET GLOBAL general_log = ‘ON’;
Restart MySQL to apply the changes if you edit the config, e.g. /etc/mysql/my.cnf
Now, if you’d like you can tail -f /var/log/mysql/mysql.log

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.