Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mount logs of mysql container in host directory

I want to mount a directory from host inside mysql container so that mysql would write its logs to that directory and I would be able to access those logs from host.

For this I am using the following volume configuration:

volumes:
  - ./logs/mysql:/var/log/mysql

But as pointed out in this answer, there are permission issues between host user and container user. The solution there was to use named volumes, but what I want is to access those logs on host in a convenient directory. Not inside internal directories of docker.

like image 556
Ayushya Avatar asked Aug 01 '17 07:08

Ayushya


1 Answers

So I ran the default image and made few observations

  1. By default the log files are not created at all in /var/log/mysql. This is because the default my.cnf has the error-log settings commented
  2. You need to create your own config file to add these settings and map them inside /etc/mysql/mysql.conf.d
  3. The /entrypoint.sh does change the permissions on /var/lib/mysql but not on /var/log/mysql

So to fix the issue you add a test.cnf file with below content

[mysqld]
log-error   = /var/log/mysql/error.log
general_log  = /var/log/mysql/log_output.log

And update your docker-compose with below settings

version: '2'

services:
  mysql:
    image: mysql:latest
    volumes:
      - ./logs:/var/log/mysql
      - ./test.cnf:/etc/mysql/mysql.conf.d/test.cnf
    environment:
      MYSQL_ROOT_PASSWORD: root
    entrypoint: ""
    command: bash -c "chown -R mysql:mysql /var/log/mysql && exec /entrypoint.sh mysqld"

This would make sure that before running the entrypoint script the proper permissions are set

like image 68
Tarun Lalwani Avatar answered Oct 29 '22 06:10

Tarun Lalwani