重启Linux服务器上的elasticsearch时,需要做的步骤

文章目录

    • 正常的情况
    • 重启elasticsearch时遇到的错误
      • 错误一:can not run elasticsearch as root
      • 错误二:{"error":{"root_cause":[{"type":"master_not_discovered_exception","reason":null}],"type":"master_not_discovered_exception","reason":null},"status":503}
    • 结论:重启Linux服务器上的elasticsearch时,需要做的步骤
    • 知识点:Linux中的curl命令

这次,我充分吸取上次 服务器上Kafka启动报错:error=‘Cannot allocate memory‘ (errno=12)_夜中听雪的博客-CSDN博客 的教训。当我在用startup.sh命令启动tomcat后,查看tomcat的日志/www/server/tomcat/logs/catalina.2021-05-18.log,发现报错和elasticsearch有关,我就直接去重新启动elasticsearch了(就是kill 原es进程,再次启动es),并且是前台启动,方便查看错误。

PS:elasticsearch我也是在安装时就跟着老师一遍启动成功了的。结果kill es进程,然后启动es,出了这么多错。

正常的情况

压缩包安装elasticsearch和ik分词器,然后启动并测试,启动成功。

# 安装elasticsearch和ik分词器
[root@wu1 ~]# tar -zvxf /root/elasticsearch-7.12.0-linux-x86_64.tar.gz -C /opt
[root@wu1 ~]# unzip -d /opt/elasticsearch-7.12.0/plugins/ik /root/elasticsearch-analysis-ik-7.12.0.zip 	
[root@wu1 ~]# vim /opt/elasticsearch-7.12.0/config/elasticsearch.yml 	# 取消下方内容的注释,修改以下内容
cluster.name: nowcoder
path.data: /tmp/elastic/data
path.logs: /tmp/elastic/logs

[root@wu1 ~]# vim /opt/elasticsearch-7.12.0/config/jvm.options	# 取消下方内容的注释,修改以下内容,这两个参数要顶格写。
-Xms256m
-Xmx512m

[root@wu1 ~]# groupadd nowcoder
[root@wu1 ~]# useradd nowcoder1 -p 123456 -g nowcoder
[root@wu1 ~]# cd /opt
[root@wu1 opt]# chown -R nowcoder1:nowcoder *
[root@wu1 opt]# cd /tmp
[root@wu1 tmp]# chown -R nowcoder1:nowcoder *
[root@wu1 tmp]# su - nowcoder1
[nowcoder1@wu1 ~]$ /opt/elasticsearch-7.12.0/bin/elasticsearch -d	# 后台启动es
[nowcoder1@wu1 ~]$ su -
Password: 
[root@wu1 ~]# curl -X GET "localhost:9200/_cat/health?v"	# 测试es是否成功启动
epoch      timestamp cluster  status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1619627359 16:29:19  nowcoder green           1         1      0   0    0    0        0             0                  -                100.0%

重启elasticsearch时遇到的错误

错误一:can not run elasticsearch as root

[root@wu1 target]# /opt/elasticsearch-7.12.0/bin/elasticsearch	# 前台启动elasticsearch
warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
Future versions of Elasticsearch will require Java 11; your Java version from [/usr/java/jdk1.8.0_281-amd64/jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.
warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
Future versions of Elasticsearch will require Java 11; your Java version from [/usr/java/jdk1.8.0_281-amd64/jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.
[2021-05-18T14:19:37,621][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [wu1] 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:163) ~[elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75) ~[elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116) ~[elasticsearch-cli-7.12.0.jar:7.12.0]
	at org.elasticsearch.cli.Command.main(Command.java:79) ~[elasticsearch-cli-7.12.0.jar:7.12.0]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81) ~[elasticsearch-7.12.0.jar:7.12.0]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
	at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:101) ~[elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:168) ~[elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:397) ~[elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.12.0.jar:7.12.0]
	... 6 more
uncaught exception in thread [main]
java.lang.RuntimeException: can not run elasticsearch as root
	at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:101)
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:168)
	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 /tmp/elastic/logs/nowcoder.log
[root@wu1 target]# 2021-05-18 06:31:15,826225 UTC [9667] INFO  Main.cc@106 Parent process died - ML controller exiting

它说:有关完整的错误详细信息,请参阅/tmp/elastic/logs/nowcoder.log

错误很明显,即:需要创建一个独立的用户来启动elasticsearch,不能用root用户启动。

错误二:{“error”:{“root_cause”:[{“type”:“master_not_discovered_exception”,“reason”:null}],“type”:“master_not_discovered_exception”,“reason”:null},“status”:503}

我在XShell建立两个窗口都连接到云服务器,我的窗口一用于前台启动es,窗口二用于测试es是否启动成功。

我在窗口一启动es后,用窗口二进行测试,窗口二得到下方错误。

[nowcoder1@wu1 ~]$ curl -X GET "localhost:9200/_cat/health?v"
{"error":{"root_cause":[{"type":"master_not_discovered_exception","reason":null}],"type":"master_not_discovered_exception","reason":null},"status":503}

下方报错是我在窗口一启动es后,用窗口二进行测试,然后窗口一输出的报错。

