Enable logging in docker mysql container

I was looking for the exact same thing, and now, there is a better way to do it.

The docker mysql writes:

Many configuration options can be passed as flags to mysqld. This will
give you the flexibility to customize the container without needing a
cnf file. For example, if you want to change the default encoding and
collation for all tables to use UTF-8 (utf8mb4) just run the
following:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

In a docker-compose world, one could pass these arguments through the “command” section of the service:

command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

In my use case, I just wanted to turn on the logs and specify the path to the log file:

 command: mysqld --general-log=1 --general-log-file=/var/lib/mysql/general-log.log

With the adequate volumes (e.g. - ./logs/mysql.log:/var/lib/mysql/general-log.log), it becomes easy to reach them.

This is pretty straight forward and avoid dealing with a local configuration. It will work with any MySQL Docker image and will keep the my.cnf as shipped by the image.

Edit: change path from /var/log/mysql/ to /var/lib/mysql/ to ensure a MySQL writable folder.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)