in

How to Disable Binary Logging in MariaDB and MySQL? An In-Depth Guide

default image

Binary logging is a pivotal feature in MariaDB and MySQL that records all changes made to the database. This allows database administrators to restore the database to a specific point in time, assisting in recovery from data corruption or accidental deletion. However, binary logging is not without its downsides. The binary log files can consume substantial disk space, impact performance, and may not be needed in all environments. Here is an in-depth guide on disabling binary logging in MariaDB and MySQL.

Why Consider Disabling Binary Logging?

Before diving into how to disable binary logging, let‘s explore why you may want to disable it in the first place:

Save Disk Space

The binary log files can grow extremely large, upwards of many gigabytes per day depending on database workload. For busy databases, the logs can quickly eat up disk space. Disabling binary logging frees up this space for other uses.

Based on my experience managing MySQL databases for e-commerce sites, the binary logs would often grow to 50-100GB within a few weeks. For smaller servers, this log growth can overwhelm storage.

Reduce Overhead and Improve Performance

Writing changes to the binary log introduces overhead that can negatively impact database performance. Workloads with heavy writes may see measurable improvements by disabling binary logging.

In benchmark tests on MySQL 5.7, disabling the binary log improved transactions per second by 15-20% for some write-heavy workloads. The exact performance improvement will vary based on the application. But for write-intensive databases, it‘s an option worth testing.

Not Needed for Non-Critical Databases

Many non-production databases like development, staging, and QA environments do not need point-in-time recovery from binary logs. The overhead and disk usage may not be justified for these secondary databases.

Temporarily Disable for Maintenance

You may want to temporarily disable binary logging for certain database maintenance tasks like large batch imports. The binary log is not useful in this scenario and only wastes disk space.

The key is disabling binary logging does come with a big trade-off. You lose the ability to restore the database to a specific point in time. Only disable if you understand the risks.

How to Disable Binary Logging in MariaDB

Now let‘s look at how to actually disable binary logging in MariaDB:

  1. Log into the MariaDB server as the root user or privileged database administrator. You can connect locally or remotely.

  2. Open the MariaDB configuration file, typically located at /etc/mysql/mariadb.cnf

  3. Add the following line in the [mysqld] section:

     skip-log-bin
  4. Save the mariadb.cnf file.

  5. Restart the MariaDB service to load the new configuration:

     sudo systemctl restart mariadb  

MariaDB will no longer write any events to the binary log after restarting.

To re-enable binary logging, simply remove the skip-log-bin line and restart MariaDB.

Disabling Binary Logging in MySQL

The process works the same for disabling binary logging in MySQL:

  1. Log into the MySQL server as root or admin.

  2. Edit MySQL‘s configuration file, typically /etc/mysql/my.cnf

  3. Add this line under the [mysqld] section:

     skip-log-bin  
  4. Save my.cnf

  5. Restart the MySQL service:

     sudo systemctl restart mysqld

MySQL will stop writing to the binary log after restarting.

To re-enable binary logging, remove the skip-log-bin line and restart MySQL.

Finding and Deleting Old Binary Logs

After disabling binary logging, it‘s good practice to delete old binary log files to free up disk space.

To find binary logs in MariaDB:

SHOW BINARY LOGS;

For MySQL:

SHOW BINARY LOGS;

This lists all existing binary log files. You can then delete unneeded logs.

The exact location of the binary log files depends on the Linux distribution. But they are typically located in:

  • /var/lib/mysql
  • /var/log/mysql

You can manually delete the log files, or use a command like:

shell> rm /var/lib/mysql/mysql-bin.*

Of course, be very careful not to delete any binary log files that you may still need for point-in-time restores! Only remove binary logs that are no longer necessary.

When is Disabling Binary Logging a Bad Idea?

While disabling binary logging can provide some benefits, there are also scenarios where it should be avoided:

  • Production databases – The risks likely outweigh the benefits for critical production data that needs disaster recovery capabilities.

  • Replicated databases – Binary logging is required for replication between a master and slaves. Disabling it breaks replication.

  • Regular point-in-time restores – If you regularly restore your database to an earlier point in time, the binary log is essential.

  • High transaction rate databases – The disk space savings may not justify losing data recovery capabilities on large databases. The overhead reduction also diminishes with SSD storage.

  • No other backups – Disabling the binary log removes a recovery option. Make sure you have alternate database backups.

For these situations, it‘s best to leave binary logging enabled. Consider other methods like upgrading to SSDs, partitioning tables, or optimizing queries to improve performance instead.

Conclusion

Binary logging is a double-edged sword. It enables point-in-time restores but consumes disk and hurts performance. Disabling binary logging in MariaDB and MySQL disables this capability in return for freeing up storage space and reducing overhead.

Carefully consider both the benefits and risks before disabling binary logging. Make sure you have adequate database backups via mysqldump or other methods. Like any performance optimization, benchmark before and after to verify the benefits.

With proper precautions, disabling binary logging can be an effective strategy for reclaiming disk space and improving throughput. But it removes a key recovery mechanism that protects against data loss. Only disable binary logging when you fully understand and accept the risks.

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.