Christophe CasalegnoAutoimmune warrior, Entrepreneur & Bio/Hacker

brain@127.0.0.1:~$ cat post.html

MySQL / MariaDB: how to purge binary logs when there is no disk space left

English version updated from the French article revised on 1 January 2024, including its additions on MariaDB and the space reserved on ext4. The original publication date is retained.

When space starts running low on a MySQL server or MariaDB you may need to purge binary logs. To do that, simply connect to the server and use the PURGE BINARY LOGS command:

mysql> PURGE BINARY LOGS BEFORE 'yyyy-mm-dd hh:mm:ss'; 

For example:

mysql> PURGE BINARY LOGS BEFORE '2023-03-02 22:46:26'; 

This command will purge all binary logs from before the specified date. Another method is to use the log file’s name. For example:

mysql> PURGE BINARY LOGS TO 'ns30XXXX-bin.009836';

 
Still, you may find yourself in a more serious situation: there is no longer enough space on your storage to carry out this operation. In most cases, on ext4, the tune2fs command can be a lifesaver. By default, 5% of the space is reserved for root, and you can use tune2fs to change that value on the fly:

02:39:52-root@buster:~$ df -h |grep sda1
/dev/sda1        15G  4.9G  9.1G  36% /
02:39:54-root@buster:~$ tune2fs -m 1 /dev/sda1
tune2fs 1.44.5 (15-Dec-2018)
Setting reserved blocks percentage to 1% (39439 blocks)
02:40:02-root@buster:~$ df -h |grep sda1
/dev/sda1        15G  4.9G  9.7G  34% /
02:40:05-root@buster:~$

 
However, if you cannot free up space any other way, or for some other reason cannot log in to the MySQL / MariaDB server, you will have to manually delete the binary log files. For this example, we will assume your log files are in /var/lib/mysql: you can then use the following method to solve the problem.

Step 1: stop MySQL or MariaDB (the syntax may depend on your Linux distribution version). For example:

service mysql stop 

 
Step 2: Delete binary log files and remove their names from the MySQL or MariaDB binary log index.
 
Lost? This “brute-force” command line will count and delete 50% of the binary log files to free up space, and remove them from the MySQL or MariaDB binary log index.

cd /var/lib/mysql && a=$(ls | grep -v relay | grep bin.index) && b=$(wc -l < $a); c=$((b/2)); for file in $(head -n $c $a | cut -d "/" -f2); do rm "$file" ; done ; sed -i '1,'"$c"'d' $a  

 

Step 3: Start MySQL or MariaDB

service mysql start 

 
Step 4: check that there are no problems

MariaDB [(none)]> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000013 |       365 |
| mysql-bin.000014 |       365 |
| mysql-bin.000015 |       365 |
| mysql-bin.000016 |       342 |
+------------------+-----------+
4 rows in set (0.000 sec)
MariaDB [(none)]> 

 
If you see a file with a value of 0, something is wrong: you will need to stop MySQL again and remove the file from the index manually. If everything is OK, you can now use “PURGE BINARY LOGS BEFORE” in the usual way to delete more logs if needed.
 
Enjoy!
 
Would you rather not do this yourself, and/or never have to deal with it again for the rest of your life? Contact ScalarX ;)

Christophe Casalegno
You can follow me on: Telegram | YouTube | Twitter | Facebook | LinkedIn | Twitch

4 comments

  1. Hi, I encountered the exactly same situation like what your page title said.
    I google for solution urgently and saw the light from here.
    Thank you very much, you saved my day.

    Due to my lack of understanding and security concerns, I didn’t run your command at second step.
    Your article gave me the idea how to do it manually:
    I sudo remove few oldest binary logs, then sudo vim binlog.index to remove those deleted logs from the list.
    Thank you.

    1. Dear Tyler,

      Thank you for taking the time to write such a positive comment. I’m delighted to hear that the article was helpful to you and provided a solution to your issue.

      There’s always more than one way to solve a problem: kudos to you for figuring it out!

      I understand the security concerns related to running commands on a production server, and it’s always wise to be cautious.

      Your method of manually editing the files and index is a good workaround when you’re not comfortable running scripts or complex command. This post was old, and I’ll probably update it.

      For example, you can gain instant space on your server with : tune2fs -m 0 (or 1, 2, 3, 4) /dev/yourdisk (generally it is 5% by default) if you are on a extxx filesystem.

      If you have any further questions or run into more challenges, please don’t hesitate to reach out. Have a nice day!

Leave a comment

Add a comment
Christophe Casalegno Brain 0verride

Keep it simple
and direct!