写博客是为了和过去以及未来的自己对话
“对任何渴望进步的人来说,写博客/文章/回答对自己的成长帮助都是巨大的。坚持写作的那一刻起,就已经开始受益。”

最近想在docker中跑一个MySQL5.7版本的服务,而且要基于CentOS,所以着手自己构建镜像。

容器的构建参照下面这篇文章
基于CentOS7镜像容器的MySQL环境构筑 - sxb_sunday - 博客园 (cnblogs.com)

构建完成后,用下面命令启动MySQL服务的时候,启动进程一直卡住没有反应,只能CTRL+C强制停止。

systemctl start mysqld

查看服务状态如下

[root@1a6bce4f17af /]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: activating (start) since Tue 2024-04-30 14:31:10 UTC; 5min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 250 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 235 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   CGroup: /docker/1a6bce4f17af7952758dbf3666ac5505500cddd656b37fd134ab13c0a86b7bc1/system.slice/mysqld.service
           └─252 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Apr 30 14:31:10 1a6bce4f17af systemd[1]: Starting MySQL Server...
Apr 30 14:31:11 1a6bce4f17af systemd[1]: New main PID 252 does not belong to service, and PID file is not owned by root. Refusing.
Apr 30 14:31:11 1a6bce4f17af systemd[1]: New main PID 252 does not belong to service, and PID file is not owned by root. Refusing.

New main PID 252 does not belong to service, and PID file is not owned by root. Refusing.

找了很久都是下面这种解决方案

把容器的cgroup目录挂载到宿主机的cgroup目录,如下在启动容器的时候,增加文件卷挂载命令

docker run -itd --name centos-mysql --privileged=true centos:centos7.9.2009 -v /sys/fs/cgroup:/sys/fs/cgroup /usr/sbin/init

cgroup(Control Groups)是 Linux 内核提供的一个功能,用于限制、控制和监视进程的资源使用(如 CPU、内存、磁盘 I/O 等)。cgroup 目录是用于管理这些 cgroup 的文件系统目录,通常挂载在 /sys/fs/cgroup 下。通过在 cgroup 目录中的文件进行读写操作,可以实现对进程组的资源分配、限制和监控。

但这只适用于Linux系统下的docker容器,如果是Windows下,就不存在显式的控制组目录了(Windows下的docker是基于WSL的,一个虚拟Linux内核),那应该怎么办呢?

下面说明一种解决办法,这种方法可以正常启动mysql服务,但对于实际使用有没有影响有待检证。

查看mysql服务单元文件

 

[root@1a6bce4f17af /]# cat /usr/lib/systemd/system/mysqld.service
# Copyright (c) 2015, 2023, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# systemd service file for MySQL forking server
#

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/var/run/mysqld/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
ExecStartPre=/usr/bin/mysqld_pre_systemd

# Start main service
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false
将下面这行注释掉
PIDFile=/var/run/mysqld/mysqld.pid

 保存并重新加载服务单元,再重启mysql服务

[root@1a6bce4f17af /]# systemctl daemon-reload
[root@1a6bce4f17af /]# systemctl restart mysqld
[root@1a6bce4f17af /]# 

再次查看MySQL服务状态,可以看到启动成功

 

posted on 2024-04-30 22:55  sxb_sunday  阅读(25)  评论(0编辑  收藏  举报