Mysql deletes binlog logs and restores data.

Source: Internet
Author: User

The binlog function is enabled for mysql in the home decoration. The advantage of enabling this function is that it facilitates data recovery in the future, but the disadvantage is that log files are growing rapidly and will soon occupy the disk space.

Therefore, we need to regularly or manually delete large log files.

The methods for deleting log files are as follows:

1. Set the Log retention period expire_logs_days to automatically delete

 
 
  1. # View the current Log Retention days:
  2.  
  3. Show variables like '% expire_logs_days % ';
  4.  
  5. # The default value is 0, indicating that logs does not expire. You can set a global parameter to make it take effect temporarily:
  6.  
  7. Set global expire_logs_days = 7;
  8.  
  9. # If BINLOG is set to be retained for only 7 days, the parameter "mysql restart" will fail by default. Therefore, you need to set it in my. cnf.
  10.  
  11. Expire_logs_days = 7

Ii. manually delete BINLOG (purge binary logs)

 
 
  1. # Delete all binary logs listed in the log index before the specified log or date. These logs are also recorded in the log index file
  2.  
  3. PURGE {MASTER | BINARY} logs to 'Log _ name'
  4. PURGE {MASTER | BINARY} logs before 'date'
  5.  
  6. # Example:
  7.  
  8. Purge master logs to 'mysql-bin.010 ′;
  9. Purge master logs before '2017-06-22 13:00:00 ′;
  10. Purge master logs before DATE_SUB (NOW (), INTERVAL 3 DAY );
  11.  

Others:

 
 
  1. View the current log file list: show binary logs;

The purpose of using binlog logs is to facilitate data recovery, so we should not only delete the logs, but also use logs to restore data.

Use binlog to restore data:

Use the mysqlbinlog command to restore logs

 
 
  1. Mysql BINLOG-d test/root/mysql/mysql-bin.000001 | mysql-uroot-ppassword
  2.  
  3. ※Appendix
  4.  
  5. #1. For how to use mysqlbinlog on mysql 5.0, refer to: official materials
  6.  
  7. #2. If you do not know the location of the files stored in log-bin, run the following command:
  8.  
  9. Mysql-uroot-ppassword-e 'show binlog events \ G'
  10.  
  11. #3. When you only need to restore some records, you can use the time point or location point to locate them. For example:
  12.  
  13. Mysqlbinlog -- start-date = "9:01:00" -- stop-date = "10:00:01"/var/log/mysql/bin.123456 | mysql-uroot-ppassword [code]
  14.  
  15. # It means to restore data from AM to AM on January 1, April 20.
  16.  
  17. Mysqlbinlog -- start-position = "368301" -- stop-position = "368312"/var/log/mysql/bin.123456 | mysql-uroot-pmypwd
  18.  
  19. # It means to redo the operation between 368301 and 368312. The location point can be the at xxx node displayed in mysqlbinlog)
  20.  
  21. Mysqlbinlog -- start-date = "9:55:00" -- stop-date = "10:05:00"/var/log/mysql/bin.123456>/tmp/mysql_restore. SQL
  22.  
  23. # Similarly, if we just want to view the operation for a certain period of time, just export it to a file.

 

This article from the "small Cui's growth path" blog, please be sure to keep this source http://cyr520.blog.51cto.com/714067/957041

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.