MySQL log files   (1)

MySQL has several log files that can help you find out what activity is taking place.

log type     

information write to log file

error log  Problems encoutered  starting, running or stopping mysqld
General query  log      Established client connections and statements receive from client
Binary log Statements that change data(also are used to replicate)

Slow query 

DDL log(metadata log)

Queries that take more seconds to execute

Metadata operations performanced by DDL statements

By default MySQL sever's log files store in the data directory. You can force the server to close or reopen the log files.

When using MySQL with logging enabled, you may want to back up and remove old log file from time to time, and tell MySQL to start new logs to new log files.

On linux installation, you can use the mysql-log-rotate script for this. If you installed MySQL from RPM distribution, this script should have been installed automatically. Be carefull with this script if you are using binary log for replication. You should not remove binary log until you are certain that their contents have been processed by all slaves.

[root@HWZX-DB-MONITER support-files]# pwd
/opt/mysql/support-files
[root@HWZX-DB-MONITER support-files]# ls -ld mysql-log-rotate 
-rwxr-xr-x 1 root root 801 Feb 9 2015 mysql-log-rotate
[root@HWZX-DB-MONITER support-files]# more mysql-log-rotate 
# This logname can be set in /etc/my.cnf
# by setting the variable "err-log"
# in the [safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/data/mysql/mysqld.log
#
# If the root user has a password you have to create a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password = <secret> 
# user= root
#
# where "<secret>" is the password. 
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !

/data/mysql/mysqld.log {
# create 600 mysql mysql
notifempty
daily
rotate 3
missingok
compress
postrotate
# just if mysqld is really running
if test -x /opt/mysql/bin/mysqladmin && \
/opt/mysql/bin/mysqladmin ping &>/dev/null
then
/opt/mysql/bin/mysqladmin flush-logs
fi
endscript
}

On orther system, you should install a short script yourself from cron for handling log files. 

For binary log, you can set expire_logs_days system variable to expire binayr log automatically after given the number days.

mysql> show variables like '%expire%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| expire_logs_days | 0 |
+------------------+-------+
1 row in set (0.00 sec)

mysql> set global expire_logs_days=5;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%expire%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| expire_logs_days | 5 |
+------------------+-------+
1 row in set (0.00 sec)

If you just set expire_logs_days system variable in mysqld instance, it would be return the old value when you restart the MySQL server. In order to set expire_logs_days system varible permanetly, you can write the parameter in mysql server parameter file named my.cnf which locates at /etc/ or mysql_home/etc


vim /etc/my.cnf

[mysqld]

expire_logs_days=10



You can force MySQL to start using new log files by flushing the logs. Log flushing occurs when you issue a flush logs command or execute a mysqladmin flush-logs, mysqladmin refresh, mysqldump --fulsh-logs or mysqldump --master-data.


eg.

mysql> flush logs;Query OK, 0 rows affected (0.10 sec)
[root@HWZX-DB-MONITER ~]# mysqladmin flush-logs -uroot -pEnter 
password: 
[root@HWZX-DB-MONITER ~]# mysqladmin refresh -uroot -pEnter 
password:


你可能感兴趣的:(mysql,log,File)