启用登录docker mysql容器 [英] Enable logging in docker mysql container

查看:207
本文介绍了启用登录docker mysql容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图熟悉码头生态系统,并试图设置一个mysql数据库容器。使用 docker-compose ,如下所示:

  version:'2' 
服务:
db:
图像:mysql:5.6.33@sha256:31ad2efd094a1336ef1f8efaf40b88a5019778e7d9b8a8579a4f95a6be88eaba
卷:
- ./db/data:/var/lib/ mysql
- ./db/log:/var/log/mysql
- ./db/conf:/etc/mysql/conf.d
restart:yes
环境:
MYSQL_ROOT_PASSWORD:rootpw
MYSQL_DATABASE:db
MYSQL_USER:db
MYSQL_PASSWORD:dbpw

我的目录包含一个文件:

  [mysqld] 
log_error = / var / log / mysql / mysql_error.log
generic_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



不幸的是我没有这样的任何日志文件。设置本身是正确的,并使用cnf文件。在连接到容器并创建3个文件后,将$ code> chown 它们转到 mysql 并重新启动容器,日志记录工作正如预期的那样。



我很确定这是一个常见的情况,我目前的方式让它运行似乎真的很愚蠢。 正确的方法是什么?



我可以通过在Dockerfile中移动所有这些东西来改进我的方法,但这似乎仍然奇怪给我。

解决方案


连接到容器并创建3个文件后,将它们选择到mysql并重新启动容器,日志记录工作正常。


这指向主机卷权限问题。当您从容器映射到主机时,不会在用户ID上进行映射,并且附加到容器内的uid的名称可能与外部有很大的不同。您需要使用容器用户可以写入的东西来初始化目录权限。一个简单的方法是创建一个可以访问主机和容器上的文件的组的组,然后在您的映像和主机操作系统上将各种用户添加到此组。另一个选择是使用您不直接从主机访问的命名文件系统,并使用映像的目录权限进行初始化。






编辑:使用docker-compose.yml命名的卷的示例与以下简单:

  version: 2'
卷:
mysql-data:
驱动程序:local
mysql-log:
驱动程序:local
mysql-conf:
驱动程序:local

服务:
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
环境:
MYSQL_ROOT_PASSWORD:rootpw
MYSQL_DATABASE:db
MYSQL_USER:db
MYSQL_PASSWORD:dbpw

请注意,我也从您的图像名称中删除了sha256,此引用将阻止您无法拉修补的版本的图像。我还喜欢除非停止的重新启动策略,以便Docker可以预期重新启动。


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.

解决方案

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.

这篇关于启用登录docker mysql容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