[2021-05-18T15:59:24,196][WARN ][r.suppressed             ] [wu1] path: /_cat/health, params: {v=}
org.elasticsearch.discovery.MasterNotDiscoveredException: null
	at org.elasticsearch.action.support.master.TransportMasterNodeAction$AsyncSingleAction$2.onTimeout(TransportMasterNodeAction.java:219) [elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.cluster.ClusterStateObserver$ContextPreservingListener.onTimeout(ClusterStateObserver.java:324) [elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.cluster.ClusterStateObserver$ObserverClusterStateListener.onTimeout(ClusterStateObserver.java:241) [elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.cluster.service.ClusterApplierService$NotifyTimeout.run(ClusterApplierService.java:590) [elasticsearch-7.12.0.jar:7.12.0]
	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:673) [elasticsearch-7.12.0.jar:7.12.0]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_281]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_281]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_281]
[2021-05-18T15:59:30,918][WARN ][o.e.c.c.ClusterFormationFailureHelper] [wu1] master not discovered or elected yet, an election requires two nodes with ids [JmLua1JyT8mIZQz87lqOGQ, hn_ORzUDT5m9l1ERh-JmnQ], have discovered [{wu1}{hn_ORzUDT5m9l1ERh-JmnQ}{s0HFL6T6Tc6kSfxJV_PqJg}{127.0.0.1}{127.0.0.1:9300}{cdfhilmrstw}{ml.machine_memory=3823214592, xpack.installed=true, transform.node=true, ml.max_open_jobs=20, ml.max_jvm_size=519438336}] which is not a quorum; discovery will continue using [127.0.0.1:9301, 127.0.0.1:9302, 127.0.0.1:9303, 127.0.0.1:9304, 127.0.0.1:9305] from hosts providers and [{wu1}{hn_ORzUDT5m9l1ERh-JmnQ}{s0HFL6T6Tc6kSfxJV_PqJg}{127.0.0.1}{127.0.0.1:9300}{cdfhilmrstw}{ml.machine_memory=3823214592, xpack.installed=true, transform.node=true, ml.max_open_jobs=20, ml.max_jvm_size=519438336}] from last-known cluster state; node term 0, last-accepted version 0 in term 0

经过查找资料,我在 /opt/elasticsearch-7.12.0/config/elasticsearch.yml 中配置了如下内容:

# my start
node.name: node-1
discovery.seed_hosts: ["127.0.0.1"]
cluster.initial_master_nodes: ["node-1"]
# my end 

然后重新启动es,并测试是否启动成功,启动失败。

注意:在用命令 /opt/elasticsearch-7.12.0/bin/elasticsearch 启动es时,要先用 ps -ef | grep elasticsearch 看看有没有运行的es进程,有的话,要用 kill PID 来杀死es进程,然后再启动,不然,你在启动es时就会报错,都不需要到进行测试就知道启动失败。

看到一句话:很多问题都出现在第一次配置失败。假如你Es项目路径下有建立了datas的目录,那就要在每次改配置的时候去清掉里面的东西,像是缓存垃圾,导致后面每次修改都不生效。

我反应过来,为什么第一次启动es能够直接启动成功,重新启动es却一直报错。可能就是因为第一次启动时没有 /tmp/elastic/data ,这个目录是启动后自动生成的。

于是我去删除了 /tmp/elastic/data 的data目录。然后重新启动es,并测试,成功。

然后,我去掉上面在/opt/elasticsearch-7.12.0/config/elasticsearch.yml中进行的配置,并删除 /tmp/elastic/data ,再次重启es并测试,成功。

当然,我是只用了本机的es,没有用集群。

结论:重启Linux服务器上的elasticsearch时,需要做的步骤

即:重启es时,需要做以下步骤:

1、要先用 ps -ef | grep elasticsearch 看看有没有运行的es进程,有的话,要用 kill PID 来杀死es进程。

2、删除了 /tmp/elastic/data 的data目录。

3、别忘记切换到非root用户

[root@wu1 tmp]# su - nowcoder1
[nowcoder1@wu1 ~]$ /opt/elasticsearch-7.12.0/bin/elasticsearch -d	# 后台启动es
[nowcoder1@wu1 ~]$ su -
Password: 
[root@wu1 ~]# curl -X GET "localhost:9200/_cat/health?v"	# 测试es是否成功启动
epoch      timestamp cluster  status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1619627359 16:29:19  nowcoder green           1         1      0   0    0    0        0             0                  -                100.0%

知识点:Linux中的curl命令

下方代码中的curl命令,可以看 curl 的用法指南 - 阮一峰的网络日志 来了解下。

[nowcoder1@wu1 ~]$ curl -X GET "localhost:9200/_cat/health?v"	# 测试es是否成功启动
epoch      timestamp cluster  status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1621329032 09:10:32  nowcoder green           1         1      0   0    0    0        0             0                  -                100.0%

简单来说:curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。-X参数指定 HTTP 请求的方法。

就是说,如果你Windows上的elasticsearch启动了的话,可以在浏览器中输入该链接 localhost:9200/_cat/health?v 来测试Windows上的elasticsearch是否启动成功。

下方展示结果,可以看到,得到的内容和命令行输入命令curl -X GET "localhost:9200/_cat/health?v"是一样的。
在这里插入图片描述

你可能感兴趣的:(报错和解决,linux,elasticsearch,curl)