个人学习笔记,谢绝转载!!!
原文:https://www.cnblogs.com/wshenjin/p/10100949.html
#Dockerfile for mysql5.7
FROM centos
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
libcurl libcurl-devel
zlib zlib-devel
&& yum clean all ;
cd /src/
&& tar xf percona-server-5.7.23-24.tar.gz
&& mv boost_1_59_0.tar.gz percona-server-5.7.23-24/
&& mkdir percona-server-5.7.23-24/build
&& cd percona-server-5.7.23-24/build
&& 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
-DWITH_BOOST=..
&& 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* iproute file make gcc cpp vim-minimal acl bind* rootfiles file
python-chardet gobject-introspection libxml2-python python-gobject-base basesystem libgomp
libstdc++-devel glibc-headers mpfr passwd yum-plugin-ovl dbus-glib python-kitchen kernel-headers
&& yum clean all ;
userdel mail ;
userdel ftp ;
cp /usr/share/zoneinfo/Asia/Shanghai /etc/ ;
ln -svf /etc/Shanghai /etc/localtime ;
rm -rf /usr/local/mysql/{mysql-test,sql-bench,support-files,data,man,README.MySQL,COPYING,INSTALL-BINARY,docs} ;
rm -rf /src /root/.bash* /root/.cshrc /root/.tcshrc /var/cache
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目录下需要拷贝的文件:
-rw-r--r-- 1 root root 83709983 Jun 30 2017 boost_1_59_0.tar.gz
-rwxr-xr-x 1 root root 1481 Nov 23 09:39 mysql_init.sh
-rw-rw-r-- 1 root root 81641623 Oct 31 22:55 percona-server-5.7.23-24.tar.gz
-rw-rw-r-- 1 root root 2320 Dec 13 12:46 my.cnf
由于MySQL5.7的初始化方式等和MySQL5.5有差异,所以ENTRYPOINT脚本也有一定的差异:
#!/bin/bash
#数据库初始化脚本
chown mysql:mysql -R /data/database/mysql
#这里只判断判断数据目录为空,则先初始化再启动
if [ -z "$(ls -A /data/database/mysql/)" ] ; then
#初始化新实例
init_res=$(/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/database/mysql/ 2>&1)
[ "$?" -eq 0 ] && echo "初始化数据库完成..." || exit 1
chmod 750 /data/database/mysql
chmod 700 /data/database/mysql/mysql
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
old_passwd=$( echo "$init_res" | awk '/A temporary password is generated for root@localhost:/{print $NF}')
#配置初始化密码,删除空账号
/usr/local/mysql/bin/mysqladmin -u root -p"$old_passwd" password `cat /data/save/mysql_root`
[ "$?" -eq 0 ] && echo "root密码已重置..." || exit 1
#关闭mysqld
/usr/local/mysql/bin/mysqladmin -uroot -p`cat /data/save/mysql_root` shutdown
while ps uax | grep -v grep |grep $mysqld_pid > /dev/null 2>&1
do
echo "等待mysqld关闭..."
sleep 2s
done
[ "$?" -eq 0 ] && echo "重启mysqld..." || exit 1
fi
exec "$@"