在 docker mysql 容器中启用日志记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39708213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable logging in docker mysql container
提问by mheinzerling
I'm trying to get familiar with the docker ecosystem and tried to setup a mysql database container. With docker-compose
this looks like:
我正在尝试熟悉 docker 生态系统并尝试设置一个 mysql 数据库容器。有了docker-compose
这个样子:
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:
我的 conf 目录包含一个文件:
[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.
不幸的是,我没有以这种方式获得任何日志文件。设置本身是正确的,并且使用了 cnf 文件。连接到容器并创建 3 个文件后,将chown
它们连接到mysql
并重新启动容器,日志记录按预期工作。
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.
我可以通过在 Dockerfile 中移动所有这些东西来改进我的方法,但这对我来说仍然很奇怪。
采纳答案by BMitch
After connecting to the container and creating the 3 files, chown them to mysql and restarting the container, the logging is working as expected.
连接到容器并创建 3 个文件后,将它们 chown 到 mysql 并重新启动容器,日志记录按预期工作。
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.
这指向主机卷权限问题。当您从容器映射到主机时,不会对用户 ID 进行映射,并且容器内的 uid 附加的名称可能与外部有很大不同。您需要使用容器用户可以写入的内容来初始化目录权限。一种简单的方法是创建一个组,该组有权写入主机和容器上的文件,然后将各种用户添加到您的映像和主机操作系统上的该组中。另一种选择是使用不能直接从主机访问的命名文件系统,并使用图像的目录权限对其进行初始化。
Edit: An example of a named volume with your docker-compose.yml is as simple as:
编辑:使用 docker-compose.yml 命名卷的示例非常简单:
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.
请注意,我还从您的映像名称中删除了 sha256,此引用会阻止您提取映像的修补版本。我也更喜欢“除非停止”重启策略,以便 Docker 在重启时做预期的事情。
回答by ponsfrilus
I was looking for the exact same thing, and now, there is a better way to do it.
我一直在寻找完全相同的东西,现在,有更好的方法来做到这一点。
The docker mysqlwrites:
该搬运工的MySQL写道:
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:
许多配置选项可以作为标志传递给 mysqld。这将使您可以灵活地自定义容器,而无需 cnf 文件。例如,如果要将所有表的默认编码和排序规则更改为使用 UTF-8 (utf8mb4),只需运行以下命令:
$ 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-composeworld, one could pass these arguments through the "command"section of the service:
在docker-compose世界中,可以通过服务的“命令”部分传递这些参数:
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.
有了足够的数量(例如- ./logs/mysql.log:/var/log/mysql/general-log.log
),就可以很容易地接触到它们。
This is pretty straight forward and avoid dealing with a local configuration. It will works with any MySQL Docker imagesand will keep the my.cnf
as shipped by the image.
这是非常直接的并且避免处理本地配置。它将适用于任何MySQL Docker 镜像,并保持镜像提供的my.cnf
原样。