问题: 用docker 容器启动mysql 导入数据, 字符集是乱码
查看MYSQL数据库服务器和数据库字符集
mysql> show variables like '%character%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb3 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8mb3 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.02 sec)
# 需求是把 下面的四个字段 都显示为 utf8mb3 才可以导入数据
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb3 | |
| character_set_results | latin1 |
退出mysql
mysql> exit
Bye
root@f2fe58b9f195:/#
设置mysql 容器 客户端字符集
root@f2fe58b9f195:/# cat > /etc/mysql/conf.d/mysql.cnf <<EOF
[mysqld]
symbolic-links=0
max_connections=3000
max_user_connections=500
wait_timeout=200
character-set-server=utf8
collation-server=utf8_general_ci
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
EOF
退出mysql 容器
root@f2fe58b9f195:/# exit
exit
[root@node1 ~]#
重新进入mysql 容器
docker exec -it mysql-svc-container bash
再次查看字符集
#连接远程 mysql
root@f2fe58b9f195:/# mysql -u root -p'Passw0rd' -h 101.42.101.140 -P 3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
# 查看字符集
mysql> show variables like '%character%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8mb3 |
| character_set_connection | utf8mb3 |
| character_set_database | utf8mb3 |
| character_set_filesystem | binary |
| character_set_results | utf8mb3 |
| character_set_server | utf8mb3 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.01 sec)
mysql>
再次执行导入数据,数据就是正常的了
#复制宿主机 初始化数据到容器
#将 sql 文件拷贝到 mysql 客户端容器/tmp 目录下
docker cp /data/init_sql/init.sql mysql-svc-container:/tmp/init.sql
#导入命令
mysql -u 远程数据库用户名 -p'远程数据库密码' -h 远程数据库主机 -P 远程数据库端口 远程数据库名称 < /tmp/init.sql
释义:
小p 指定的是 -p'数据库密码'
大P 指定的是 -P 远程数据库端口