diff --git a/5.5/Dockerfile b/5.5/Dockerfile new file mode 100644 index 0000000..a02b055 --- /dev/null +++ b/5.5/Dockerfile @@ -0,0 +1,33 @@ +FROM ubuntu:trusty +MAINTAINER Fernando Mayo , Feng Honglin + +# Add MySQL configuration +COPY my.cnf /etc/mysql/conf.d/my.cnf +COPY mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf + +RUN apt-get update && \ + apt-get -yq install mysql-server-5.5 pwgen && \ + rm -rf /var/lib/apt/lists/* && \ + rm /etc/mysql/conf.d/mysqld_safe_syslog.cnf && \ + if [ ! -f /usr/share/mysql/my-default.cnf ] ; then cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf; fi && \ + mysql_install_db > /dev/null 2>&1 && \ + touch /var/lib/mysql/.EMPTY_DB + +# Add MySQL scripts +COPY import_sql.sh /import_sql.sh +COPY run.sh /run.sh + +ENV MYSQL_USER=admin \ + MYSQL_PASS=**Random** \ + ON_CREATE_DB=**False** \ + REPLICATION_MASTER=**False** \ + REPLICATION_SLAVE=**False** \ + REPLICATION_USER=replica \ + REPLICATION_PASS=replica \ + ON_CREATE_DB=**False** + +# Add VOLUMEs to allow backup of config and databases +VOLUME ["/etc/mysql", "/var/lib/mysql"] + +EXPOSE 3306 +CMD ["/run.sh"] diff --git a/5.5/docker-compose.yml b/5.5/docker-compose.yml new file mode 100644 index 0000000..ce0e974 --- /dev/null +++ b/5.5/docker-compose.yml @@ -0,0 +1,4 @@ +db: + image: tutum/mysql:5.5 + environment: + MYSQL_PASS: "**ChangeMe**" diff --git a/5.5/import_sql.sh b/5.5/import_sql.sh new file mode 100755 index 0000000..975d021 --- /dev/null +++ b/5.5/import_sql.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +if [[ $# -ne 3 ]]; then + echo "Usage: $0 " + exit 1 +fi + +echo "=> Starting MySQL Server" +/usr/bin/mysqld_safe > /dev/null 2>&1 & +PID=$! + +RET=1 +while [[ RET -ne 0 ]]; do + echo "=> Waiting for confirmation of MySQL service startup" + sleep 5 + mysql -u"$1" -p"$2" -e "status" > /dev/null 2>&1 + RET=$? +done + +echo " Started with PID ${PID}" + +echo "=> Importing SQL file" +mysql -u"$1" -p"$2" < "$3" + +echo "=> Stopping MySQL Server" +mysqladmin -u"$1" -p"$2" shutdown + +echo "=> Done!" diff --git a/5.5/my.cnf b/5.5/my.cnf new file mode 100644 index 0000000..02fe8be --- /dev/null +++ b/5.5/my.cnf @@ -0,0 +1,6 @@ +[mysqld] +bind-address=0.0.0.0 +# http://www.percona.com/blog/2008/05/31/dns-achilles-heel-mysql-installation/ +skip_name_resolve +#server-id +#log-bin diff --git a/mysqld_charset.cnf b/5.5/mysqld_charset.cnf similarity index 100% rename from mysqld_charset.cnf rename to 5.5/mysqld_charset.cnf diff --git a/5.5/run.sh b/5.5/run.sh new file mode 100755 index 0000000..f529780 --- /dev/null +++ b/5.5/run.sh @@ -0,0 +1,190 @@ +#!/bin/bash + +set -m +set -e + +VOLUME_HOME="/var/lib/mysql" +CONF_FILE="/etc/mysql/conf.d/my.cnf" +LOG="/var/log/mysql/error.log" + +# Set permission of config file +chmod 644 ${CONF_FILE} +chmod 644 /etc/mysql/conf.d/mysqld_charset.cnf + +StartMySQL () +{ + /usr/bin/mysqld_safe ${EXTRA_OPTS} > /dev/null 2>&1 & + # Time out in 1 minute + LOOP_LIMIT=60 + for (( i=0 ; ; i++ )); do + if [ ${i} -eq ${LOOP_LIMIT} ]; then + echo "Time out. Error log is shown as below:" + tail -n 100 ${LOG} + exit 1 + fi + echo "=> Waiting for confirmation of MySQL service startup, trying ${i}/${LOOP_LIMIT} ..." + sleep 1 + mysql -uroot -e "status" > /dev/null 2>&1 && break + done +} + +CreateMySQLUser() +{ + if [ "$MYSQL_PASS" = "**Random**" ]; then + unset MYSQL_PASS + fi + + PASS=${MYSQL_PASS:-$(pwgen -s 12 1)} + _word=$( [ ${MYSQL_PASS} ] && echo "preset" || echo "random" ) + echo "=> Creating MySQL user ${MYSQL_USER} with ${_word} password" + + mysql -uroot -e "CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '$PASS'" + mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION" + echo "=> Done!" + echo "========================================================================" + echo "You can now connect to this MySQL Server using:" + echo "" + echo " mysql -u$MYSQL_USER -p$PASS -h -P" + echo "" + echo "Please remember to change the above password as soon as possible!" + echo "MySQL user 'root' has no password but only allows local connections" + echo "========================================================================" +} + +OnCreateDB() +{ + if [ "$ON_CREATE_DB" = "**False**" ]; then + unset ON_CREATE_DB + else + echo "Creating MySQL database ${ON_CREATE_DB}" + mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${ON_CREATE_DB};" + echo "Database created!" + fi +} + +ImportSql() +{ + for FILE in ${STARTUP_SQL}; do + echo "=> Importing SQL file ${FILE}" + if [ "$ON_CREATE_DB" ]; then + mysql -uroot "$ON_CREATE_DB" < "${FILE}" + else + mysql -uroot < "${FILE}" + fi + done +} + +# Main +if [ ${REPLICATION_MASTER} == "**False**" ]; then + unset REPLICATION_MASTER +fi + +if [ ${REPLICATION_SLAVE} == "**False**" ]; then + unset REPLICATION_SLAVE +fi + +# Initialize empty data volume and create MySQL user +if [[ ! -d $VOLUME_HOME/mysql ]]; then + echo "=> An empty or uninitialized MySQL volume is detected in $VOLUME_HOME" + echo "=> Installing MySQL ..." + if [ ! -f /usr/share/mysql/my-default.cnf ] ; then + cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf + fi + mysql_install_db || exit 1 + touch /var/lib/mysql/.EMPTY_DB + echo "=> Done!" +else + echo "=> Using an existing volume of MySQL" +fi + +# Set MySQL REPLICATION - MASTER +if [ -n "${REPLICATION_MASTER}" ]; then + echo "=> Configuring MySQL replication as master (1/2) ..." + if [ ! -f /replication_set.1 ]; then + RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})" + echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}" + sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE} + sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE} + touch /replication_set.1 + else + echo "=> MySQL replication master already configured, skip" + fi +fi + +# Set MySQL REPLICATION - SLAVE +if [ -n "${REPLICATION_SLAVE}" ]; then + echo "=> Configuring MySQL replication as slave (1/2) ..." + if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then + if [ ! -f /replication_set.1 ]; then + RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})" + echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}" + sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE} + sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE} + touch /replication_set.1 + else + echo "=> MySQL replication slave already configured, skip" + fi + else + echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'" + exit 1 + fi +fi + + +echo "=> Starting MySQL ..." +StartMySQL +tail -F $LOG & + +# Create admin user and pre create database +if [ -f /var/lib/mysql/.EMPTY_DB ]; then + echo "=> Creating admin user ..." + CreateMySQLUser + OnCreateDB + rm /var/lib/mysql/.EMPTY_DB +fi + + +# Import Startup SQL +if [ -n "${STARTUP_SQL}" ]; then + if [ ! -f /sql_imported ]; then + echo "=> Initializing DB with ${STARTUP_SQL}" + ImportSql + touch /sql_imported + fi +fi + +# Set MySQL REPLICATION - MASTER +if [ -n "${REPLICATION_MASTER}" ]; then + echo "=> Configuring MySQL replication as master (2/2) ..." + if [ ! -f /replication_set.2 ]; then + echo "=> Creating a log user ${REPLICATION_USER}:${REPLICATION_PASS}" + mysql -uroot -e "CREATE USER '${REPLICATION_USER}'@'%' IDENTIFIED BY '${REPLICATION_PASS}'" + mysql -uroot -e "GRANT REPLICATION SLAVE ON *.* TO '${REPLICATION_USER}'@'%'" + mysql -uroot -e "reset master" + echo "=> Done!" + touch /replication_set.2 + else + echo "=> MySQL replication master already configured, skip" + fi +fi + +# Set MySQL REPLICATION - SLAVE +if [ -n "${REPLICATION_SLAVE}" ]; then + echo "=> Configuring MySQL replication as slave (2/2) ..." + if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then + if [ ! -f /replication_set.2 ]; then + echo "=> Setting master connection info on slave" + mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT}, MASTER_CONNECT_RETRY=30" + mysql -uroot -e "start slave" + echo "=> Done!" + touch /replication_set.2 + else + echo "=> MySQL replication slave already configured, skip" + fi + else + echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'" + exit 1 + fi +fi + +fg diff --git a/5.6/Dockerfile b/5.6/Dockerfile new file mode 100644 index 0000000..7784afe --- /dev/null +++ b/5.6/Dockerfile @@ -0,0 +1,32 @@ +FROM ubuntu:trusty +MAINTAINER Fernando Mayo , Feng Honglin + +# Add MySQL configuration +COPY my.cnf /etc/mysql/conf.d/my.cnf +COPY mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf + +RUN apt-get update && \ + apt-get -yq install mysql-server-5.6 pwgen && \ + rm -rf /var/lib/apt/lists/* && \ + rm /etc/mysql/conf.d/mysqld_safe_syslog.cnf && \ + if [ ! -f /usr/share/mysql/my-default.cnf ] ; then cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf; fi && \ + mysql_install_db > /dev/null 2>&1 && \ + touch /var/lib/mysql/.EMPTY_DB + +# Add MySQL scripts +COPY import_sql.sh /import_sql.sh +COPY run.sh /run.sh + +ENV MYSQL_USER=admin \ + MYSQL_PASS=**Random** \ + ON_CREATE_DB=**False** \ + REPLICATION_MASTER=**False** \ + REPLICATION_SLAVE=**False** \ + REPLICATION_USER=replica \ + REPLICATION_PASS=replica + +# Add VOLUMEs to allow backup of config and databases +VOLUME ["/etc/mysql", "/var/lib/mysql"] + +EXPOSE 3306 +CMD ["/run.sh"] diff --git a/5.6/docker-compose.yml b/5.6/docker-compose.yml new file mode 100644 index 0000000..6854cd1 --- /dev/null +++ b/5.6/docker-compose.yml @@ -0,0 +1,4 @@ +db: + image: tutum/mysql:5.6 + environment: + MYSQL_PASS: "**ChangeMe**" diff --git a/5.6/import_sql.sh b/5.6/import_sql.sh new file mode 100755 index 0000000..f21b4f9 --- /dev/null +++ b/5.6/import_sql.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +if [[ $# -ne 3 ]]; then + echo "Usage: $0 " + exit 1 +fi + +echo "=> Starting MySQL Server" +/usr/bin/mysqld_safe > /dev/null 2>&1 & +PID=$! + +RET=1 +while [[ $RET -ne 0 ]]; do + echo "=> Waiting for confirmation of MySQL service startup" + sleep 5 + mysql -u"$1" -p"$2" -e "status" > /dev/null 2>&1 + RET=$? +done + +echo " Started with PID ${PID}" + +echo "=> Importing SQL file" +mysql -u"$1" -p"$2" < "$3" + +echo "=> Stopping MySQL Server" +mysqladmin -u"$1" -p"$2" shutdown + +echo "=> Done!" diff --git a/5.6/my.cnf b/5.6/my.cnf new file mode 100644 index 0000000..02fe8be --- /dev/null +++ b/5.6/my.cnf @@ -0,0 +1,6 @@ +[mysqld] +bind-address=0.0.0.0 +# http://www.percona.com/blog/2008/05/31/dns-achilles-heel-mysql-installation/ +skip_name_resolve +#server-id +#log-bin diff --git a/5.6/mysqld_charset.cnf b/5.6/mysqld_charset.cnf new file mode 100644 index 0000000..2c9c4f1 --- /dev/null +++ b/5.6/mysqld_charset.cnf @@ -0,0 +1,7 @@ +[mysqld] +character_set_server=utf8 +character_set_filesystem=utf8 +collation-server=utf8_general_ci +init-connect='SET NAMES utf8' +init_connect='SET collation_connection = utf8_general_ci' +skip-character-set-client-handshake \ No newline at end of file diff --git a/5.6/run.sh b/5.6/run.sh new file mode 100755 index 0000000..f529780 --- /dev/null +++ b/5.6/run.sh @@ -0,0 +1,190 @@ +#!/bin/bash + +set -m +set -e + +VOLUME_HOME="/var/lib/mysql" +CONF_FILE="/etc/mysql/conf.d/my.cnf" +LOG="/var/log/mysql/error.log" + +# Set permission of config file +chmod 644 ${CONF_FILE} +chmod 644 /etc/mysql/conf.d/mysqld_charset.cnf + +StartMySQL () +{ + /usr/bin/mysqld_safe ${EXTRA_OPTS} > /dev/null 2>&1 & + # Time out in 1 minute + LOOP_LIMIT=60 + for (( i=0 ; ; i++ )); do + if [ ${i} -eq ${LOOP_LIMIT} ]; then + echo "Time out. Error log is shown as below:" + tail -n 100 ${LOG} + exit 1 + fi + echo "=> Waiting for confirmation of MySQL service startup, trying ${i}/${LOOP_LIMIT} ..." + sleep 1 + mysql -uroot -e "status" > /dev/null 2>&1 && break + done +} + +CreateMySQLUser() +{ + if [ "$MYSQL_PASS" = "**Random**" ]; then + unset MYSQL_PASS + fi + + PASS=${MYSQL_PASS:-$(pwgen -s 12 1)} + _word=$( [ ${MYSQL_PASS} ] && echo "preset" || echo "random" ) + echo "=> Creating MySQL user ${MYSQL_USER} with ${_word} password" + + mysql -uroot -e "CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '$PASS'" + mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION" + echo "=> Done!" + echo "========================================================================" + echo "You can now connect to this MySQL Server using:" + echo "" + echo " mysql -u$MYSQL_USER -p$PASS -h -P" + echo "" + echo "Please remember to change the above password as soon as possible!" + echo "MySQL user 'root' has no password but only allows local connections" + echo "========================================================================" +} + +OnCreateDB() +{ + if [ "$ON_CREATE_DB" = "**False**" ]; then + unset ON_CREATE_DB + else + echo "Creating MySQL database ${ON_CREATE_DB}" + mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${ON_CREATE_DB};" + echo "Database created!" + fi +} + +ImportSql() +{ + for FILE in ${STARTUP_SQL}; do + echo "=> Importing SQL file ${FILE}" + if [ "$ON_CREATE_DB" ]; then + mysql -uroot "$ON_CREATE_DB" < "${FILE}" + else + mysql -uroot < "${FILE}" + fi + done +} + +# Main +if [ ${REPLICATION_MASTER} == "**False**" ]; then + unset REPLICATION_MASTER +fi + +if [ ${REPLICATION_SLAVE} == "**False**" ]; then + unset REPLICATION_SLAVE +fi + +# Initialize empty data volume and create MySQL user +if [[ ! -d $VOLUME_HOME/mysql ]]; then + echo "=> An empty or uninitialized MySQL volume is detected in $VOLUME_HOME" + echo "=> Installing MySQL ..." + if [ ! -f /usr/share/mysql/my-default.cnf ] ; then + cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf + fi + mysql_install_db || exit 1 + touch /var/lib/mysql/.EMPTY_DB + echo "=> Done!" +else + echo "=> Using an existing volume of MySQL" +fi + +# Set MySQL REPLICATION - MASTER +if [ -n "${REPLICATION_MASTER}" ]; then + echo "=> Configuring MySQL replication as master (1/2) ..." + if [ ! -f /replication_set.1 ]; then + RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})" + echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}" + sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE} + sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE} + touch /replication_set.1 + else + echo "=> MySQL replication master already configured, skip" + fi +fi + +# Set MySQL REPLICATION - SLAVE +if [ -n "${REPLICATION_SLAVE}" ]; then + echo "=> Configuring MySQL replication as slave (1/2) ..." + if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then + if [ ! -f /replication_set.1 ]; then + RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})" + echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}" + sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE} + sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE} + touch /replication_set.1 + else + echo "=> MySQL replication slave already configured, skip" + fi + else + echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'" + exit 1 + fi +fi + + +echo "=> Starting MySQL ..." +StartMySQL +tail -F $LOG & + +# Create admin user and pre create database +if [ -f /var/lib/mysql/.EMPTY_DB ]; then + echo "=> Creating admin user ..." + CreateMySQLUser + OnCreateDB + rm /var/lib/mysql/.EMPTY_DB +fi + + +# Import Startup SQL +if [ -n "${STARTUP_SQL}" ]; then + if [ ! -f /sql_imported ]; then + echo "=> Initializing DB with ${STARTUP_SQL}" + ImportSql + touch /sql_imported + fi +fi + +# Set MySQL REPLICATION - MASTER +if [ -n "${REPLICATION_MASTER}" ]; then + echo "=> Configuring MySQL replication as master (2/2) ..." + if [ ! -f /replication_set.2 ]; then + echo "=> Creating a log user ${REPLICATION_USER}:${REPLICATION_PASS}" + mysql -uroot -e "CREATE USER '${REPLICATION_USER}'@'%' IDENTIFIED BY '${REPLICATION_PASS}'" + mysql -uroot -e "GRANT REPLICATION SLAVE ON *.* TO '${REPLICATION_USER}'@'%'" + mysql -uroot -e "reset master" + echo "=> Done!" + touch /replication_set.2 + else + echo "=> MySQL replication master already configured, skip" + fi +fi + +# Set MySQL REPLICATION - SLAVE +if [ -n "${REPLICATION_SLAVE}" ]; then + echo "=> Configuring MySQL replication as slave (2/2) ..." + if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then + if [ ! -f /replication_set.2 ]; then + echo "=> Setting master connection info on slave" + mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT}, MASTER_CONNECT_RETRY=30" + mysql -uroot -e "start slave" + echo "=> Done!" + touch /replication_set.2 + else + echo "=> MySQL replication slave already configured, skip" + fi + else + echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'" + exit 1 + fi +fi + +fg diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 1279137..0000000 --- a/Dockerfile +++ /dev/null @@ -1,29 +0,0 @@ -FROM ubuntu:trusty -MAINTAINER Fernando Mayo , Feng Honglin - -# Install packages -RUN apt-get update -RUN DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server pwgen - -# Remove pre-installed database -RUN rm -rf /var/lib/mysql/* - -# Add MySQL configuration -ADD my.cnf /etc/mysql/conf.d/my.cnf -ADD mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf - -# Add MySQL scripts -ADD create_mysql_admin_user.sh /create_mysql_admin_user.sh -ADD import_sql.sh /import_sql.sh -ADD run.sh /run.sh -RUN chmod 755 /*.sh - -# Exposed ENV -ENV MYSQL_USER admin -ENV MYSQL_PASS **Random** - -# Add VOLUMEs to allow backup of config and databases -VOLUME ["/etc/mysql", "/var/lib/mysql"] - -EXPOSE 3306 -CMD ["/run.sh"] diff --git a/README.md b/README.md index 123a9d8..b12cb8b 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,17 @@ tutum-docker-mysql ================== +**This image will be deprecated soon. Please use the docker official image:** https://hub.docker.com/_/mysql/ + +[![Deploy to Tutum](https://s.tutum.co/deploy-to-tutum.svg)](https://dashboard.tutum.co/stack/deploy/) + Base docker image to run a MySQL database server MySQL version ------------- -`master` branch maintains MySQL from Ubuntu trusty official source. If you want to get different version of MySQL, please checkout `5.5` branch and `5.6` branch. - -If you want to use MariaDB, please check our `tutum/mariadb` image: https://github.com/tutumcloud/tutum-docker-mariadb +Different versions are built from different folders. If you want to use MariaDB, please check our `tutum/mariadb` image: https://github.com/tutumcloud/tutum-docker-mariadb Usage @@ -17,39 +19,46 @@ Usage To create the image `tutum/mysql`, execute the following command on the tutum-mysql folder: - docker build -t tutum/mysql . + docker build -t tutum/mysql 5.5/ To run the image and bind to port 3306: - docker run -d -p 3306:3306 tutum/mysql + docker run -d -p 3306:3306 tutum/mysql -The first time that you run your container, a new user `admin` with all privileges +The first time that you run your container, a new user `admin` with all privileges will be created in MySQL with a random password. To get the password, check the logs of the container by running: - docker logs + docker logs You will see an output like the following: - ======================================================================== - You can now connect to this MySQL Server using: + ======================================================================== + You can now connect to this MySQL Server using: - mysql -uadmin -p47nnf4FweaKu -h -P + mysql -uadmin -p47nnf4FweaKu -h -P - Please remember to change the above password as soon as possible! - MySQL user 'root' has no password but only allows local connections - ======================================================================== + Please remember to change the above password as soon as possible! + MySQL user 'root' has no password but only allows local connections. + ======================================================================== In this case, `47nnf4FweaKu` is the password allocated to the `admin` user. -Remember that the `root` user has no password but it's only accesible from within the container. +Remember that the `root` user has no password, but it's only accessible from within the container. You can now test your deployment: - mysql -uadmin -p + mysql -uadmin -p Done! +Passing extra configuration to start mysql server +------------------------------------------------ + +To pass additional settings to `mysqld`, you can use environment variable `EXTRA_OPTS`. +For example, to run mysql using lower case table name, you can do: + + docker run -d -p 3306:3306 -e EXTRA_OPTS="--lower_case_table_names=1" tutum/mysql Setting a specific password for the admin account ------------------------------------------------- @@ -57,30 +66,43 @@ Setting a specific password for the admin account If you want to use a preset password instead of a random generated one, you can set the environment variable `MYSQL_PASS` to your specific password when running the container: - docker run -d -p 3306:3306 -e MYSQL_PASS="mypass" tutum/mysql + docker run -d -p 3306:3306 -e MYSQL_PASS="mypass" tutum/mysql You can now test your deployment: - mysql -uadmin -p"mypass" + mysql -uadmin -p"mypass" + +The admin username can also be set via the `MYSQL_USER` environment variable. + + + +Creating a database on container creation +------------------------------------------------- -The admin username can also be set via the MYSQL_USER environment variable. +If you want a database to be created inside the container when you start it up +for the first time you can set the environment variable `ON_CREATE_DB` to a string +that names the database. + docker run -d -p 3306:3306 -e ON_CREATE_DB="newdatabase" tutum/mysql + +If this is combined with importing SQL files, those files will be imported into the +created database. Mounting the database file volume --------------------------------- -In order to persist the database data, you can mount a local folder from the host +In order to persist the database data, you can mount a local folder from the host on the container to store the database files. To do so: - docker run -d -v /path/in/host:/var/lib/mysql tutum/mysql /bin/bash -c "/usr/bin/mysql_install_db" + docker run -d -v /path/in/host:/var/lib/mysql tutum/mysql /bin/bash -c "/usr/bin/mysql_install_db" This will mount the local folder `/path/in/host` inside the docker in `/var/lib/mysql` (where MySQL will store the database files by default). `mysql_install_db` creates the initial database structure. Remember that this will mean that your host must have `/path/in/host` available when you run your docker image! -After this you can start your mysql image but this time using `/path/in/host` as the database folder: +After this you can start your MySQL image, but this time using `/path/in/host` as the database folder: - docker run -d -p 3306:3306 -v /path/in/host:/var/lib/mysql tutum/mysql + docker run -d -p 3306:3306 -v /path/in/host:/var/lib/mysql tutum/mysql Mounting the database file volume from other containers @@ -89,14 +111,14 @@ Mounting the database file volume from other containers Another way to persist the database data is to store database files in another container. To do so, first create a container that holds database files: - docker run -d -v /var/lib/mysql --name db_vol -p 22:22 tutum/ubuntu-trusty + docker run -d -v /var/lib/mysql --name db_vol -p 22:22 tutum/ubuntu-trusty -This will create a new ssh-enabled container and use its folder `/var/lib/mysql` to store MySQL database files. +This will create a new ssh-enabled container and use its folder `/var/lib/mysql` to store MySQL database files. You can specify any name of the container by using `--name` option, which will be used in next step. After this you can start your MySQL image using volumes in the container created above (put the name of container in `--volumes-from`) - docker run -d --volumes-from db_vol -p 3306:3306 tutum/mysql + docker run -d --volumes-from db_vol -p 3306:3306 tutum/mysql Migrating an existing MySQL Server @@ -106,26 +128,48 @@ In order to migrate your current MySQL server, perform the following commands fr To dump your databases structure: - mysqldump -u -p --opt -d -B > /tmp/dbserver_schema.sql + mysqldump -u -p --opt -d -B > /tmp/dbserver_schema.sql To dump your database data: - mysqldump -u -p --quick --single-transaction -t -n -B > /tmp/dbserver_data.sql + mysqldump -u -p --quick --single-transaction -t -n -B > /tmp/dbserver_data.sql To import a SQL backup which is stored for example in the folder `/tmp` in the host, run the following: - sudo docker run -d -v /tmp:/tmp tutum/mysql /bin/bash -c "/import_sql.sh /tmp/") + sudo docker run -d -v /tmp:/tmp tutum/mysql /bin/bash -c "/import_sql.sh /tmp/" + +Also, you can start the new database initializing it with the SQL file: + + sudo docker run -d -v /path/in/host:/var/lib/mysql -e STARTUP_SQL="/tmp/" tutum/mysql + +Where `` and `` are the database username and password set earlier and `` is the name of the SQL file to be imported. + -Where `` is the root password set earlier and `` is the name of the SQL file to be imported. +Replication - Master/Slave +------------------------- +To use MySQL replication, please set environment variable `REPLICATION_MASTER`/`REPLICATION_SLAVE` to `true`. Also, on master side, you may want to specify `REPLICATION_USER` and `REPLICATION_PASS` for the account to perform replication, the default value is `replica:replica` +Examples: +- Master MySQL +- + docker run -d -e REPLICATION_MASTER=true -e REPLICATION_PASS=mypass -p 3306:3306 --name mysql tutum/mysql + +- Example on Slave MySQL: +- + docker run -d -e REPLICATION_SLAVE=true -p 3307:3306 --link mysql:mysql tutum/mysql + +Now you can access port `3306` and `3307` for the master/slave MySQL. Environment variables --------------------- -`MYSQL_USER`: Set a specific username for the admin account (default 'admin') +`MYSQL_USER`: Set a specific username for the admin account (default 'admin'). + `MYSQL_PASS`: Set a specific password for the admin account. -Compatibliity Issues +`STARTUP_SQL`: Defines one or more SQL scripts separated by spaces to initialize the database. Note that the scripts must be inside the container, so you may need to mount them. + +Compatibility Issues -------------------- -- Volume created by MySQL 5.6 cannot be used in MySQL 5.5 Images or MariaDB images +- Volume created by MySQL 5.6 cannot be used in MySQL 5.5 Images or MariaDB images. diff --git a/create_db.sh b/create_db.sh deleted file mode 100755 index 26fdfd7..0000000 --- a/create_db.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -if [[ $# -eq 0 ]]; then - echo "Usage: $0 " - exit 1 -fi - -/usr/bin/mysqld_safe > /dev/null 2>&1 & - -echo "=> Creating database $1" -RET=1 -while [[ RET -ne 0 ]]; do - sleep 5 - mysql -uroot -e "CREATE DATABASE $1" - RET=$? -done - -mysqladmin -uroot shutdown - -echo "=> Done!" diff --git a/create_mysql_admin_user.sh b/create_mysql_admin_user.sh deleted file mode 100644 index 666ed68..0000000 --- a/create_mysql_admin_user.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -/usr/bin/mysqld_safe > /dev/null 2>&1 & - -RET=1 -while [[ RET -ne 0 ]]; do - echo "=> Waiting for confirmation of MySQL service startup" - sleep 5 - mysql -uroot -e "status" > /dev/null 2>&1 - RET=$? -done - -if [ "$MYSQL_PASS" = "**Random**" ]; then - unset MYSQL_PASS -fi - -PASS=${MYSQL_PASS:-$(pwgen -s 12 1)} -_word=$( [ ${MYSQL_PASS} ] && echo "preset" || echo "random" ) -echo "=> Creating MySQL user ${MYSQL_USER} with ${_word} password" - -mysql -uroot -e "CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '$PASS'" -mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION" - - -echo "=> Done!" - -echo "========================================================================" -echo "You can now connect to this MySQL Server using:" -echo "" -echo " mysql -u$MYSQL_USER -p$PASS -h -P" -echo "" -echo "Please remember to change the above password as soon as possible!" -echo "MySQL user 'root' has no password but only allows local connections" -echo "========================================================================" - -mysqladmin -uroot shutdown diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..bd56e19 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,4 @@ +db: + image: tutum/mysql:latest + environment: + MYSQL_PASS: "**ChangeMe**" diff --git a/import_sql.sh b/import_sql.sh deleted file mode 100755 index f8a2ac6..0000000 --- a/import_sql.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -if [[ $# -ne 2 ]]; then - echo "Usage: $0 " - exit 1 -fi - -echo "=> Starting MySQL Server" -/usr/bin/mysqld_safe > /dev/null 2>&1 & -sleep 5 -echo " Started with PID $!" - -echo "=> Importing SQL file" -mysql -uadmin -p"$1" < "$2" - -echo "=> Stopping MySQL Server" -mysqladmin -uadmin -p"$1" shutdown - -echo "=> Done!" diff --git a/my.cnf b/my.cnf deleted file mode 100644 index 84e7bbf..0000000 --- a/my.cnf +++ /dev/null @@ -1,2 +0,0 @@ -[mysqld] -bind-address=0.0.0.0 diff --git a/run.sh b/run.sh deleted file mode 100644 index fc223ab..0000000 --- a/run.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -VOLUME_HOME="/var/lib/mysql" - -if [[ ! -d $VOLUME_HOME/mysql ]]; then - echo "=> An empty or uninitialized MySQL volume is detected in $VOLUME_HOME" - echo "=> Installing MySQL ..." - mysql_install_db > /dev/null 2>&1 - echo "=> Done!" - /create_mysql_admin_user.sh -else - echo "=> Using an existing volume of MySQL" -fi - -exec mysqld_safe diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..8426141 --- /dev/null +++ b/test.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +set -e + +echo "=> Building mysql 5.5 image" +docker build -t mysql-5.5 5.5/ + +echo "=> Testing if mysql is running on 5.5" +docker run -d -p 13306:3306 -e MYSQL_USER="user" -e MYSQL_PASS="test" mysql-5.5; sleep 10 +mysqladmin -uuser -ptest -h127.0.0.1 -P13306 ping | grep -c "mysqld is alive" + +echo "=> Testing replication on mysql 5.5" +docker run -d -e MYSQL_USER=user -e MYSQL_PASS=test -e REPLICATION_MASTER=true -e REPLICATION_USER=repl -e REPLICATION_PASS=repl -p 13307:3306 --name mysql55master mysql-5.5; sleep 10 +docker run -d -e MYSQL_USER=user -e MYSQL_PASS=test -e REPLICATION_SLAVE=true -p 13308:3306 --link mysql55master:mysql mysql-5.5; sleep 10 +docker logs mysql55master | grep "repl:repl" +mysql -uuser -ptest -h127.0.0.1 -P13307 -e "show master status\G;" | grep "mysql-bin.*" +mysql -uuser -ptest -h127.0.0.1 -P13308 -e "show slave status\G;" | grep "Slave_IO_Running.*Yes" +mysql -uuser -ptest -h127.0.0.1 -P13308 -e "show slave status\G;" | grep "Slave_SQL_Running.*Yes" + +echo "=> Testing volume on mysql 5.5" +mkdir vol55 +docker run --name mysql55.1 -d -p 13309:3306 -e MYSQL_USER="user" -e MYSQL_PASS="test" -v "$(pwd)/vol55":/var/lib/mysql mysql-5.5; sleep 10 +mysqladmin -uuser -ptest -h127.0.0.1 -P13309 ping | grep -c "mysqld is alive" +docker stop mysql55.1 +docker run -d -p 13310:3306 -v "$(pwd)/vol55":/var/lib/mysql mysql-5.5; sleep 10 +mysqladmin -uuser -ptest -h127.0.0.1 -P13310 ping | grep -c "mysqld is alive" + +echo "=> Building mysql 5.6 image" +docker build -t mysql-5.6 5.6/ + +echo "=> Testing if mysql is running on 5.6" +docker run -d -p 23306:3306 -e MYSQL_USER="user" -e MYSQL_PASS="test" mysql-5.6; sleep 10 +mysqladmin -uuser -ptest -h127.0.0.1 -P13307 ping | grep -c "mysqld is alive" + +echo "=> Testing replication on mysql 5.6" +docker run -d -e MYSQL_USER=user -e MYSQL_PASS=test -e REPLICATION_MASTER=true -e REPLICATION_USER=repl -e REPLICATION_PASS=repl -p 23307:3306 --name mysql56master mysql-5.6; sleep 10 +docker run -d -e MYSQL_USER=user -e MYSQL_PASS=test -e REPLICATION_SLAVE=true -p 23308:3306 --link mysql56master:mysql mysql-5.6; sleep 10 +docker logs mysql56master | grep "repl:repl" +mysql -uuser -ptest -h127.0.0.1 -P23307 -e "show master status\G;" | grep "mysql-bin.*" +mysql -uuser -ptest -h127.0.0.1 -P23308 -e "show slave status\G;" | grep "Slave_IO_Running.*Yes" +mysql -uuser -ptest -h127.0.0.1 -P23308 -e "show slave status\G;" | grep "Slave_SQL_Running.*Yes" + +echo "=> Testing volume on mysql 5.6" +mkdir vol56 +docker run --name mysql56.1 -d -p 23309:3306 -e MYSQL_USER="user" -e MYSQL_PASS="test" -v "$(pwd)/vol56":/var/lib/mysql mysql-5.6; sleep 10 +mysqladmin -uuser -ptest -h127.0.0.1 -P23309 ping | grep -c "mysqld is alive" +docker stop mysql56.1 +docker run -d -p 23310:3306 -v "$(pwd)/vol56":/var/lib/mysql mysql-5.6; sleep 10 +mysqladmin -uuser -ptest -h127.0.0.1 -P23310 ping | grep -c "mysqld is alive" + +echo "=>Done"