使用docker 基于centos7制作mysql镜像

说明:由于业务需要使用centos7.6+mysql5.7+jdk8以及其他的java程序,本想在网上找一个现成的,发现镜像都不适合我。

一、yum方式安装mysql

1、编写dockerfile文件

#FROM   centos:
FROM    xxlaila/centos7.-jdk1.
LABEL   maintainer='xxx' centos_version='7.6' mysql_version='5.7' jdk_version='1.8'
#ADD     jdk-8u181-linux-x64.tar.gz  /usr/local
COPY    bootstrap.sh    /etc/
WORKDIR /usr/local
RUN     yum install wget crontabs iproute -y \
        && wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm \
        && rpm -ivh mysql57-community-release-el7-.noarch.rpm  \
        && yum install install mysql-server -y --nogpgcheck \
        && mysqld --initialize --explicit_defaults_for_timestamp --user=mysql \
        && chmod  /etc/bootstrap.sh \

#ENV     PATH /usr/local/jdk/bin:$PATH
COPY    my.cnf /etc/my.cnf
COPY    createDB.sql /mysql/createDB.sql
COPY    privileges.sql /mysql/privileges.sql

EXPOSE
ENTRYPOINT ["/etc/bootstrap.sh"]
CMD     ["/usr/sbin/init"]

2、bootstrap.sh文件

#!/bin/bash
set -x
#判断mysql root密码是否被赋值,如果没有赋值,给定默认值
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-123456}
#判断mysql是否启动
mysqld --user=mysql &
sleep
function start_mysql(){
..};do
    PORT=$(ss -anlp|grep |wc -l)
     ]; then
        echo "Mysql 未启动,启动mysql"
        mysqld --user=mysql &
        sleep
    else
       echo "Mysql 以启动"
       break
    fi

     ]; then
        echo >& 'MySQL 启动失败!'
        exit
    fi
done
}
start_mysql
#获取mysql初始密码
PASSWORD=$(awk '/password/{print $NF}' /var/log/mysqld.log)
echo "修改mysql密码..."
mysql --connect-expired-password -uroot -p>&
#判断密码是否修改成功
#if [ $? -eq 0 ];then
#    echo "Mysql密码修改成功..."
#else
#   echo "Mysql密码修改失败..."
#   exit
#fi
#设置环境变量
MYSQL="mysql --protocol=socket -uroot -p${MYSQL_ROOT_PASSWORD}"
#初始化sql
echo "1. 初始化mysql..."
#、判断数据库test_db是否存在
$MYSQL  -e >&
 ]; then
    echo '2. creating database...'
    $MYSQL < /mysql/createDB.sql

    echo '4. flush privileges....'
   $MYSQL < /mysql/privileges.sql
fi
# check mysql status
#echo `service mysql status`
echo '5. mysql for test_db is ready...'

3、my.cnf

[mysqld]
server-id=
max_connections=
#basedir = /usr/local/mysql
#datadir = /data/mysql/data
port =
#secure_file_priv=/data/mysql/data
query_cache_type=
query_cache_size=120M
query_cache_min_res_unit=
character_set_server=utf8
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
general_log=ON
slow_query_log=
long_query_time=
#log-error=/data/mysql/logs/mysql.err
#log_bin=/data/mysql/logs/mysql-bin
#slow-query-log-file=/data/mysql/logs/slowquery.log
#socket = /data/mysql/mysql.sock
symbolic-links=
character-set-server=gbk
#######################
[client]
#socket=/data/mysql/mysql.sock
[mysql]
default-character-set=gbk

二、基于源码安装mysql

1、dockerfile文件

FROM    centos:
LABEL   maintainer='xxx' centos_version='7.7' mysql_version='5.7' jdk_version='1.8'
#拷贝并解压mysql、jdk软件包
ADD     jdk-8u181-linux-x64.tar.gz  /usr/local
ADD     mysql--linux-glibc2.-x86_64.tar.gz /usr/local
WORKDIR         /usr/local
COPY    setup.sh /usr/bin/

RUN     ln -s mysql--linux-glibc2.-x86_64 mysql \
        && ln -s jdk1..0_181 jdk \
        && groupadd -r mysql \
        &&   useradd -r -g mysql mysql \
        &&   yum install -y crontabs libaio libaio-devel numactl.x86_64 iproute \
        &&  cat /data/cron | crontab \
        && chmod  /usr/bin/setup.sh

