概述:
mysql的mysql-bin是數據庫的操作日志文件,如果不做主從復制的話,基本上是沒用的。
例如UPDATE一個表,或者DELETE一些數據,即使該語句沒有匹配的數據,這個命令也會存儲到日志文件中,還包括每個語句執行的時間,也會記錄進去的。
這樣做主要有以下兩個目的:
1:
數據恢復如果你的數據庫出問題了,而你之前有過備份,那么可以看日志文件,找出是哪個命令導致你的數據庫出問題了,想辦法挽回損失。
2:
主從服務器之間同步數據主服務器上所有的操作都在記錄日志中,從服務器可以根據該日志來進行,以確保兩個同步。舉例:當單一的mysql服務器服務使用時,可以將相應的 log-bin=/program/mysql/mysql-bin 該項注釋掉,加 “#”號然后重啟 mysql 服務。
如何刪除mysql-bin文件?
默認情況下mysql會一直保留mysql-bin文件,這樣到一定時候,磁盤可能會被撐滿,雖然文件沒用,但是不建議使用rm命令刪除,這樣有可能會不安全,正確的方法是通過mysql的命令去刪除。
mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2819416 Server version: 5.5.24-0ubuntu0.12.04.1-log (Ubuntu) Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> reset master; Query OK, 0 rows affected (3 min 37.65 sec)
其實關鍵的命令就是reset master;這個命令會清空mysql-bin文件。
另外如果你的mysql服務器不需要做主從復制的話,建議通過修改my.cnf文件,來設置不生成這些文件,只要刪除my.cnf中的下面一行就可以了。
log-bin=mysql-bin
如果你需要復制,最好控制一下這些日志文件保留的天數,可以通過下面的配置設定日志文件保留的天數:
expire_logs_days = 7
表示保留7天的日志,這樣老日志會自動被清理掉。
轉載於:https://www.php.cn/mysql-tutorials-419473.html