Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enable MySQL binary logging using the official Docker image?

Tags:

docker

mysql

What would be the best way to enable binary logging using the official mysql image?

I have tried using the mysql:5.7 image, overriding the command when running it to also pass through the startup options to enable binary logging to mysqld (see below). The problem with this approach is that the mysql user does not have permission to write to the /var/log/mysql directory.

The run command:

docker run -d \
  --name mysql \
  -v /var/lib/mysql:/var/lib/mysql \
  mysql:5.7 \
  mysqld \
  --datadir=/var/lib/mysql \
  --user=mysql \
  --server-id=1 \
  --log-bin=/var/log/mysql/mysql-bin.log \
  --binlog_do_db=test

The output:

mysqld: File '/var/log/mysql/mysql-bin.index' not found (Errcode: 2 - No such file or directory)

Should I fork the repository and add a volume for /var/log/mysql which the mysql user can write to and create a custom image, or is there a better way to do it? Is this possible using only the official mysql image?

like image 712
Brett Avatar asked Oct 30 '14 09:10

Brett


People also ask

How do I enable binary logging in MySQL?

From MySQL 8.0, binary logging is enabled by default, whether or not you specify the --log-bin option. The exception is if you use mysqld to initialize the data directory manually by invoking it with the --initialize or --initialize-insecure option, when binary logging is disabled by default.

How do I know if MySQL is enabled by binary logging?

to know if MySQL binary log is enabled through SQL command, you can use show variables command. show variables like 'yourPatternValue'; In place of 'yourPatternValue', you can use log_bin to check the binary log is enabled using SQL command show.

How do I enable and use binary log in MySQL Mariadb?

Enable Binary Log in MySQL You must edit the MySQL main configuration file to enable binary logging. Save and close the file when you are finished. Next, restart the MySQL service to apply the changes.


1 Answers

The problem with this approach is that the mysql user does not have permission to write to the /var/log/mysql directory

The problem actually is that the directory /var/log/mysql does not exists on the mysql:5.7 Docker image. You can make sure of it running the following container:

$ docker run --rm mysql:5.7 ls /var/log/
alternatives.log
apt
bootstrap.log
btmp
dmesg
dpkg.log
faillog
fsck
lastlog
wtmp

Furthermore, MySQL binary logs aren't logs meant for following your MySQL server activity or errors ; they are logs meant to give your MySQL server a chance to recover data in case of a server crash.

As a consequence, you want those binary logs:

  • to stay close to your data
  • to be written on a fast file system

In most cases, Docker container file system is slow and that's why the MySQL data folder for the container is declared as a VOLUME.

So you also want your binary logs to be written on a Docker data volume and not the Docker container file system.


long story short, start your container with:

docker run -d \
  --name mysql \
  -v /var/lib/mysql:/var/lib/mysql \
  mysql:5.7 \
  mysqld \
  --datadir=/var/lib/mysql \
  --user=mysql \
  --server-id=1 \
  --log-bin=/var/lib/mysql/mysql-bin.log \
  --binlog_do_db=test
like image 93
Thomasleveil Avatar answered Nov 13 '22 22:11

Thomasleveil