一、起因和驗(yàn)證
由于在項(xiàng)目中使用了Docker的mysql容器,需要到docker里面去做增量的sql更新。結(jié)果發(fā)現(xiàn)更新后的中文都丟失了。懷疑是docker容器內(nèi)承載的系統(tǒng)不支持中文.
所以,通過宿主機(jī)進(jìn)入docker的bash環(huán)境后,連接mysql,執(zhí)行sql語句。再次驗(yàn)證;
現(xiàn)象如圖所示:
+-----+-----------+----------+--------+------------+
| id | name | value | dic_id | is_default |
+-----+-----------+----------+--------+------------+
| 1 | ??? | 1 | 1 | 1 |
| 2 | ??? | 2 | 1 | 1 |
| 3 | ??? | 3 | 1 | 1 |
| 4 | ?? | 1 | 2 | 1 |
進(jìn)入Docker內(nèi)部,執(zhí)行sql語句現(xiàn)象:
[root@vm172-31-32-3 ~]# docker exec -it mysql2 bash
root@9929c772da63:/# mysql -u root -p
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
##下一行是原文:
mysql> insert into user(id,username) values('1','張三');
##變成了:
mysql> insert into user(id,username) values('1','');
二、查找原因
2.1 查看系統(tǒng)及系統(tǒng)所支持字符集
A、使用 cat /etc/issue 命令查看承載系統(tǒng)
root@9929c772da63:/# cat /etc/issue
Debian GNU/Linux 9 \n \l
B、使用locale查看linux正在使用的編碼方式
root@9929c772da63:/# locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
C、查看Linux支持的編碼方式
root@9929c772da63:/# locale -a
C
C.UTF-8
POSIX
root@9929c772da63:/#
三、原因及解決方案
3.1 原因及無法生效的解決方案
首先,目前網(wǎng)上搜了下,出現(xiàn)這個(gè)問題的以Debian系統(tǒng)居多,網(wǎng)上有的方法是直接在命令行下輸入命令,然后重新加載系統(tǒng)變量,不過筆者發(fā)現(xiàn)并未改變系統(tǒng)編碼;
##注:此方案筆者測試未生效
root@9929c772da63:/# LANG=C.UTF-8 (有的是zh_CN.UTF-8,不過我在本地沒發(fā)現(xiàn)這種編碼)
root@9929c772da63:/# source /etc/profile
3.2 解決方案一
此方案參考了peakhell的文章 《解決docker中的容器無法使用中文的問題》
http://www.itdecent.cn/p/6fe582ba6d3d
使用vim編輯 /etc/profile 將“export LANG="C.UTF-8”命令添加在profile最后
但是Debian內(nèi)沒有vim以及vi工具可以有2種方法
注意:這2方法都是臨時(shí)改變編碼,一旦從容器中退出,然后再進(jìn)去,編碼集還原,需要再次使用 source /etc/profile再次刷新才可
3.2.1將文件從docker容器內(nèi)拷貝出,然后再宿主機(jī)上修正后,再拷貝回去
#將容器內(nèi)/etc/profile文件拷貝到宿主機(jī)/app/backup目錄下
[root@vm8-80-8-8 sql]# docker cp mysql2:/etc/profile /app/backup
[root@vm8-80-8-8 sql]# vim /app/backup/profile
#追加命令-在最后一行,然后保存
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
....忽略N行
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
export LANG="C.UTF-8"
最后將文件拷貝回容器內(nèi),刷新系統(tǒng)變量
[root@vm8-80-8-8 sql]# docker cp /app/backup/profile mysql2:/etc/profile
[root@vm8-80-8-8 sql]# docker exec -it mysql2 bash
root@9929c772da63:/# source /etc/profile
檢查結(jié)果
root@9929c772da63:/# locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=
3.2.2直接在容器內(nèi)安裝vim工具,直接編輯
首先,更換apt-get源,然后再docker容器內(nèi)安裝vim
mv /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb http://mirrors.163.com/debian/ jessie main non-frcontrib" >/etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ jessie-proposed-updatmain non-free contrib" >>/etc/apt/sources.list
echo "deb-src http://mirrors.163.com/debian/ jessie main non-frcontrib" >>/etc/apt/sources.list
echo "deb-src http://mirrors.163.com/debiajessie-proposed-updates main non-free contri>>/etc/apt/sources.list
#更新安裝源
apt-get update
apt-get install -y vim
后續(xù)操作,如3.1.1所示,不在贅述;
3.2 直接在Docker建立是指定字符集(暫時(shí)未驗(yàn)證)
參考:
《Docker下終端無法輸入中文問題》
https://blog.csdn.net/wen3qin/article/details/78386654
在純docker環(huán)境中
docker run -it mysql env LANG=C.UTF-8 /bin/bash
在K8S環(huán)境中
//啟動
kubectl run -it –rm –image=mysql mysql-client – env LANG=C.UTF-8 /bin/bash
//進(jìn)入pod
kubectl exec -ti mysql-client – env LANG=C.UTF-8 /bin/bash