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?
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.
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.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With