Skip to content
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.

Custom character set (and set default to utf8mb4) #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion 5.5/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ MAINTAINER Fernando Mayo <[email protected]>, Feng Honglin <[email protected]>

# Add MySQL configuration
ADD my.cnf /etc/mysql/conf.d/my.cnf
ADD mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf

RUN apt-get update && \
apt-get -yq install mysql-server-5.5 pwgen && \
Expand All @@ -13,12 +12,17 @@ RUN apt-get update && \
mysql_install_db > /dev/null 2>&1 && \
touch /var/lib/mysql/.EMPTY_DB

# Add MySQL charset configuration
ADD mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf

# Add MySQL scripts
ADD import_sql.sh /import_sql.sh
ADD run.sh /run.sh

ENV MYSQL_USER=admin \
MYSQL_PASS=**Random** \
MYSQL_CHARACTER_SET=utf8mb4 \
MYSQL_COLLATION=utf8mb4_unicode_ci \
ON_CREATE_DB=**False** \
REPLICATION_MASTER=**False** \
REPLICATION_SLAVE=**False** \
Expand Down
9 changes: 4 additions & 5 deletions 5.5/mysqld_charset.cnf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[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'
character_set_server=MYSQL_CHARACTER_SET
collation-server=MYSQL_COLLATION
init-connect='SET NAMES MYSQL_CHARACTER_SET'
init_connect='SET collation_connection = MYSQL_COLLATION'
skip-character-set-client-handshake
10 changes: 9 additions & 1 deletion 5.5/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ set -e
VOLUME_HOME="/var/lib/mysql"
CONF_FILE="/etc/mysql/conf.d/my.cnf"
LOG="/var/log/mysql/error.log"
CHARSET_CONF_FILE="/etc/mysql/conf.d/mysqld_charset.cnf"

# Set permission of config file
chmod 644 ${CONF_FILE}
chmod 644 /etc/mysql/conf.d/mysqld_charset.cnf

# Update charset file and set permission
sed -i "s/MYSQL_CHARACTER_SET/${MYSQL_CHARACTER_SET}/" ${CHARSET_CONF_FILE}
sed -i "s/MYSQL_COLLATION/${MYSQL_COLLATION}/" ${CHARSET_CONF_FILE}
chmod 644 ${CHARSET_CONF_FILE}

StartMySQL ()
{
Expand All @@ -26,6 +31,9 @@ StartMySQL ()
sleep 1
mysql -uroot -e "status" > /dev/null 2>&1 && break
done

# Remove non-exist plugin
mysql -uroot -e "DELETE FROM mysql.plugin WHERE name IN ('innodb', 'federated', 'blackhole', 'archive');"
}

CreateMySQLUser()
Expand Down
6 changes: 5 additions & 1 deletion 5.6/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ MAINTAINER Fernando Mayo <[email protected]>, Feng Honglin <[email protected]>

# Add MySQL configuration
ADD my.cnf /etc/mysql/conf.d/my.cnf
ADD mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf

RUN apt-get update && \
apt-get -yq install mysql-server-5.6 pwgen && \
Expand All @@ -13,12 +12,17 @@ RUN apt-get update && \
mysql_install_db > /dev/null 2>&1 && \
touch /var/lib/mysql/.EMPTY_DB

# Add MySQL charset configuration
ADD mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf

# Add MySQL scripts
ADD import_sql.sh /import_sql.sh
ADD run.sh /run.sh

ENV MYSQL_USER=admin \
MYSQL_PASS=**Random** \
MYSQL_CHARACTER_SET=utf8mb4 \
MYSQL_COLLATION=utf8mb4_unicode_ci \
ON_CREATE_DB=**False** \
REPLICATION_MASTER=**False** \
REPLICATION_SLAVE=**False** \
Expand Down
9 changes: 4 additions & 5 deletions 5.6/mysqld_charset.cnf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[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'
character_set_server=MYSQL_CHARACTER_SET
collation-server=MYSQL_COLLATION
init-connect='SET NAMES MYSQL_CHARACTER_SET'
init_connect='SET collation_connection = MYSQL_COLLATION'
skip-character-set-client-handshake
10 changes: 9 additions & 1 deletion 5.6/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ set -e
VOLUME_HOME="/var/lib/mysql"
CONF_FILE="/etc/mysql/conf.d/my.cnf"
LOG="/var/log/mysql/error.log"
CHARSET_CONF_FILE="/etc/mysql/conf.d/mysqld_charset.cnf"

# Set permission of config file
chmod 644 ${CONF_FILE}
chmod 644 /etc/mysql/conf.d/mysqld_charset.cnf

# Update charset file and set permission
sed -i "s/MYSQL_CHARACTER_SET/${MYSQL_CHARACTER_SET}/" ${CHARSET_CONF_FILE}
sed -i "s/MYSQL_COLLATION/${MYSQL_COLLATION}/" ${CHARSET_CONF_FILE}
chmod 644 ${CHARSET_CONF_FILE}

StartMySQL ()
{
Expand All @@ -26,6 +31,9 @@ StartMySQL ()
sleep 1
mysql -uroot -e "status" > /dev/null 2>&1 && break
done

# Remove non-exist plugin
mysql -uroot -e "DELETE FROM mysql.plugin WHERE name IN ('innodb', 'federated', 'blackhole', 'archive');"
}

CreateMySQLUser()
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,31 @@ Examples:

Now you can access port `3306` and `3307` for the master/slave MySQL.


Character set and collation
---------------------------
To change MySQL character set and collation, please set environment variable `MYSQL_CHARACTER_SET` and `MYSQL_COLLATION` to any supported value by MySQL server. By default, `utf8mb4` and `utf8mb4_unicode_ci` are used in order to support 4-bytes unicode character (including emoji, astral symbol, etc.).

To ensure to use `utf8mb4` and `utf8mb4_general_ci`:

docker run -d -e MYSQL_CHARACTER_SET=utf8mb4 -e MYSQL_COLLATION=utf8mb4_general_ci -p 3306:3306 --name mysql tutum/mysql

To use `utf8` and `utf8_general_ci`:

docker run -d -e MYSQL_CHARACTER_SET=utf8 -e MYSQL_COLLATION=utf8_general_ci -p 3306:3306 --name mysql tutum/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.

`MYSQL_CHARACTER_SET`: Set a specific character encoding set (default `utf8mb4`)

`MYSQL_COLLATION`: Set a specific collation (default `utf8mb4_unicode_ci`)

`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
Expand Down