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

fiei3ece  于 2023-02-11  发布在  Mysql
关注(0)|答案(9)|浏览(308)

当我保存的字符串的中文,mysql上升的错误"异常值:
(1366,"不正确的字符串值:'\xE5\xB0\x8F\xE6\x98\x8E'为列'名称'在第1行")",我检查了字符的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"

我知道如何用我的. cnf来设置mysql的字符,但是我怎样才能在docker-compose.yml中做到呢?有人知道吗?谢谢!

dxxyhpgq

dxxyhpgq1#

您可以在修改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

ifmq2ha2

ifmq2ha22#

我读到的GitHub问题是@Ziemowit Stolarczyk列出的,它们并不完全正确,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)

参考

xu3bshqb

xu3bshqb3#

如果你想用上面的设置创建你自己的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"]
py49o6xq

py49o6xq4#

@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

z3yyvxxp

z3yyvxxp5#

合成文件用于运行容器(并且可以构建映像)。您不能自定义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>
nhjlsmyf

nhjlsmyf6#

尝试使用字符集服务器= utf8

docker run --name mysql-server -t -e MYSQL_DATABASE="docker_db" -e MYSQL_USER="username" -e MYSQL_PASSWORD="qwerasdf" -e MYSQL_ROOT_PASSWORD="YjA0OTYwZDBiN2EwNWFjMTRjZGU3Yjcy" -d mysql --character-set-server=utf8 --collation-server=utf8_bin --default-authentication-plugin=mysql_native_password
yc0p9oo0

yc0p9oo07#

在我的例子中,问题是我的SQL-dump文件在导出时没有使用UTF-8编码,我不得不手动修复转储文件以获得正确的字符。

j0pj023g

j0pj023g8#

我试图通过在docker-compose.yml中设置命令来解决这个问题

command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']

在许多变体中,但由于某种神奇的原因,它对我不起作用。我的表排序规则和连接排序规则仍然是latin1_swedish_ci,尽管collation_server是utf8_unicode_ci
我通过直接将此选项设置为mysql配置文件(my.cnf)并将my.cnf设置为卷来解决这个问题。
my.cnf:

[mysqld]
init_connect=‘SET collation_connection = utf8_unicode_ci’
character-set-server = utf8
collation-server = utf8_unicode_ci

[client]
default-character-set = utf8

docker-compose.yml:

version: '3.1'

services:

  db:
    image: mariadb
    restart: always
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=example
      - MYSQL_DATABASE=test
      - MYSQL_USER=testuser
      - MYSQL_PASSWORD=testpassword
    volumes:
      - ./my.cnf:/etc/mysql/my.cnf

p.s. my.cnf在本例中位于docker-compose.yml附近。您可以将其放在另一个文件夹中,但应在卷部分更改路径

bgtovc5b

bgtovc5b9#

大部分的答案是好的,但可能是最好的码头组成与正确的字符集和伟大的结构,我发现是在这里。

version: '2'
services:
  mysql:
    image: mysql:5.7
    ports:
      - '3306:3306'
    volumes:
      - ./sql-scripts/:/docker-entrypoint-initdb.d/
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --skip-character-set-client-handshake
    environment:
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_DATABASE: test

结果是:

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

此外,sql-scipts目录中的SQL(和.sh)文件将在启动期间执行(例如,在第一次启动容器时导入现有转储)

相关问题