技术角 | 解决ES SQL命令行启动报错 ./x-pack-env: No such file or directory

时间:2022-07-22
本文章向大家介绍技术角 | 解决ES SQL命令行启动报错 ./x-pack-env: No such file or directory,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

今天在云服务器上,通过下载源码包完成有认证配置的Elasticsearch部署后,准备执行如下命令进入Elasticsearch SQL进行一些SQL语句的操作:

bin/elasticsearch-sql-cli uri=http://elastic:ESabc+2333@10.66.66.2:9200/

但执行后,发现使用bin/elasticsearch-sql-cli登录时,出错提示./x-pack-env: No such file or directory,莫名其妙,遂即查看下该脚本,内容如下:

#!/bin/bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.

source "`dirname "$0"`"/elasticsearch-env

source "`dirname "$0"`"/x-pack-env

CLI_JAR=$(ls $ES_HOME/bin/elasticsearch-sql-cli-*.jar)

exec 
  "$JAVA" 
  -jar "$CLI_JAR" 
  "$@"

其中有效行第二行的source "`dirname "$0"`"/x-pack-env看来执行起来有问题,估计是在依赖导入生效时找不到路径。经过一番折腾,解决了该问题,即将该行替换为如下命令:

# source "`dirname "$0"`"/x-pack-env
source /usr/share/elasticsearch/bin/x-pack-env

即将引入依赖的路径写死即可,使用时请根据实际路径修改x-pack-env的指向路径。

修改完后,再用刚才的命令启动Elasticsearch SQL,正常进入,完美结局。

后来又仔细查找了下资料,发现该问题是Elasticsearch 7.4版本的一个小bug,在Elasticsearch 7.8的包内,bin/elasticsearch-sql-cli这个脚本内容已经修改为:

#!/bin/bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.

source "`dirname "$0"`"/elasticsearch-env

source "$ES_HOME"/bin/x-pack-env

CLI_JAR=$(ls "$ES_HOME"/bin/elasticsearch-sql-cli-*.jar)

exec 
  "$JAVA" 
  "$XSHARE" 
  -jar "$CLI_JAR" 
  "$@"

也就是说,如果Elasticsearch 7.8安装时,操作系统环境变量内$ES_HOME能够正确设置的话,就可以正常启动Elasticsearch SQL了。这样比直接修改产品本身脚本来说要规范的多了。