ENV     PATH /usr/local/mysql/bin:/usr/local/jdk/bin:$PATH
EXPOSE   

#拷贝初始化mysql的sql文件
COPY my.cnf /etc/my.cnf
COPY createDB.sql /mysql/createDB.sql

COPY privileges.sql /mysql/privileges.sql

#执行初始化脚本文件
ENTRYPOINT ["setup.sh"]

2、setup.sh文件信息

#!/bin/bash
set -x
echo "----开始初始化mysql----------------------"
mkdir -p  /data/mysql/{data,logs,conf,mysql.pid}
touch /data/mysql/logs/mysql.err
chown -R mysql.mysql /data/mysql
#curl -o /data/mysql/conf/my.cnf  http://124.207.22.13/logs/Math/my.cnf
#ln -s /data/mysql/conf/my.cnf /etc/my.cnf
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod  /etc/init.d/mysqld
#ln -s /usr/local/mysql/bin/* /usr/bin/
echo "初始化数据库完毕.数据持目录:/data/mysql"

echo "设置变量"
passwd="$(awk '/localhost/{print $NF}' /data/mysql/logs/mysql.err|head -1)"
dbinit=( mysql --connect-expired-password -uroot -p"$passwd" )
#sed -i '/\[mysqld\]/a\skip-grant-tables' /etc/my.cnf

echo "启动数据库"
#启动数据库并判断状态
sed -i "47cdatadir=/data/mysql/data" /etc/init.d/mysqld
function start_mysql(){
/etc/init.d/mysqld restart && sleep 3
echo "启动后查看进程"
port=$(ss -lntp|grep "3306"|wc -l)
for p in {3..0}; do
  if [ $port -ne 1 ];then
     echo "Mysql启动失败-重新启动"
     /etc/init.d/mysqld restart && sleep 2
  else
       echo "Mysql 已经启动"
       break
  fi
  if [ "$p" = 0 ];then
    echo >&2 'MySQL 启动失败!'
        exit 1
  fi
done
}
start_mysql
echo "修改数据库密码"
#passinit="alter  user 'root'@'localhost' identified by '123456';"
#echo $passinit | $dbinit
#echo $dbinit

mysql --connect-expired-password  -uroot -p"$passwd" -e "alter  user 'root'@'localhost' identified by '123456';"
echo "$passwd"  密码

#echo "重新启动mysql"
#start_mysql

#初始化sql
#mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="/data/mysql/mysql.sock" -p123456)
mysql="mysql --protocol=socket -uroot -hlocalhost --socket=/data/mysql/mysql.sock -p123456"

#echo '1. starting mysql...'
#service mysql start
$mysql  -e "use test_db;" > /dev/null 2>&1
if [ $? != 0 ]; then
echo '2. creating database...'
$mysql < /mysql/createDB.sql

 set password
echo '3. flush privileges....'
$mysql < /mysql/privileges.sql
fi
# check mysql status
#echo `service mysql status`
echo '4. mysql for test_db is ready...'

3、my.cnf

[mysqld]
server-id=
max_connections=
basedir = /usr/local/mysql
datadir = /data/mysql/data
port =
secure_file_priv=/data/mysql/data
query_cache_type=
query_cache_size=120M
query_cache_min_res_unit=
character_set_server=utf8
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
general_log=ON
slow_query_log=
long_query_time=
log-error=/data/mysql/logs/mysql.err
log_bin=/data/mysql/logs/mysql-bin
slow-query-log-file=/data/mysql/logs/slowquery.log
socket = /data/mysql/mysql.sock
symbolic-links=
character-set-server=gbk
#######################
[client]
socket=/data/mysql/mysql.sock
[mysql]
default-character-set=gbk

注:基于源码安装比基于yum安装方式,最后生成的镜像大一点多G,如下:

使用docker 基于centos7制作mysql镜像

1.78GB是yuam方式安装

3.31GB这个是源码安装,这种方式安装以后,镜像实在是有点太大了。

总的来说,这个镜像还是太臃肿,最好是几百兆就完美了。

借鉴:

https://www.jianshu.com/p/121e045d14f7

https://blog.csdn.net/weixin_33981932/article/details/92600770

上一篇:[BS-18] 对OC中不可变类的理解


下一篇:navicat与phpmyadmin做mysql的自定义函数和事件