场景:
在项目中,为了搭建一个高效的全文搜索和分析引擎,我们决定使用 Elasticsearch 7.12.1。由于项目需求,需要在本地开发环境中安装和运行 Elasticsearch。然而,在初次安装和启动 Elasticsearch 容器时,遇到了 org.elasticsearch.bootstrap.StartupException: ElasticsearchException[failed to bind service]; nested: FileSystemException[/usr/share/elasticsearch/data/nodes/0:的报错信息错误。
问题描述
在使用 Docker 命令启动 Elasticsearch 容器时,出现了以下错误:
"stacktrace": ["org.elasticsearch.bootstrap.StartupException: ElasticsearchException[failed to bind service]; nested: FileSystemException[/usr/share/elasticsearch/data/nodes/0: Not a directory];",
"at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116) ~[elasticsearch-cli-7.12.1.jar:7.12.1]",
"at org.elasticsearch.cli.Command.main(Command.java:79) ~[elasticsearch-cli-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81) ~[elasticsearch-7.12.1.jar:7.12.1]",
"Caused by: org.elasticsearch.ElasticsearchException: failed to bind service",
"at org.elasticsearch.node.Node.<init>(Node.java:744) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.node.Node.<init>(Node.java:278) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:217) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:217) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:397) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.12.1.jar:7.12.1]",
"... 6 more",
"Caused by: java.nio.file.FileSystemException: /usr/share/elasticsearch/data/nodes/0: Not a directory",
"at sun.nio.fs.UnixException.translateToIOException(UnixException.java:100) ~[?:?]",
"at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106) ~[?:?]",
"at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111) ~[?:?]",
"at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:396) ~[?:?]",
"at java.nio.file.Files.createDirectory(Files.java:694) ~[?:?]",
"at java.nio.file.Files.createAndCheckIsDirectory(Files.java:801) ~[?:?]",
"at java.nio.file.Files.createDirectories(Files.java:787) ~[?:?]",
"at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment.java:265) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.env.NodeEnvironment$NodeLock.<init>(NodeEnvironment.java:202) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:262) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.node.Node.<init>(Node.java:352) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.node.Node.<init>(Node.java:278) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:217) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:217) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:397) ~[elasticsearch-7.12.1.jar:7.12.1]",
"at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.12.1.jar:7.12.1]",
"... 6 more"] }
uncaught exception in thread [main]
ElasticsearchException[failed to bind service]; nested: FileSystemException[/usr/share/elasticsearch/data/nodes/0: Not a directory];
Likely root cause: java.nio.file.FileSystemException: /usr/share/elasticsearch/data/nodes/0: Not a directory
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:100)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:396)
at java.base/java.nio.file.Files.createDirectory(Files.java:694)
at java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:801)
at java.base/java.nio.file.Files.createDirectories(Files.java:787)
at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment.java:265)
at org.elasticsearch.env.NodeEnvironment$NodeLock.<init>(NodeEnvironment.java:202)
at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:262)
at org.elasticsearch.node.Node.<init>(Node.java:352)
at org.elasticsearch.node.Node.<init>(Node.java:278)
at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:217)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:217)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:397)
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159)
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150)
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116)
at org.elasticsearch.cli.Command.main(Command.java:79)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81)
For complete error details, refer to the log at /usr/share/elasticsearch/logs/docker-cluster.log
运行的 Docker 命令如下:
docker run -d \
--name elasticsearch \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-v es-data:/usr/share/elasticsearch/data \
-v es-plugins:/usr/share/elasticsearch/plugins \
-p 9200:9200 \
-p 9300:9300 \
docker.elastic.co/elasticsearch/elasticsearch:7.12.1
原因分析:
这个错误的根本原因是 /usr/share/elasticsearch/data/nodes/0 不是一个目录。可能是在第一次运行 Elasticsearch 容器时,某些文件被写入了这个位置,导致其变成了一个文件而不是目录。
Elasticsearch 在启动时需要在 /usr/share/elasticsearch/data/nodes/0 位置创建和访问数据目录。如果该路径已经存在并且不是目录,Elasticsearch 将无法正确绑定服务,从而导致启动失败。
解决方案:
1.停止并删除容器
Elasticsearch是我当前的容器名称,需要改成自己使用的哦
首先,停止并删除当前的 Elasticsearch 容器:
docker stop elasticsearch
docker rm elasticsearch
2.检查并删除卷
由于使用了 Docker 卷,所以需要确保卷的状态是正确的。首先,找到卷的名称,然后删除它们:
docker volume ls
找到 es-data 和 es-plugins 相关的卷,然后删除它们:
docker volume rm es-data es-plugins
3.重新创建并启动容器
确保卷是干净的,然后重新启动容器:
docker run -d \
--name elasticsearch \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-v es-data:/usr/share/elasticsearch/data \
-v es-plugins:/usr/share/elasticsearch/plugins \
-p 9200:9200 \
-p 9300:9300 \
docker.elastic.co/elasticsearch/elasticsearch:7.12.1
4.查看日志信息
执行启动命令后查看容器日志:
docker logs elasticsearch
5.检查卷的内容
如果再次遇到问题,可以检查卷的内容以确保它们的状态是正确的。进入卷目录检查:
docker run --rm -it -v es-data:/data busybox sh
ls /data
希望这篇博客能帮助你解决在初次安装 Elasticsearch 时遇到的 ‘failed to bind service’ 问题。如果有其他问题或疑问,欢迎留言讨论!