如何使用docker-compose更改mysql的默认字符集?

时间:2017-08-17 07:45:47

标签: mysql docker docker-compose

当我保存中文字符串时,mysql会出现错误"异常值:
(1366,"字符串值不正确:' \ xE5 \ xB0 \ x8F \ xE6 \ x98 \ x8E'列'名称'第1行和第34行;)&#34 ;,我检查了mysql的字符,它显示了这个:

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

而我的docker-compose.yml就像其他人一样:

web:
    image: yetongxue/docker_test:1.2
    links:
      - "db"
    ports:
      - "8100:8000"
    volumes:
      - "/Users/yetongxue/docker_v/docker_test/media:/root/media"
    restart: always

db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: qwerasdf
      MYSQL_DATABASE: docker_db
    restart: always
    volumes:
      - "/Users/yetongxue/docker_v/docker_test/db:/var/lib/mysql"

我知道如何使用my.cnf设置mysql的字符,但是如何在docker-compose.yml中执行此操作? 谁知道这个?谢谢!

5 个答案:

答案 0 :(得分:12)

您可以在修改my.cnf的位置构建自己的mysql图像,也可以使用--character-set-server=utf8mb4--collation-server=utf8_unicode_ci修改启动mysql后台程序的命令。

web:
    image: yetongxue/docker_test:1.2
    links:
      - "db"
    ports:
      - "8100:8000"
    volumes:
      - "/Users/yetongxue/docker_v/docker_test/media:/root/media"
    restart: always

db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: qwerasdf
      MYSQL_DATABASE: docker_db
    restart: always
    volumes:
      - "/Users/yetongxue/docker_v/docker_test/db:/var/lib/mysql"
    command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']

我建议使用utf8mb4,因为它可以存储每个多字节字符" 4字节" (https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html

答案 1 :(得分:2)

我在@Ziemowit Stolarczyk 列出的情况下阅读了 GitHub 问题。所有这些都不完全正确。 GitHub 的答案是 -e LANG=C.UTF-8。我在我的 docker-compose.yml 中使用它,但是 mysql 打印出来是这样的:

mysql>  show variables like 'char%';
+--------------------------+----------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

它不适用于数据库和服务器。所以我在 docker-compose.yml 中添加了如下配置。

command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

整个yaml文件是:

version: "3"
services:
  mysql:
    container_name: mysql
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      LANG: C.UTF-8
    volumes:
      - ./mysql:/var/lib/mysql
    ports:
      - "3306:3306"
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

这是最终的输出。

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

参考

答案 2 :(得分:0)

撰写文件用于运行容器(并可以构建图像)。您无法自定义docker-compose文件中的图像。您需要创建一个扩展 mysql 图像的自定义图像。 创建一个Dockerfile:

FROM mysql:5.7
RUN <command to update my.cnf>

然后建立这个图像:

docker build -t <image-name> .

更新撰写文件:

db:
    image: <image-name-specified-above>

答案 3 :(得分:0)

如果您想使用上述设置创建自己的docker映像,而不是在docker-compose中指定它们,可以这样做:

FROM mysql:5.5.60

RUN ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime

VOLUME ["/docker-entrypoint-initdb.d"]

CMD ["--character-set-server=utf8mb4", "--collation-server=utf8mb4_general_ci", "--skip-character-set-client-handshake"]

答案 4 :(得分:0)

@Javier Arias 的回答很棒,但仍然有一些非 utf8 设置留在我的网站上,如下所示。

mysql> show variables like "%char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

它导致了一些与 docker-compose exec mysql bash 后显示的 utf8 字符和进行查询相关的问题。

添加一项环境设置后:LANG: C.UTF_8 我得到:

mysql> show variables like "%char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

所以我认为最终的 docker-compose.yml 应该是:

db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: qwerasdf
      MYSQL_DATABASE: docker_db
      LANG: C.UTF_8  # this one should be added
    restart: always
    volumes:
      - "/Users/yetongxue/docker_v/docker_test/db:/var/lib/mysql"
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

您可以在此处阅读与此相关的整个主题https://github.com/docker-library/mysql/issues/131