Docker mysql通过MariaDB与Supervisor

nfs0ujit  于 12个月前  发布在  Mysql
关注(0)|答案(2)|浏览(198)

大约一年前,我提出了扩展我的Docker知识的想法,开始以创建一种用于开发目的的多平台服务器映像,从那时起,我想出了如何让Nginx和PHP-fpm在稳定的环境中运行。这一切都是基于Debian映像。现在,从几个星期前开始,我想在映像中添加MySQL功能。起初,我尝试了正常的MySQL(-server)镜像,在尝试修复它无法在我的镜像中运行的错误后,我切换到使用MariaDB -我甚至改变了MySQL的Docker镜像以适应我的需要(当然,因为我的项目使用了几个服务,所以将CMD ["mysqld"]替换为supervisord.conf可执行文件)。我试着弄清楚它几天,但它仍然没有运行。目前,我选择使用https://hub.docker.com/_/mariadb(第二:10.4.12-bionic,10.4-bionic,10-bionic,bionic,10.4.12,10.4,10,最新)与我的形象。
我在写这篇文章的时候刚刚创建了一个mariadb副本,但是替换了直接执行mysqld(工作)。当这个主题被创建的时候,它没有和一个supervisord一起工作,现在它应该工作了。
我有一个docker-compose.yml,它将在这里启动,代码如下:

version: "3"
services:
  db:
    container_name: mariadb
    image: mariadb
    build: .
    restart: on-failure
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=test123
    networks: 
      - local-network

networks:
  local-network:
    driver: bridge

字符串
然后,我将使用(--build)参数执行docker-compose up -d或。
这背后的Dockerfile是:

FROM debian:buster-slim

ENV DEBIAN_FRONTEND noninteractive

ENV GOSU_VERSION 1.12

ENV MARIADB_VERSION 10.4

ENV GPG_KEYS \
    199369E5404BD5FC7D2FE43BCBCB082A1BB943DB \
    177F4010FE56CA3336300305F1656F24C74CD1D8

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mysql && useradd -r -g mysql mysql

RUN apt-get update && apt-get install --no-install-recommends --no-install-suggests -q -y \
    wget \
    ca-certificates \
    gnupg \
    gnupg1 \
    gnupg2 \
    dirmngr \
    pwgen \
    tzdata \
    xz-utils

# Get Gosu for easy stepdown from root (to avoid sudo/su miscommunications)
# https://github.com/tianon/gosu/releases
RUN set -eux; \
    savedAptMark="$(apt-mark showmanual)"; \
    dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
    wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
    wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
    export GNUPGHOME="$(mktemp -d)"; \
    gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
    gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
    gpgconf --kill all; \
    rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \
    apt-mark auto '.*' > /dev/null; \
    [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
    apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
    chmod +x /usr/local/bin/gosu; \
    gosu --version; \
    gosu nobody true

RUN mkdir /docker-entrypoint-initdb.d

RUN set -ex; \
    export GNUPGHOME="$(mktemp -d)"; \
    for key in $GPG_KEYS; do \
        gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
    done; \
    gpg --batch --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mariadb.gpg; \
    command -v gpgconf > /dev/null && gpgconf --kill all || :; \
    rm -r "$GNUPGHOME"; \
    apt-key list

# Add MariaDB repo
RUN set -e;\
    echo "deb http://downloads.mariadb.com/MariaDB/mariadb-$MARIADB_VERSION/repo/debian buster main" > /etc/apt/sources.list.d/mariadb.list; \
    { \
        echo 'Package: *'; \
        echo 'Pin: release o=MariaDB'; \
        echo 'Pin-Priority: 999'; \
    } > /etc/apt/preferences.d/mariadb

# Install MariaDB and set custom requirements
RUN set -ex; \
    { \
        echo "mariadb-server" mysql-server/root_password password 'unused'; \
        echo "mariadb-server" mysql-server/root_password_again password 'unused'; \
    } | debconf-set-selections; \
    apt-get update && apt-get install --no-install-recommends --no-install-suggests -y -q \
    mariadb-server \
    mariadb-backup \
    socat; \
# comment out any "user" entires in the MySQL config ("docker-entrypoint.sh" or "--user" will handle user switching)
    sed -ri 's/^user\s/#&/' /etc/mysql/my.cnf /etc/mysql/conf.d/*; \
# making sure that the correct permissions are set
    mkdir -p /var/lib/mysql /var/run/mysqld; \
    chown -R mysql:mysql /var/lib/mysql /var/run/mysqld; \
# comment out a few problematic configuration values
    find /etc/mysql/ -name '*.cnf' -print0 \
        | xargs -0 grep -lZE '^(bind-address|log)' \
        | xargs -rt -0 sed -Ei 's/^(bind-address|log)/#&/'; \
# don't reverse lookup hostnames, they are usually another container
    echo '[mysqld]\nskip-host-cache\nskip-name-resolve' > /etc/mysql/conf.d/docker.cnf

# Setup the Supervisor
RUN apt-get update && apt-get install supervisor -y \
    && mkdir -p /var/log/supervisor

COPY /supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN chmod +x /etc/supervisor/conf.d/supervisord.conf

VOLUME /var/lib/mysql

COPY /docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh \
    && ln -s /usr/local/bin/docker-entrypoint.sh /

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 3306 33060

# call and execute the supervisor after build
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]


经过几天的工作,对固定的形象,我认为supervisord是问题,它不能运行,因为这或什么.好吧,这里是supervisord:

[supervisord]
logfile=/var/log/supervisord.log
nodaemon=true
user=root

[program:mysql]
command=/usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql
process_name=mysqld
priority=1
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stdout_events_enabled=true
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
stderr_events_enabled=true
autorestart=true
user=mysql


镜像构建完成后,mysql将被supervisor执行。但是,问题是我想使用https://github.com/mariadb-corporation/mariadb-server-docker/tree/master/10.4的入口点-我在Bash中不是很有名,所以需要一些时间来练习。无论如何,docker-entrypoint第一次没有执行,数据库将不会初始化。我能做的是,我正在创建一个自己的shell脚本来初始化它。测试了一下,它工作了,但是为什么我不能使用默认的入口点作为第一选择呢?
是否在Supervisord命令之间的某个点出错- docker-entrypoint与mysql连接点或其他东西?
我真的希望有人能帮我。

**编辑[04/26/2020]:**描述了从现在开始的最新情况,数据库未初始化,没有来自entrypoint脚本的消息,注解或警告。

问候,科林

d6kp6zgx

d6kp6zgx1#

MySQL服务应该以root用户的身份运行,但后来是mysql用户试图访问“socket”。因此,socket目录应该由mysql用户访问,但Superviser以root用户的身份运行mysql服务。
我通过在我的Dockerfile中创建MySQL套接字目录并给予正确的权限来解决这个问题:

ARG MARIADB_MYSQL_SOCKET_DIRECTORY='/var/run/mysqld'

RUN mkdir -p $MARIADB_MYSQL_SOCKET_DIRECTORY && \
    chown root:mysql $MARIADB_MYSQL_SOCKET_DIRECTORY && \
    chmod 774 $MARIADB_MYSQL_SOCKET_DIRECTORY

字符串
然后像这样配置Supervisor:

[program:mariadb]
command=/usr/sbin/mysqld
autorestart=true
user=root

wnrlj8wa

wnrlj8wa2#

你应该能够chown套接字到mysql的userid。

相关问题