Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a20e2bd
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM alpine:latest | ||
MAINTAINER leafney "babycoolzx@126.com" | ||
|
||
RUN apk update && \ | ||
apk add mysql mysql-client && \ | ||
addgroup mysql mysql && \ | ||
mkdir /scripts && \ | ||
rm -rf /var/cache/apk/* | ||
|
||
VOLUME ["/var/lib/mysql"] | ||
|
||
COPY ./startup.sh /scripts/startup.sh | ||
RUN chmod +x /scripts/startup.sh | ||
|
||
EXPOSE 3306 | ||
|
||
ENTRYPOINT ["/scripts/startup.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Docker + Alpine-3.4 + Mysql/MariaDB-10.1.18 | ||
|
||
#### parameter | ||
|
||
* `MYSQL_ROOT_PWD` : root Password default "mysql" | ||
* `MYSQL_USER` : new User | ||
* `MYSQL_USER_PWD` : new User Password | ||
* `MYSQL_USER_DB` : new Database for new User | ||
|
||
#### build image | ||
|
||
``` | ||
$ docker build -t="leafney/docker-alpine-mysql" . | ||
``` | ||
|
||
#### run a default contaier | ||
|
||
``` | ||
$ docker run --name mysql -v /mysql/data/:/var/lib/mysql -d -p 3306:3306 leafney/docker-alpine-mysql | ||
``` | ||
|
||
#### run a container with new User and Password | ||
|
||
``` | ||
$ docker run --name mysql -v /mysql/data/:/var/lib/mysql -d -p 3306:3306 -e MYSQL_ROOT_PWD=123 -e MYSQL_USER=dev -e MYSQL_USER_PWD=dev leafney/docker-alpine-mysql | ||
``` | ||
|
||
#### run a container with new Database for new User and Password | ||
|
||
``` | ||
$ docker run --name mysql -v /mysql/data/:/var/lib/mysql -d -p 3306:3306 -e MYSQL_ROOT_PWD=123 -e MYSQL_USER=dev -e MYSQL_USER_PWD=dev -e MYSQL_USER_DB=userdb leafney/docker-alpine-mysql | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/bin/sh | ||
|
||
# parameters | ||
MYSQL_ROOT_PWD=${MYSQL_ROOT_PWD:-"mysql"} | ||
MYSQL_USER=${MYSQL_USER:-""} | ||
MYSQL_USER_PWD=${MYSQL_USER_PWD:-""} | ||
MYSQL_USER_DB=${MYSQL_USER_DB:-""} | ||
|
||
if [ ! -d "/run/mysqld" ]; then | ||
mkdir -p /run/mysqld | ||
chown -R mysql:mysql /run/mysqld | ||
fi | ||
|
||
if [ -d /var/lib/mysql/myql ]; then | ||
echo '[i] MySQL directory already present, skipping creation' | ||
else | ||
echo "[i] MySQL data directory not found, creating initial DBs" | ||
|
||
chown -R mysql:mysql /var/lib/mysql | ||
|
||
# init database | ||
echo 'Initializing database' | ||
mysql_install_db --user=mysql > /dev/null | ||
echo 'Database initialized' | ||
|
||
echo "[i] MySql root password: $MYSQL_ROOT_PWD" | ||
|
||
# create temp file | ||
tfile=`mktemp` | ||
if [ ! -f "$tfile" ]; then | ||
return 1 | ||
fi | ||
|
||
# save sql | ||
echo "[i] Create temp file: $tfile" | ||
cat << EOF > $tfile | ||
USE mysql; | ||
FLUSH PRIVILEGES; | ||
DELETE FROM mysql.user; | ||
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PWD' WITH GRANT OPTION; | ||
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$MYSQL_ROOT_PWD' WITH GRANT OPTION; | ||
EOF | ||
|
||
|
||
# Create new database | ||
if [ "$MYSQL_USER_DB" != "" ]; then | ||
echo "[i] Creating database: $MYSQL_USER_DB" | ||
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_USER_DB\` CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile | ||
|
||
# set new User and Password | ||
if [ "$MYSQL_USER" != "" ] && [ "$MYSQL_USER_PWD" != "" ]; then | ||
echo "[i] Creating user: $MYSQL_USER with password $MYSQL_USER_PWD" | ||
echo "GRANT ALL ON \`$MYSQL_USER_DB\`.* to '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_USER_PWD';" >> $tfile | ||
fi | ||
else | ||
# don`t need to create new database,Set new User to control all database. | ||
if [ "$MYSQL_USER" != "" ] && [ "$MYSQL_USER_PWD" != "" ]; then | ||
echo "[i] Creating user: $MYSQL_USER with password $MYSQL_USER_PWD" | ||
echo "GRANT ALL ON *.* to '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_USER_PWD';" >> $tfile | ||
fi | ||
fi | ||
|
||
echo 'FLUSH PRIVILEGES;' >> $tfile | ||
|
||
# run sql in tempfile | ||
echo "[i] run tempfile: $tfile" | ||
/usr/bin/mysqld --user=mysql --bootstrap --verbose=0 < $tfile | ||
rm -f $tfile | ||
fi | ||
|
||
echo "[i] Sleeping 5 sec" | ||
sleep 5 | ||
|
||
echo '[i] start running mysqld' | ||
exec /usr/bin/mysqld --user=mysql --console |