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.

  • MongoDB & Docker: connections from Dockerized Mongo client to external server refused
  • meet Call to undefined function Xinax\LaravelGettext\bindtextdomain() error in LaravelProject
  • Adding newuser in docker container with sudo privileges
  • Angular 2 app deploy in a docker Container running on AWS
  • How much maximum heap space does a Play Framework App use per default
  • How to start and register a chaincode of Hyperledger on Windows10
  • 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?

  • Profiling Java application in kubernetes
  • the docker's host machine cannot connect internet
  • Linking Local MongoDB Data to Dockerized MongoDB container
  • Docker says: No connection could be made because the target machine actively refused it
  • Gitlab runner: It is possible running an docker image that depends from another
  • Enter into docker container after shell script execution is complete
  • 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.