总结遇到的elasticsearch启动失败的几种情况及解决
在有一次搭建elasticsearch的时候,使用systemctl启动elasticsearch失败,然后在bin目录下面去使用启动脚本启动,发现报错不能用root用户启动,报“Caused by: java.lang.RuntimeException: can not run elasticsearch as root”:
[root@localhost bin]# ./elasticsearch
[2017-12-20T17:01:47,922][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [node-1] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.1.1.jar:6.1.1]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.1.1.jar:6.1.1]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.1.1.jar:6.1.1]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.1.1.jar:6.1.1]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.1.1.jar:6.1.1]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-6.1.1.jar:6.1.1]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:85) ~[elasticsearch-6.1.1.jar:6.1.1]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:104) ~[elasticsearch-6.1.1.jar:6.1.1]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) ~[elasticsearch-6.1.1.jar:6.1.1]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:322) ~[elasticsearch-6.1.1.jar:6.1.1]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) ~[elasticsearch-6.1.1.jar:6.1.1]
... 6 more
[root@localhost bin]# cd ..
解决:
创建一个独立的用户,比如elk来启动elasticsearch,不用root用户启动
2、elasticsearch安装目录权限不对
遇到启动elasticsearch失败,使用的是专门的用户elk来启动的,启动日志提示不能加载配置文件:
[elk@docker bin]$ ./elasticsearch
Exception in thread "main" 2018-06-03 17:36:23,881 main ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property ‘log4j2.debug‘ to show Log4j2 internal initialization logging.
2018-06-03 17:36:24,113 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:585)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.checkMBeanTrustPermission(DefaultMBeanServerInterceptor.java:1848)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:322)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
at org.apache.logging.log4j.core.jmx.Server.register(Server.java:389)
at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:167)
at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:140)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:556)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
at org.elasticsearch.common.logging.ESLoggerFactory.getLogger(ESLoggerFactory.java:54)
at org.elasticsearch.common.logging.ESLoggerFactory.getLogger(ESLoggerFactory.java:62)
at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
at org.elasticsearch.ExceptionsHelper.<clinit>(ExceptionsHelper.java:42)
at org.elasticsearch.ElasticsearchException.toString(ElasticsearchException.java:663)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at java.lang.Throwable$WrappedPrintStream.println(Throwable.java:748)
at java.lang.Throwable.printStackTrace(Throwable.java:655)
at java.lang.Throwable.printStackTrace(Throwable.java:643)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1061)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1052)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1959)
SettingsException[Failed to load settings from /usr/local/elasticsearch-5.6.0/config/elasticsearch.yml]; nested: AccessDeniedException[/usr/local/elasticsearch-5.6.0/config/elasticsearch.yml];
at org.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:102)
at org.elasticsearch.cli.EnvironmentAwareCommand.createEnv(EnvironmentAwareCommand.java:72)
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:67)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134)
at org.elasticsearch.cli.Command.main(Command.java:90)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84)
Caused by: java.nio.file.AccessDeniedException: /usr/local/elasticsearch-5.6.0/config/elasticsearch.yml
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at org.elasticsearch.common.settings.Settings$Builder.loadFromPath(Settings.java:1032)
at org.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:100)
... 6 more
[elk@docker bin]$
解决:
看到“Caused by: java.nio.file.AccessDeniedException: /usr/local/elasticsearch-5.6.0/config/elasticsearch.yml”的提示,就去检查目录的权限,果然是root:root权限,使用elk用户去启动,就报错了。
将目录的权限改成elk:elk就好了。
[root@docker ~]# ll /usr/local/
drwxr-xr-x 7 root root 123 9月 7 2017 elasticsearch-5.6.0
[root@docker local]# chown -R elk:elk elasticsearch-5.6.0
[root@docker local]# ll
drwxr-xr-x 7 elk elk 123 9月 7 2017 elasticsearch-5.6.0
3、日志和数据目录权限异常,启动失败
在一次启动elasticsearch的时候启动失败,日志有提示“main ERROR Unable to create file /home/elk/logs/my-application_index_indexing_slowlog.log java.io.IOException: 权限不够”和“Caused by: java.nio.file.AccessDeniedException: /home/elk/data/nodes”。关键日志信息如下(下面两段日志是节选的,日志信息太多了,有很多重复的):
[elk@docker bin]$ ./elasticsearch
2018-06-04 01:15:07,609 main ERROR Unable to create file /home/elk/logs/my-application.log java.io.IOException: 权限不够
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
后面还有一段:
Caused by: java.lang.IllegalStateException: Failed to create node environment
at org.elasticsearch.node.Node.<init>(Node.java:268) ~[elasticsearch-5.6.0.jar:5.6.0]
at org.elasticsearch.node.Node.<init>(Node.java:245) ~[elasticsearch-5.6.0.jar:5.6.0]
at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:233) ~[elasticsearch-5.6.0.jar:5.6.0]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:233) ~[elasticsearch-5.6.0.jar:5.6.0]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:342) ~[elasticsearch-5.6.0.jar:5.6.0]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:132) ~[elasticsearch-5.6.0.jar:5.6.0]
... 6 more
Caused by: java.nio.file.AccessDeniedException: /home/elk/data/nodes
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84) ~[?:?]
日志报的“/home/elk/data/”和“/home/elk/logs/”目录都是在elasticsearch.yml配置文件里面配置的两个目录,查看权限果然不对,权限是elk的目录权限是elk,但是下面的data目录和logs目录权限还是root,因此引起了启动失败:
[root@docker home]# ll
drwx------ 5 elk elk 125 6月 3 17:35 elk #elk目录权限正常
[root@docker home]# cd elk/
[root@docker elk]# ll
总用量 0
drwxr-xr-x 2 root root 6 6月 3 17:34 data #下面的data和logs目录还是root
drwxr-xr-x 2 root root 6 6月 3 17:34 logs
将目录权限修改成elk之后,启动就OK了
[root@docker elk]# chown elk:elk -R ./*
[root@docker elk]# ll
总用量 0
drwxr-xr-x 2 elk elk 6 6月 3 17:34 data
drwxr-xr-x 2 elk elk 6 6月 3 17:34 logs
[root@docker elk]#
4、内存不够,启动失败
在一次使用虚拟机做实验的过程中启动elasticsearch遇到了这样的报错:
[elk@docker bin]$ ./elasticsearch
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error=‘Cannot allocate memory‘ (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/local/elasticsearch-5.6.0/bin/hs_err_pid2819.log
解决:
原因:
查看了 /usr/local/elasticsearch-5.6.0/bin/hs_err_pid1027.log日志文件,出现这样的报错,有两种可能:
1、系统进程数达到上限了,部属的时候/etc/security/limits.conf文件修改没有生效。
2、确实物理内存不够
解决:
通过命令查看系统限制:ulimit -a查看open files不够大,如果不够大,就尝试通过设大该值:
[root@docker ~]# ulimit -n
1024
果然是部属的时候修改的/etc/security/limits.conf文件没有生效。于是将机器reboot了一下,在查看就生效了:
[root@docker ~]# ulimit -n
65536
但是在启动的时候还是报同样的错,查看了一下内存,这个虚拟机的内存只有1G,那应该就是物理内存不够了。目前还剩下这么多:
[root@docker ~]# free -h
total used free shared buff/cache available
Mem: 974M 119M 79M 7.7M 775M 680M
Swap: 819M 0B 819M
手动清理了一下内存:
[root@docker ~]# echo 3 > /proc/sys/vm/drop_caches
[root@docker ~]# free -h
total used free shared buff/cache available
Mem: 974M 114M 789M 7.7M 69M 739M
Swap: 819M 0B 819M
[root@docker ~]#
但是在启动报错依旧。
于是增加物理内存到2G:
[root@docker elk]# free -h
total used free shared buff/cache available
Mem: 1.8G 1.6G 77M 4K 72M 29M
Swap: 819M 691M 128M
然后切换到elk用户去启动服务,就OK了:
[elk@docker bin]$ ./elasticsearch #为了看启动日志就前台启动的,ctrl+c 进程就会over掉
检查端口,起来了
[root@docker elk]# netstat -tlunp|grep 9200
tcp6 0 0 10.0.0.16:9200 :::* LISTEN 9628/java
[root@docker elk]#
然后关掉进程重新后台启动:
[elk@docker bin]$ nohup ./elasticsearch >/dev/null 2>&1 & #后台启动
[2] 9808
[elk@docker bin]$ ps -ef|grep elasticsearch #检查进程
elk 9808 4115 10 01:29 pts/0 00:00:23 /usr/local/jdk1.8.0_151/bin/java -Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError -Des.path.home=/usr/local/elasticsearch-5.6.0 -cp /usr/local/elasticsearch-5.6.0/lib/* org.elasticsearch.bootstrap.Elasticsearch
elk 9890 4115 0 01:32 pts/0 00:00:00 grep --color=auto elasticsearch
[elk@docker bin]$
[root@docker elk]# netstat -tlunp|grep 9200 #用root用户检查端口
tcp6 0 0 10.0.0.16:9200 :::* LISTEN 9808/java
[root@docker elk]#