默認支持utf8編碼的mysql docker鏡像

為促進社區發展,運維派尋求戰略合作、贊助、投資,請聯系微信:helloywp

其他人都是不正確的

使用mysql容器鏡像可以很快速的運行mysql,免去了傳統的虛擬機安裝方式的繁瑣配置。但是使用官方的mysql鏡像,你會遇到中文亂碼的問題,原因是官方鏡像的字符集默認值不是utf8。這時候你去google,會找到一些文章,如這個哥們的,但是你按照他的做法,還是會亂碼,這時候你進入mysql查看字符集,發現是這樣的:

show variables like "%character%";

+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | latin1                           |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | latin1                           |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysqlarsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

這里才是正確的

好吧,我費了不少力氣,才找到正確方法。

首先查看官方鏡像說明,有關于utf8設置的參數:

Many configuration options can be passed as flags to mysqld. This will give you the flexibility to customize the container without needing a cnf file. For example, if you want to change the default encoding and collation for all tables to use UTF-8 (utf8mb4) just run the following:
    $ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

如果只是設置這兩個參數是不夠的,你會得到這樣的配置:

mysql> show variables like "%character%";
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | latin1                           |
| character_set_connection | latin1                           |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | latin1                           |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

你會發現上面那個哥們的與官方的這個指導取并集,就是正確的結果了,所以方法就很簡單了。查看dockerfile正確的配置:

mysql> show variables like "%character%";
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.01 sec)

網友評論comments

發表評論

電子郵件地址不會被公開。 必填項已用*標注

暫無評論

Copyright ? 2012-2019 YUNWEIPAI.COM - 運維派 - 粵ICP備14090526號-3
掃二維碼
掃二維碼
返回頂部
街机电玩捕鱼抢红包 河南3D走势图表 扑克推锅怎么赢 秒速时时有官方的吗 七乐彩基本走势图连线带座标 广东南粤福彩开奖结果 湖北快三跨度走势图 黄大仙救世报期期准 广东11选5盈利计划在线 20选5中奖金额计算 四川福彩快乐12开奖结果图