Enable logging in docker mysql container

前端 未结 2 876
旧时难觅i
旧时难觅i 2020-12-17 14:33

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

versio         


        
相关标签:
2条回答
  • 2020-12-17 15:04

    After connecting to the container and creating the 3 files, chown them to mysql and restarting the container, the logging is working as expected.

    That points to a host volume permission issue. When you map from a container to the host, no mappings are made on user id's, and the name attached to the uid inside the container may be very different from outside. You need to initialize the directory permissions with something the container user can write to. One simple method is to create a group that has access to write to the files on both the host and container, and then add the various users to this group on both your image and host OS. Another option is to use a named filesystem that you don't access directly from your host and initialize it with the image's directory permissions.


    Edit: An example of a named volume with your docker-compose.yml is as simple as:

    version: '2'
    volumes:
      mysql-data:
        driver: local
      mysql-log:
        driver: local
      mysql-conf:
        driver: local
    
    services:
      db:
        image: mysql:5.6.33
        volumes:
          - "mysql-data:/var/lib/mysql"
          - "mysql-log:/var/log/mysql"
          - "mysql-conf:/etc/mysql/conf.d"
        restart: unless-stopped
        environment:
          MYSQL_ROOT_PASSWORD: rootpw
          MYSQL_DATABASE: db
          MYSQL_USER: db
          MYSQL_PASSWORD: dbpw
    

    Note that I also removed the sha256 from your image name, this reference would block you from being able to pull patched versions of the image. I also prefer the "unless-stopped" restart policy so that Docker does expected things on a reboot.

    0 讨论(0)
  • 2020-12-17 15:19

    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.

    0 讨论(0)
提交回复
热议问题