# Origin: https://hub.docker.com/r/openresty/openresty
# Copyright (C) 2016-2020  Eric D. Evan Wies
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

ARG	BASE_IMAGE

FROM	${BASE_IMAGE}

ARG	MYSQL_VER

MAINTAINER	Luan Shengde <shdluan@163.com>

RUN	yum -y update && \
	yum -y install shadow && \
	userdel $(awk -F':' '{if($3 == 999)print $1}' /etc/passwd) && \
	groupadd -g 999 mysql && \
	useradd -r -g 999 -u 999 mysql && \
	yum -y install gcc gcc-c++ ncurses-devel openssl-devel cmake3 \
	       bison bzip2 tar libtirpc-devel rpcgen make m4 findutils wget libudev-devel

#	The time period for downloading mysql maybe a long time
#	Suggest pre-download it and use COPY
#	COPY	mysql-boost-${MYSQL_VER}.tar.gz /
#	when execut make, customize threads num for make
#	example:
#		make -j 16
#		make -j 96
#	use nproc to check the max threads num,
#	better use a little numer of threads
RUN	wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-${MYSQL_VER}.tar.gz -o /mysql-boost-${MYSQL_VER}.tar.gz && \
	tar xf mysql-boost-${MYSQL_VER}.tar.gz && \
	mkdir -p /mysql-${MYSQL_VER}/build && \
	cd /mysql-${MYSQL_VER}/build && \
	cmake ../ -DWITH_BOOST=../boost/ && \
	make && \
	make install && \
	rm -f /mysql-boost-${MYSQL_VER}.tar.gz  && \
	rm -rf /mysql-${MYSQL_VER}

RUN	ln -s /usr/local/mysql/bin/mysqld /usr/sbin/mysqld && \
	ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql && \
	ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/mysqladmin && \
	ln -s /usr/local/mysql/bin/mysql_tzinfo_to_sql /usr/bin/mysql_tzinfo_to_sql

RUN	mkdir -p /var/run/mysqld && \
	mkdir -p /var/lib/mysql && \
	mkdir -p /var/lib/mysql-files && \
	chmod -R 777 /var/run/mysqld && \
	chmod -R 755 /var/lib/mysql && \
	chown -R mysql:mysql /var/lib/mysql && \
	chmod -R 777 /var/lib/mysql-files && \
	ln -s /usr/local/mysql/share/english/errmsg.sys /usr/share/errmsg.sys

#	the command gousu is required when execute script: docker-entrypoint.sh
#	docker-entrypoint.sh is used to initialize mysql database
#	download gosu may take a long time.
#	Suggest pre-download them and use COPY
#	COPY	gosu /usr/local/bin/
#	COPY	gosu.asc /usr/local/bin/
RUN	wget -o /usr/local/bin/gosu https://github.com/tianon/gosu/releases/download/1.16/gosu-arm64
RUN	wget -o /usr/local/bin/gosu.asc https://github.com/tianon/gosu/releases/download/1.16/gosu-arm64.asc
RUN	chmod +x /usr/local/bin/gosu

COPY	mysql/8.0/docker-entrypoint.sh /usr/local/bin/

RUN	ln -s /usr/local/bin/docker-entrypoint.sh /entrypoint.sh && \
	ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

RUN     yum -y remove gcc gcc-c++ ncurses-devel cmake bison libtirpc-devel m4 rpcgen

VOLUME	[/var/lib/mysql]

ENTRYPOINT	["docker-entrypoint.sh"]

EXPOSE 3306

CMD	["mysqld"]
