Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable logging in docker mysql container

Tags:

I'm trying to get familiar with the docker ecosystem and tried to setup a mysql database container. With docker-compose this looks like:

version: '2' services:   db:     image: mysql:5.6.33@sha256:31ad2efd094a1336ef1f8efaf40b88a5019778e7d9b8a8579a4f95a6be88eaba     volumes:       - "./db/data:/var/lib/mysql"       - "./db/log:/var/log/mysql"       - "./db/conf:/etc/mysql/conf.d"     restart: "yes"     environment:       MYSQL_ROOT_PASSWORD: rootpw       MYSQL_DATABASE: db       MYSQL_USER: db       MYSQL_PASSWORD: dbpw 

My conf directory contains one file:

[mysqld] log_error       =/var/log/mysql/mysql_error.log general_log_file=/var/log/mysql/mysql.log general_log     =1 slow_query_log  =1 slow_query_log_file=/var/log/mysql/mysql_slow.log long_query_time =2 log_queries_not_using_indexes = 1 

Unfortunately I don't get any log files that way. The setup itself is correct and the cnf file is used. After connecting to the container and creating the 3 files, chown them to mysql and restarting the container, the logging is working as expected.

I'm pretty sure that this is a common scenario, and my current way to get it running seems really stupid. What is the correct way to do it?

I could improve my approach by moving all this stuff in a Dockerfile, but this still seem strange to me.

like image 869
mheinzerling Avatar asked Sep 26 '16 16:09

mheinzerling


People also ask

How can I see the logs inside a docker container?

First of all, to list all running containers, use the docker ps command. Then, with the docker logs command you can list the logs for a particular container. Most of the time you'll end up tailing these logs in real time, or checking the last few logs lines.

What is docker logging?

When an application in a Docker container emits logs, they are sent to the application's stdout and stderr output streams. The container's logging driver can access these streams and send the logs to a file, a log collector running on the host, or a log management service endpoint.


1 Answers

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/log/mysql/general-log.log 

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

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

like image 141
ponsfrilus Avatar answered Sep 22 '22 02:09

ponsfrilus