【原创】如何解决CentOS6.5手动升级glibc-2.15后中文乱码问题?【亲测可用干货】

blogdaren 2018-06-29 2评论 401人次

先说下为什么要升级glibc:

1. 首先大家一定要特别特别特别的谨慎和小心,除非确定升级的必要,否则不要随意升级glibc,因为会带来很大的麻烦。

2. OK,那金牛座为啥要升级glibc呢?因为我正在进行 React Native 开发时项目所依赖的包需要更高版本的glibc库支持, 而Centos6.5 中glibc默认版本为2.12, 比如调试时经常会遇到报错:

adb: /lib64/libc.so.6: version GLIBC_2.14 not found(required by adb)
adb: /lib64/libc.so.6: version GLIBC_2.15 not found(required by adb)

这两行报错意味着我们需要对glibc进行升级了,自然是升级到 glibc-2.15 这个高版本才对。

问题背景:

升级完后,重新SSH远程登录,结果报错:

Last login: Fri Jun 29 09:16:05 2018 from 192.168.1.37
-bash: warning: setlocale: LC_CTYPE: cannot change locale (zh_CN.UTF-8): No such file or directory
-bash: warning: setlocale: LC_COLLATE: cannot change locale (zh_CN.UTF-8): No such file or directory
-bash: warning: setlocale: LC_MESSAGES: cannot change locale (zh_CN.UTF-8): No such file or directory
-bash: warning: setlocale: LC_NUMERIC: cannot change locale (zh_CN.UTF-8): No such file or directory
-bash: warning: setlocale: LC_TIME: cannot change locale (zh_CN.UTF-8): No such file or directory
接下来,终端运行 locale 命令,同样报错:
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
于此同时,发现所有的中文目录都变成了一堆 ???????? ,即中文乱码现象。

问题原因:

系统使用了我们升级后的 /usr/local/glibc-2.15/bin/locale,而不是原来的 /usr/bin/locale,尝试运行 /usr/local/glibc-2.15/bin/locale -a 来查看所有的本地化locale语言包:

[root@node1 ~]# /usr/local/glibc-2.15/bin/locale -a
/usr/local/glibc-2.15/bin/locale: Cannot set LC_CTYPE to default locale: No such file or directory
/usr/local/glibc-2.15/bin/locale: Cannot set LC_MESSAGES to default locale: No such file or directory
/usr/local/glibc-2.15/bin/locale: Cannot set LC_COLLATE to default locale: No such file or directory
C
POSIX
很明显了,只有C 和 POSIX两项,所以才会报告No such file or directory错误。

定位locale配置文档:

[root@node1 ~]# strings /usr/local/glibc-2.15/bin/locale  | grep locale 
setlocale
b/localeH
b/localeH
/locale.
locale (GNU %s) %s
all-locales
/usr/local/glibc-2.15/lib/locale/locale-archive
locale: %-15.15s archive: /usr/local/glibc-2.15/lib/locale/locale-archive
/usr/local/glibc-2.15/lib/locale
locale: %-15.15s directory: %.*s
/usr/local/glibc-2.15/share/locale
Cannot set LC_CTYPE to default locale
Cannot set LC_MESSAGES to default locale
Cannot set LC_COLLATE to default locale
Cannot set LC_ALL to default locale
Write names of available locales
Get locale-specific information.

解决方案-1:

其实安装包INSTALL文档说的很清楚,自己去翻翻吧,一条命令搞定:

cd /path/to/glibc-2.15/build
make localedata/install-locales

解决方案-2:

根据上述定位我们得到locale的配置文件为: /usr/local/glibc-2.15/lib/locale/locale-archive,不出所料这个文档并不存在,正是问题关键所在,所以我们只需要设定好这个文件就行啦,怎么搞?方法就是再次使用 strings 命令定位出 /usr/bin/locale 的配置文档,然后复制过去就OK啦:

mkdir -pv  /usr/local/glibc-2.15/lib/locale/
cp /usr/lib/locale/locale-archive  /usr/local/glibc-2.15/lib/locale/

版权声明:除非注明,本文由( blogdaren )原创,转载请保留文章出处。

本文链接:【原创】如何解决CentOS6.5手动升级glibc-2.15后中文乱码问题?【亲测可用干货】

用户评论:

温酒煮华雄
2018-06-30 08:13
centos 7都这么多年了,赶紧换系统吧
blogdaren
2018-06-30 11:32
@温酒煮华雄:7好多命令都变了,不适应,还是6.5来的爽

发表评论:

您的昵称:
电子邮件:
个人主页: