docker mysql image encoding change to utf8

最近在使用docker的mysql image掛載mysql db時候
發現server的語系都是在latin1的狀態
而mysql image又不允許直接把my.cnf換掉...
但是可以overwrite /etc/mysql/conf.d這個資料匣
來重寫mysqld的語系設定...

這是原始資料庫啟動後透過"\s"來查看server狀態

# mysql -h123.123.123.123 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.42 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.5.41, for debian-linux-gnu (x86_64) using readline 6.2
Connection id: 3
Current database:
Current user: root@123.123.123.123
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.42 MySQL Community Server (GPL)
Protocol version: 10
Connection: 211.78.245.67 via TCP/IP
Server characterset: latin1
Db     characterset: latin1
Client characterset: latin1
Conn.  characterset: latin1
TCP port: 3306
Uptime: 49 sec
Threads: 3  Questions: 25  Slow queries: 0  Opens: 33  Flush tables: 1  Open tables: 26  Queries per second avg: 0.510
--------------
mysql>

我們建立一個docker.cnf檔案如下

# cat docker.cnf
[mysqld]
user = mysql
datadir = /var/lib/mysql
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

我的啟動command:

docker run --name mydb -p 3306:3306 -v /root/mysql-data:/var/lib/mysql \ 
  -v /root/mysql-config:/etc/mysql/conf.d \ 
  -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.5

接著應該就可以在登入mysql後透過"\s"看到語系部分已經是utf8

Server characterset: utf8
Db     characterset: utf8
Client characterset: utf8
Conn.  characterset: utf8

PS: 關於client部分,需要在[client]部分使用,也就是連線的那端要改my.cnf來使用utf8連線...