己所不欲勿施于人!
全部博文(80)
分类: Mysql/postgreSQL
2016-09-30 16:42:17
日志文件记录了影响MySQL数据库的各种类型活动,MySQL数据库中常见的日志文件有错误日志、二进制日志、慢查询日志。
这些日志文件为DBA对数据库优化、问题查找带来了极大的便利。
"日志输出方式" mysql> show variables like 'log_output%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | FILE | +---------------+-------+ 1 row in set (0.00 sec) "更改为TABLE" mysql> set global log_output='TABLE'; Query OK, 0 rows affected (0.00 sec) "输出内容" mysql> select event_time,user_host,argument from general_log; +---------------------+---------------------------+--------------------------------------------------------+ | event_time | user_host | argument | +---------------------+---------------------------+--------------------------------------------------------+ | 2016-09-30 14:36:36 | root[root] @ localhost [] | show variables like 'log_output%' | | 2016-09-30 14:37:17 | root[root] @ localhost [] | show tables | | 2016-09-30 14:37:29 | root[root] @ localhost [] | select * from general_log | | 2016-09-30 14:37:36 | root[root] @ localhost [] | select * from general_log | | 2016-09-30 14:38:03 | root[root] @ localhost [] | select * from general_log | | 2016-09-30 14:38:30 | root[root] @ localhost [] | select event_time,user_host,argument from general_log | +---------------------+---------------------------+--------------------------------------------------------+
一、启动
使用 service 启动:service mysqld start
使用 mysqld 脚本启动:/etc/init.d/mysqld start
使用 mysqld_safe 启动:mysqld_safe&
二、停止
使用 service 启动:service mysqld stop
使用 mysqld 脚本启动:/etc/init.d/mysqld stop
mysqladmin shutdown
三、重启
使用 service 启动:service mysqld restart
使用 mysqld 脚本启动:/etc/inint.d/mysqld restart
错误日志文件对MySQL的启动、运行、关闭过程进行了记录。MySQL DBA在遇到问题时应该首先查看该文件。该文件不但记录了出错信息,也记录一些警告信息或者正确的信息。
"通过 show variables like log_error来定位该文件" mysql> show variables like 'log_error'; +---------------+------------------------+ | Variable_name | Value | +---------------+------------------------+ | log_error | /var/log/mysql/err.log | +---------------+------------------------+ 1 row in set (0.00 sec) "文件内容" 160930 14:00:57 [Note] Event Scheduler: Killing the scheduler thread, thread id 1 160930 14:00:57 [Note] Event Scheduler: Waiting for the scheduler thread to reply 160930 14:00:57 [Note] Event Scheduler: Stopped 160930 14:00:57 [Note] Event Scheduler: Purging the queue. 9 events 160930 14:00:57 InnoDB: Starting shutdown... 160930 14:00:58 InnoDB: Shutdown completed; log sequence number 892930378 160930 14:00:58 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
设置一个阈值,将运行时间超过该值的所有SQL语句都记录到慢查询日志文件中。
默认情况下,MySQL数据库并不启动慢查询日志,你需要手工将这个参数设为ON
"记录慢查询的时间阈值" mysql> show variables like '%long%' -> ; +---------------------------------------------------+----------+ | Variable_name | Value | +---------------------------------------------------+----------+ | long_query_time | 0.001000 | +---------------------------------------------------+----------+ 3 rows in set (0.05 sec) 2."查询慢查询日志是否开启" mysql> show variables like 'log_slow_queries'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | log_slow_queries | OFF | +------------------+-------+ 1 row in set (0.02 sec)
3."如果运行的SQL没有索引,慢查询日志是否记录" mysql> show variables like 'log_queries_not_using_indexes'; +-------------------------------+-------+ | Variable_name | Value | +-------------------------------+-------+ | log_queries_not_using_indexes | OFF | +-------------------------------+-------+ 1 row in set (0.00 sec) 4."更改设置" mysql> set global log_slow_queries='ON' -> ; Query OK, 0 rows affected, 1 warning (0.05 sec)
"打开查询日志" mysql> set global general_log='on'; Query OK, 0 rows affected (0.00 sec) "日志所在位置" mysql> show variables like 'general%'; +------------------+-------------------------------------+ | Variable_name | Value | +------------------+-------------------------------------+ | general_log | ON | | general_log_file | /usr/local/mysql/data/localhost.log | +------------------+-------------------------------------+ "文件内容" mysql> system more /usr/local/mysql/data/localhost.log ; Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument 160930 14:25:15 3 Query show variables like 'general%' 160930 14:25:31 3 Query select * from general_log 160930 14:25:33 3 Query select * from general_log 160930 14:25:34 3 Query select * from general_log 160930 14:25:35 3 Query select * from general_log 160930 14:25:36 3 Query select * from general_log
二进制日志文件有两个重要的作用:
1.远程复制
可以通过主机上的二进制日志文件,将数据更改同步到备机。
2.数据恢复
某些数据恢复操作需要用到二进制日志文件
恢复历史备份后,可以使用二进制文件重新执行备份后所做的操作,达到完全恢复。