bin log mysqld ทำให้ harddisk เกือบเต็ม
วิธีแก้ mysql-bin.0000xx ทำพื้นที่เก็บข้อมูลของ Mysql เต็ม ถ้าใครเปิดใช้ inno_db อาจเคยเจอปัญหาพื้นที่เก็บข้อมูล ของ Mysql เต็มทั้งๆ ที่เราได้ออกแบบไว้แล้ว ว่าพื้นที่ เก็บข้อมูล ควรใช้เท่าไร
mysql-bin.0000xx เป็น binary log ของ Mysql มันจะถูกสร้างขึ้นตั้งแต่เรา start Mysql และจะสะสมไปเรื่อยๆ
วิธีลบทำได้ 2 แบบคือ
1. ลบด้วยคำสั่งระบบ RM ลบไฟล์ต่างๆ ออกให้หมด แต่วิธีนี้มีข้อเสียคือ ต้อง down mysql ก่อน ลบเสร็จค่อย start ขึ้นมาไม่ อาจไม่เหมาะกับ เครื่องที่ต้องรันตลอดเวลา
2. ลบด้วยคำสั่งของ Mysql เอง โดยเข้าไป Comand Mysql
$ mysql -u root -pกรอก password สำหรับ root ก็จะเข้าหน้า ของ mysql
รันคำสั่ง
flush logs;
เพื่อให้ mysql สร้าง log ชุดสุดท้ายขึ้นมา จะได้ไฟล์เพิ่มขึ้นมาเช่น mysql-bin.0000xy
จากนั้นรันคำสั่ง
purge binary logs to 'mysql-bin.0000xy';ถ้ามีไฟล์ใหญ่ๆ หลายๆไฟล์ ต้องรอจนกว่ามันทำงานเสร็จ เท่านี้ เราก็จะได้พื้นที่ใช้งานคืนมาแล้ว
/var/lib/mysql/
ตัวอย่างไฟล์
-rw-rw---- 1 mysql mysql 1073742109 Jan 11 06:25 mysql-bin.000098
-rw-rw---- 1 mysql mysql 1073742291 Jan 13 07:47 mysql-bin.000099
-rw-rw---- 1 mysql mysql 1073742096 Jan 15 11:45 mysql-bin.000100
-rw-rw---- 1 mysql mysql 1073741922 Jan 17 18:35 mysql-bin.000101
-rw-rw---- 1 mysql mysql 1073741885 Jan 20 10:24 mysql-bin.000102
-rw-rw---- 1 mysql mysql 1073742351 Jan 23 15:11 mysql-bin.000103
-rw-rw---- 1 mysql mysql 1073741948 Jan 26 13:40 mysql-bin.000104
-rw-rw---- 1 mysql mysql 1073742173 Jan 29 02:36 mysql-bin.000105
-rw-rw---- 1 mysql mysql 1073742232 Jan 31 15:54 mysql-bin.000106
-rw-rw---- 1 mysql mysql 1073741913 Feb 3 13:55 mysql-bin.000107
-rw-rw---- 1 mysql mysql 1073742305 Feb 6 12:28 mysql-bin.000108
จะเห็นว่าไฟล์ขนาดใหญ่ ระดับ GB ถ้าเราเก็บไว้จะเปลืองพื้นที่มาก แนะนำให้ลบออกเป็น ระยะ ด้วย วิธีแก้ mysql-bin.0000xx ทำพื้นที่เก็บข้อมูลของ Mysql เต็ม แต่ถ้าเครื่อง เราไม่ได้ทำ Replicate หรือ มี transaction เยอะมากๆ ไม่แนะนำให้เก็บ Binary log ไว้
วิธี config ไม่ให้ mysql เก็บ Binary log
ให้แก้ไขไฟล์
my.cnf แล้วเพิ่ม # ไว้หน้า log-bin=mysql-bin ดังตัวอย่าง
# log-bin=mysql-bin
เสร็จแล้ว Restart Mysql 1 ครั้ง ด้วยคำสั่ง
/etc/init.d/mysqld restar
เท่านี้ก็จะไม่เปลืองพื้นที่ในเครื่องแล้วครับ
ที่มีขนาดโตมาก ๆ มักจะเจอปัญหาว่า
mysql-bin มันมีขนาดใหญ่มาก
เราสามารถลบทิ้งได้โดยสั่ง
With your mysql client :
mysql -u root -p
then
mysql> flush logs;
will create a brand new mysql-bin.0000xy
mysql> purge binary logs to 'mysql-bin.0000xy';
will purge all your logs files before the last.
เพราะเราไป delete ธรรมดาไม่ได้ ต้องจากคอมมานไลน์ครับ
ความคิดเห็นทั้งหมด
You need to let mysqld do that for you. Here is how mysqld manages it:
The file mysql-bin.index keeps a list of all binary logs mysqld has generated and auto-rotated. The mechanisms for cleaning out the binlogs in conjunction with mysql-bin.index are:
PURGE BINARY LOGS TO 'binlogname';
PURGE BINARY LOGS BEFORE 'datetimestamp';
These will clear all binary logs before the binlog or timestamp you just specified.
For example, if you run
PURGE BINARY LOGS TO 'mysql-bin.000223';
this will erase all binary logs before 'mysql-bin.000223'.
If you run
PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND;
this will erase all binary logs before midnight 3 days ago.
If you want to have binlog rotated away automatically and keep 3 days woth, simply set this:
mysql> SET GLOBAL expire_logs_days = 3;
then add this to /etc/my.cnf
[mysqld]
expire-logs-days=3
and mysqld will delete them logs for you
SHOW SLAVE STATUS\G
This is critical. When you run SHOW SLAVE STATUS\G, you will see two binary logs from the Master:
Master_Log_File
Relay_Master_Log_File
When replication has little or no lag these are usually the same value. When there is a lot of replication lag, these values are different. Just to make it simple, choose whatever Relay_Master_Log_File is, and go back to the Master and run
PURGE BINARY LOGS TO 'Whatever Relay_Master_Log_File Is';
That way, replication is not interrupted.
Give it a Try !!!
user=da_admin
passwd=xxxxxxxxxxx