Docker MySQL5.5镜像

个人学习笔记,谢绝转载!!!
原文:https://www.cnblogs.com/wshenjin/p/9585696.html


定制MySQL的镜像有个很大的难题:mysqld启动之前要初始化数据目录,5.5自带有空账号密码需要初始化。

Dockerfile

FROM centos

# 拷贝需要的安装和MySQL初始脚本
COPY ["src","/src"]

RUN groupadd -g 1003 mysql \
    && useradd -u 1003 -g mysql -s /sbin/nologin mysql ;\
    yum install -y gcc \
    gcc-c++ \
    cmake \
    make \
    readline-devel \
    && yum clean all ;\
    cd /src/ \
    && tar xf percona-server-5.5.61-38.13.tar.gz \
    && cd percona-server-5.5.61-38.13 \
    && CHOST="x86_64-pc-linux-gnu" CFLAGS="-march=nocona -O2 -pipe" CXXFLAGS="-march=nocona -O2 -pipe" \
    cmake . \
    -DMYSQL_USER=mysql \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DSYSCONFDIR:PATH=/usr/local/mysql \
    -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql \
    -DENABLED_PROFILING:BOOL=ON \
    -DENABLE_DEBUG_SYNC:BOOL=OFF \
    -DMYSQL_DATADIR:PATH=/data/database/mysql \
    -DMYSQL_MAINTAINER_MODE:BOOL=OFF \
    -DWITH_EXTRA_CHARSETS=all \
    -DWITH_BIG_TABLES:BOOL=ON \
    -DWITH_FAST_MUTEXES:BOOL=ON \
    -DENABLE-PROFILING:BOOL=ON \
    -DWITH_SSL:STRING=bundled \
    -DWITH_UNIT_TESTS:BOOL=OFF \
    -DWITH_ZLIB:STRING=bundled \
    -DWITH_PARTITION_STORAGE_ENGINE:BOOL=ON \
    -DWITH_PLUGINS=heap,csv,partition,innodb_plugin,myisam \
    -DEFAULT_COLLATION=utf8_general_ci \
    -DEFAULT_CHARSET=utf8 \
    -DENABLED_ASSEMBLER:BOOL=ON \
    -DENABLED_LOCAL_INFILE:BOOL=ON \
    -DENABLED_THREAD_SAFE_CLIENT:BOOL=ON \
    -DENABLED_EMBEDDED_SERVER:BOOL=OFF \
    -DWITH_CLIENT_LDFLAGS:STRING=all-static \
    -DINSTALL_LAYOUT:STRING=STANDALONE \
    -DCOMMUNITY_BUILD:BOOL=ON \
    -DWITH_SAFEMALLOC=OFF \
    -DENABLE_DTRACE=0 \
    -DWITHOUT_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 \
    -DWITHOUT_ARCHIVE_STORAGE_ENGINE=1 \
    && make -j`grep -c processor /proc/cpuinfo` \
    && make install ;\
    cd / ;\
    cp /src/mysql_init.sh / ;\
    cp /src/my.cnf /etc/ ;\
    ln -sv /usr/local/mysql/bin/* /usr/local/bin/ ;\
    mkdir /data/{save,database/mysql} -p ;\
    strings /dev/urandom | tr -dc A-Za-z0-9 | head -c20 > /data/save/mysql_root ;\
    chmod 600 /data/save/mysql_root ;\
    chmod 700 /data/save ;\
    chmod 750 /data/database/mysql ;\
    chown mysql:mysql -R /data/database/mysql ;\
    echo -e "/usr/local/mysql/bin/mysql -uroot -p\`cat /data/save/mysql_root\`" > /root/inmysql ;\
    echo -e "/usr/local/mysql/bin/mysqladmin -uroot -p\`cat /data/save/mysql_root\` 'shutdown'" > /root/mysql_stop ;\
    chmod 700 /root/mysql_stop /root/inmysql ;\
    yum remove -y iputils* \
    bind* \
    vim* \
    make \
    cmake \
    cpp \
    acl \
    rootfiles \
    lzo \
    readline-devel \ 
    python-chardet \
    hostnamed \
    bus-python \
    gobject-introspection \
    libxml2-python \
    python-gobject-base \
    basesystem \
    libgomp \
    libstdc++-devel \
    glibc-headers \
    mpfr \
    passwd \
    yum-plugin-ovl \
    dbus-glib \
    python-kitchen \
    ncurses-devel \
    kernel-headers \
    gpg-pubkey \ 
    yum-util ;\
    userdel mail ;\
    userdel ftp ;\
    userdel games ;\
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/ ;\
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/ ;\
    ln -svf /etc/Shanghai /etc/localtime ;\
    rm -rf /src /root/.bash* /root/.cshrc  /root/.tcshrc /var/cache /usr/share/zoneinfo ;\ 
    rm -rf /usr/local/mysql/{mysql-test,sql-bench,support-files,data,man,README.MySQL,COPYING,INSTALL-BINARY,docs}

ENTRYPOINT ["/mysql_init.sh"]
HEALTHCHECK --interval=60s --timeout=5s CMD /usr/local/mysql/bin/mysqladmin -uroot -p$(cat /data/save/mysql_root) ping | grep alive || exit 1

EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld","--defaults-file=/etc/my.cnf","--user=mysql"]

src目录:

[root@Docker_Machine_192.168.31.130 ~]# cd /data/dokcer/dockerfile/mysql5.5/ 
[root@Docker_Machine_192.168.31.130 mysql5.5]# ll
total 21820
-rw-r--r-- 1 mysql mysql     3673 Dec 13 14:07 my.cnf
-rwxr-xr-x 1 root  root      1705 Dec 13 17:20 mysql_init.sh
-rw-r--r-- 1 root  root  22332795 Sep  4 16:10 percona-server-5.5.61-38.13.tar.gz

mysql_init.sh

脚本判断数据目录为空就进行初始化mysqld,然后再启动mysql

#!/bin/bash
#数据库初始化脚本

chown mysql:mysql -R /data/database/mysql
#判断数据目录为空,则先初始化再启动
if [ -z "$(ls -A /data/database/mysql/)" ] ; then
    #初始化新实例
    /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --datadir=/data/database/mysql --basedir=/usr/local/mysql  --user=mysql
    [ "$?" -eq 0 ] && echo "初始化数据库完成..." || exit 1

    chmod 750 /data/database/mysql
    chmod 700 /data/database/mysql/mysql
    chmod 700 /data/database/mysql/test
    chmod 660 /data/database/mysql/mysql/*
    #启动mysqld新实例
    /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf > /dev/null &
    mysqld_pid="$!"

    #等待mysqld启动完成
    while [ ! -S /tmp/mysql.sock ]
    do
         if ps uax | grep -v grep |grep $mysqld_pid > /dev/null 2>&1 ; then
             echo "等待mysqld启动..."
             sleep 2s
         else
             echo "mysqld启动失败..."
             exit 1
         fi
    done
    [ "$?" -eq 0 ] && echo "mysqld启动完成..." || exit 1

    #配置初始化密码,删除空账号
    /usr/local/mysql/bin/mysqladmin -u root  password `cat /data/save/mysql_root` 
    /usr/local/mysql/bin/mysqladmin -h 127.0.0.1 -u root password `cat /data/save/mysql_root`
    [ "$?" -eq 0 ] && echo "root密码已重置..." || exit 1
    /usr/local/mysql/bin/mysql -u root -p`cat /data/save/mysql_root` -e "DELETE FROM mysql.user WHERE password='' OR user=''; FLUSH PRIVILEGES ; "
    [ "$?" -eq 0 ] && echo "空账号已清理..." || exit 1

    #关闭mysqld
    /usr/local/mysql/bin/mysqladmin -uroot -p`cat /data/save/mysql_root` shutdown
    [ "$?" -eq 0 ] && echo "重启mysqld..." || exit 1
fi

exec "$@"

构建镜像

docker build -t volumes/percona-server:v5.5.61   .  

启动容器

docker run -tid -v /data/dokcer/docker_local_volume/mysql_test1_3306/data/:/data/database/mysql -v /data/dokcer/docker_local_volume/mysql_test1_3306/etc/my.cnf:/etc/my.cnf --name=percona-server-v5.5.61_test1  volumes/percona-server:v5.5.61

第一次启动因为需要初始化配置,所以比较慢,可以通过logs 参数查看

  • 数据目录挂载到:/data/database/mysql
  • 配置文件挂载到:/etc/my.cnf

进入MySQL

docker exec -it  percona-server-v5.5.61_test1 sh /root/inmysql 

停止容器

docker exec percona-server-v5.5.61_test1 sh /root/mysql_stop 

docker stop percona-server-v5.5.61_test1

说明一点:容器实例中的1号进程需要具备接收外部型号,然后根据信号控制其子进程的能力。

posted @ 2018-09-04 17:18  wshenJin  阅读(565)  评论(0编辑  收藏  举报