6

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.

Ayushya
  • 9,599
  • 6
  • 41
  • 57

2 Answers2

10

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

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • 1
    I think this might be correct, but I cannot get it to work. If I am using a blank entrypoint, the docker-compose.yml file is invalid, and if I am removing `entrypoint` altogether, the container exits with error code 0. In second case, I think the default entrypoint is overridden due to use of `command`. I am trying to get it working, and when it does, I think this should be accepted answer :) – Ayushya Aug 01 '17 at 14:40
  • Not working, there were no errors and container still exits with error code 0. – Ayushya Aug 01 '17 at 16:04
  • Let me check. I ran using docker but solution was docker-compose based – Tarun Lalwani Aug 01 '17 at 16:04
  • If it helps, I am using only one services of [docoker-compose.yml](https://github.com/reactome/container/blob/develop/docker-compose.yml#L94) mysql-database and testing the code you provided on that service. – Ayushya Aug 01 '17 at 16:11
  • My mistake i missed adding mysqld after entrypoint.sh which is the default CMD. Check the updated answer now – Tarun Lalwani Aug 01 '17 at 16:13
  • Oh great! It works now. The database is up and running, but still, no logs! When I am mounting the host directory, then no logs are generated and if host directory is not mounted, then I could see logs. – Ayushya Aug 01 '17 at 16:46
  • Please add a note to your answer saying that user should verify if permissions have changed or not. The solution is correct, but it's just that [Ownership remains unchanged after `sudo chown` in ubuntu](https://superuser.com/q/1237368/396312) – Ayushya Aug 04 '17 at 04:44
  • @Ayushya, that is not a ubuntu specific issue. I tested this on ubuntu VM only and then posted the solution. But your may be related to Windows/VM combo that you are using – Tarun Lalwani Aug 04 '17 at 11:09
  • It was related to NTFS filesystem. I am running ubuntu, and my project lies on different directory, which is an NTFS. Permissions on ntfs are set differently as discussed [here](https://askubuntu.com/a/113746/353904). My Uid and Gid always remained 1000 – Ayushya Aug 04 '17 at 12:44
  • @TarunLalwani Thanks a lot for your help! May I ask a question? Why we need the line entrypoint: ""? Cheers – Albert Wang Apr 14 '20 at 04:29
  • @AlbertWang, because this image uses a `entrypoint` and we want to disable that and run our own command. Docker images run the startup command as ` `. So you can either change the entrypoint completely to or disable it and change the command to run what we want. – Tarun Lalwani Apr 14 '20 at 07:00
  • @TarunLalwani db_1 | chown: cannot access '/var/log/mysql': No such file or directory – Michael Paccione Feb 18 '23 at 18:08
0

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

KayCee
  • 115
  • 7