Binary Log
MySQL

MySQL Binary Logs – Walkthrough

intro

Binary logs are one of the primary components in a database infrastructure – here’s everything you need to know.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
MySQL logo MySQL
THE MYSQL DATABASE

Preface

All databases keep logs. Logs are just what you think they may be – they let us figure out what’s happening inside of our database at any given moment. Databases keep various types of logs – there are error logs, general query logs, slow query logs, relay logs, and binary logs.

In this blog, we’re looking through one of them – binary logs. Strapped in? Let’s go!

What are Binary MySQL Logs?

Binary logs log information about changes to data made by a database server. Binary logs can also be called “activity” logs because that’s essentially what they are – binary log describes any events that changed the state of your database – think creating, modifying, or deleting tables.

Binary logs consist of events – these events alert you about what your SQL query has accomplished (e.g. your DELETE query deleted 575 rows.)

Aside from that, binary logs are especially important for replication purposes – MySQL logs are a crucial piece of the replication puzzle because replication works by reading through them.

As far as MySQL is related, keep in mind that data recovery operations may make use of binary logs in MySQL Server, too – since the binary log records various events, replaying them is very beneficial for restoring data as well.

One more thing to keep in mind is that in the relational database management system world, the binary log isn’t used for statements that do not change our data and only return results instead – in other words, we cannot use MySQL logs on SELECT statements (read queries), but we can use them on INSERTs, UPDATEs, and DELETEs. SHOW statements are out of the loop too.

Binary MySQL Logs Demystified

Various database management systems deal with binary logs in different ways – MySQL Server, for example, uses them heavily for replication purposes, but in return, slows your database performance (not to a noticeable degree.) On the other side, the binary log can withstand errors and is ACID-compliant as well (in the sense that only transactions that are absolutely completed are logged.)

MySQL also ensures security in the binary log dilemma – since binary logs log events that deal with data, it’s likely to raise some questions and concerns about the security aspect of them too and that’s where we have great news – MySQL states that passwords in SQL queries are automatically secured (rewritten) by the server so that they’re not recorded in plain text.

MySQL logs can also be encrypted or decrypted for security purposes as well – though the feature was only introduced relatively recently (MySQL 8.0.14), there is a system variable you can use for this: it’s called binlog_encryption and setting it to ON will enable encryption. This variable enables encryption for both binary logs and relay logs. An encryption key is generated:

  1. Before binary logs are working if you start the server with encryption enabled.
  2. After generating a new binary log encryption key before the binary logs start to work.

Encryption decides an entire book in and of itself, so for those who are interested in these kinds of MySQL logs, we’d refer you to the MySQL documentation.

Binary Logging Options in MySQL

Once you have a good grasp of what binary logs are and what they do, keep in mind that different database management systems deal with binary logs in different ways, too – MySQL offers the following options for our MySQL logs (format value [DEFAULT VALUES]):

  1. log_bin=[ON|OFF] – enables or disables binary logging. Default value – OFF.
  2. --skip-log-bin|--disable-log-bin – if either of those options are specified with --log-bin, the binary log is either skipped (deactivated) or disabled altogether. Both options can be specified too, but in that case, only the option that is specified the last will work.
  3. log-bin=[name] – enables us to give a name for our MySQL logs. Default value for our MySQL logs – “binlog”
  4. Options related to replication that include:
    1. --log-slave-updates=[ON|OFF] – whether to log updates to our slave servers.
  5. --slave-preserve-commit-order=[ON|OFF] – the ability to preserve (save) the commit order.

Both of these options are disabled once either --skip-log-bin or --disable-log-bin is specified.

Also, keep in mind that MySQL Server names MySQL logs appropriately as well – for the binary logs, we have numeric extensions that look something like “binlog.0000000001.” The reason why there’s so many numbers is that MySQL Server creates a new binary MySQL log file once:

  1. The server starts.
  2. The logs are flushed.
  3. The size of the log files reach the maximum size.

So, if you see a lot of binary logs in your MySQL Server database instance, there’s no need to worry!

If you want your binary logs not to exceed a specified size, specify the maximum size you don’t want your logs to exceed in the max_binlog_size variable and you will be good to go.

MySQL also tracks the usage of binary logs by creating an index file the name of which can be changed by changing the log-bin-index variable value.

The rest of the options related to binary logs heavily depend on your replication setup – since one of the primary purposes of binary logs is to assist with replication procedures, other MySQL logs aren’t devoted for this purpose; only binary logs are. Refer to the documentation to learn everything you need to know about binary logging and replication procedures, then come back to this blog.

Binary Logs and SQL Clients

Now that you have a good grasp of what binary MySQL logs can help your MySQL Server with, it’s time to dive into SQL clients as well. You may be thinking to yourself “why do I even need SQL clients? I’m too busy anyway”, but here’s the catch – SQL clients exist to alleviate your busyness and burden in the database world.

No matter what database management system you use – MongoDB, PostgreSQL, MySQL, or even ClickHouse – proper SQL clients like the one built by the team over at DbVisualizer come with a free 21-day trial and can be used to solve a wide variety of pressing issues ranging from data visualization to security,  simplifying monitorin and workspace operations, and so on. DbVisualizer was built to meet rigorous performance, availability, security, and capacity standards – keep an eye out on its features both when testing and when using it in the real world, and until next time.

Frequently Asked Questions

What are Binary MySQL Logs?

Binary MySQL logs are logs that lo all of the data-altering changes in our MySQL Server database instance. SQL queries that do not change data itself (e.g. SELECTs) aren’t logged.

How do I Configure Binary MySQL Logs?

Binary logs can be turned on or off by fiddling with the log-bin option in my.cnf, my.ini, or setting via the CLI.

Are My Binary MySQL Logs Secure?

Yes (MySQL Server takes additional measures to not include passwords in binary logs and avoids doing other things), and if you’re paranoid, you can always encrypt them too.

When Were Binary MySQL Logs Introduced?

Binary MySQL logs were introduced in MySQL 8.0.14.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

A Guide To the SQL DECLARE Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SERVER 7 min 2024-12-12
title

SQL Upsert: Inserting a Record If It Does Not Exist

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-10
title

A Complete Guide to the MySQL COUNT Function

author Antonello Zanini tags MySQL 9 min 2024-12-09
title

MySQL Nullable Columns: Everything You Need to Know

author Antonello Zanini tags MySQL 6 min 2024-12-03
title

A Complete Guide to the MySQL GROUP_CONCAT Function

author Antonello Zanini tags Data aggregation MySQL 8 min 2024-12-02
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

MySQL 9.0 and the New EXPLAIN Features

author Lukas Vileikis tags MySQL SQL 9 min 2024-11-27
title

MySQL CAST: A Guide to the Data Conversion Function

author Antonello Zanini tags MySQL 3 min 2024-11-25
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20
title

SQL Variable: What It Is and How To Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-11-18

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