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 INSERT
s, UPDATE
s, and DELETE
s. 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:
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]):
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:
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. SELECT
s) 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.