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.

  • Execute configuration bash script during docker build
  • Is it normal to run docker instance running tomcat and deploy to this tomcat applications?
  • Django connection to postgres by docker-compose
  • Docker remote api don't restart after my computer restart
  • Docker & Rails | bundler: command not found: rails
  • Speed up Gitlab CI reusing docker machine for stages
  • 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?

  • Dockerize Applications or Machines?
  • Docker flask application environment variables
  • Cannot connect to SQL Server from a Docker container
  • Laravel POST TokenMismatchException for files bigger than 8kb/16KB (and 2MB) when jwilder/nginx-proxy
  • Not all services are registered in Consul with Registrator
  • Docker for Windows does not work with Volumes
  • 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.