Featured image of post OS字符集影响Kingbase数据库连接

OS字符集影响Kingbase数据库连接

kingbase

基础环境:

环境项 环境值
客户端操作系统 OpenEuler 22.03 LTS( AARCH64 )
服务端操作系统 Kylin v10 sp1( AARCH64 )
数据库版本 KingbaseES_V008R006C007B0024_Kunpeng64
驱动版本 kingbase8-8.6.0

问题:

基础环境如上,程序在连接数据库时报错如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
com.kingbase8.util.KSQLException: FATAL: invalid value for parameter "client_encoding": "ANSI_X3.4-1968"
	at com.kingbase8.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2676)
	at com.kingbase8.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2788)
	at com.kingbase8.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:139)
	at com.kingbase8.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:260)
	at com.kingbase8.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
	at com.kingbase8.jdbc.KbConnection.<init>(KbConnection.java:231)
	at com.kingbase8.jdbc.KbConnection.<init>(KbConnection.java:201)
	at com.kingbase8.Driver.makeConnection(Driver.java:484)
	at com.kingbase8.Driver.connect(Driver.java:286)
	at java.sq1.DriverManager.getConnection(DriverManager.java:664)
	at java.sq1.DriverManager.getConnection(DriverManager.java:247)
    ...

通过报错来看是字符集不匹配造成了,查询服务端:

1
2
> show server_encoding;
UTF8

看了下连接串,貌似没啥问题。那会不会是操作系统的问题呢?进入容器:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ locale
LANG=POSIX
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=

果然不太对劲,看看配置:

1
2
$ cat /etc/locale.conf
LANG="zh_CN.UTF-8"

配置没问题,也有这个:

1
2
3
4
5
6
$ locale -a | grep zh_CN
zh_CN
zh_CN.gb18030
zh_CN.gb2312
zh_CN.gbk
zh_CN.utf8

那就好办了:

容器加两个环境变量:

1
$ docker run ... -e LANG=zh_CN.utf8 -e LC_ALL=zh_CN.utf8 ...

再进去看:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ locale
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=

然后再连接数据库,发现能正常连接了。