Ayushya
Ayushya

Reputation: 10407

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.

Upvotes: 6

Views: 8296

Answers (2)

KayCee
KayCee

Reputation: 125

You are pretty close.

The host folder ./logs/mysql needs to be owned by UID 999 - which might cause you issues if you haven't got root access or ./logs/mysql is somewhere on a mapped drive, like NTFS or CIFS

The setting to enable error_log is outlined here in the docs - the latest versions of maria will read config files from a variety of locations and the LAST setting of this variable wins - so I've set mine in /etc/mysql/mariadb.conf.d/50-server.cnf

Upvotes: 0

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

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

Upvotes: 11

Related Questions