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

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.

  • Docker service registering into Consul but health check is failing
  • Why docker showing localhost data?
  • Proxy with self signed cert causing hosts to not be found during Docker build?
  • Docker for windows container is not starting with -m 4g but works fine with -m 3g
  • Change Docker's images/containers path
  • Some RUNs won't work on docker but will when inside a container
  • 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?

  • Getting SequelizeConnectionRefusedError: when running docker with sequelize
  • docker-compose up not adding code to remote container
  • Mesos + Docker, do I automatically get the benefits of Mesos HA, etc.?
  • What is the entry point/command required to run an etcd container in ECS?
  • Error when loading a docker imager
  • How do I “dockerize” a redis service using phusion/baseimage-docker
  • One Solution collect form web for “How can I enable MySQL binary logging using the official Docker image?”

    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
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.