制作mysql通用docker镜像包,一是包要小,二是包的运行要独立,把配置文件,日志目录 ,数据目录独立出来。

docker images
#没看到基础镜像
docker pull centos:6.9
#拉取一个基础镜像
docker images
REPOSITORY          TAG                  IMAGE ID            CREATED             VIRTUAL SIZE
centos              6.9                  657531b0552b        9 hours ago         194.7 MB
#外部目录
mkdir -p /data/mysql5.7/data
mkdir -p /data/mysql5.7/conf
mkdir -p /data/mysql5.7/log


docker run --net=host -i -t -v /home/mysql5.7/:/mnt/mysql/ --name mysql5.7 657531b0552b /bin/bash
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-1.el6.x86_64.rpm-bundle.tar
tar -xvf mysql-5.7.20-1.el6.x86_64.rpm-bundle.tar

yum install -y numactl perl
yum install -y libaio
yum install -y initscripts

rpm -ivh mysql-community-common-5.7.20-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.20-1.el6.x86_64.rpm
#mysql-community-common(x86-64) >= 5.7.9 is needed by mysql-community-libs-5.7.20-1.el6.x86_64
[root@tmp]# rpm -qa |grep -i mysql
rpm -e mysql-devel-5.1.73-8.el6_8.x86_64
rpm -e apr-util-mysql-1.3.9-3.el6_0.1.x86_64
rpm -e mysql-libs-5.1.73-8.el6_8.x86_64
rpm -e mysql-5.1.73-8.el6_8.x86_64

#其中rpm -e mysql-libs-5.1.73-8.el6_8.x86_64卸载不掉,需卸载postfix后方可
重装安装
rpm -ivh mysql-community-common-5.7.20-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.20-1.el6.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.20-1.el6.x86_64.rpm
rpm -ivh mysql-community-client-5.7.20-1.el6.x86_64.rpm

rpm -ivh mysql-community-server-5.7.20-1.el6.x86_64.rpm

warning: mysql-community-server-5.7.20-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
    /sbin/service is needed by mysql-community-server-5.7.20-1.el6.x86_64
    
rpm -ivh mysql-community-server-5.7.20-1.el6.x86_64.rpm --force --nodeps

#若报
#warning: mysql-community-server-5.7.20-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
    libsasl2.so.2()(64bit) is needed by mysql-community-server-5.7.20-1.el6.x86_64

wget https://rpmfind.net/linux/mageia/distrib/3/x86_64/media/core/updates/lib64sasl2-2.1.25-12.1.mga3.x86_64.rpm
rpm -ivh lib64sasl2-2.1.25-12.1.mga3.x86_64.rpm
rpm -ivh mysql-community-server-5.7.20-1.el6.x86_64.rpm
#-------------------

chmod -R 775 /mnt/mysql/


#把配置文件挪到外部
mv my.cnf /mnt/mysql/conf/ 
#再建议软链接
ln -s /mnt/mysql/conf/my.cnf  /etc/my.cnf

vi /etc/my.cnf
#修改
datadir=/var/lib/mysql
#为
datadir=/mnt/mysql/data
#修改
log-error=/var/log/mysqld.log
#为
log-error=/mnt/mysql/log/mysqld.log

#修改端口
port=8808
user=mysql

service mysqld start

chown -R mysql:mysql /mnt/mysql

service mysqld start

#启动成功

#设置root密码
vi /etc/my.cnf
添加一条
skip-grant-tables
保存重启mysql
service mysqld restart

mysql -u root -p
use mysql

update user set password_expired='N' where user='root';    
update user set authentication_string=password('123456') where user='root';
flush privileges;
#基本测试没问题
#导出镜像
docker export aeee48d8f0e6 > /usr/tmp/mysql5.7.21.tar

#导入示范 docker import - mysqltest < /tmp/mysql.tar