ElasticSearch
- 概念
- 安装
- 安装es
- 后台启动 & 停止
- 启动
- nohup 记录pid
- 停止
- 其他启动错误
- max number of threads
- 内存不足 Cannot allocate memory
- failed to obtain node locks
概念
ES是一款分布式全文搜索引擎,基于Lucene,进行了二次封装,更容易上手。
安装
基于Java环境,最好提前安装好Java环境,有一个自带的jdk,但是版本可能不是其他组件需要的。
我们这里基于虚拟机来安装 es
安装es
- 下载官网
手动下载或者 curl 直接下载
官网可能会比较慢,国内也有镜像站 华为
这是ES历史版本
curl -O https://repo.huaweicloud.com/elasticsearch/7.14.0/elasticsearch-7.14.0-linux-x86_64.tar.gz
- es放入 opt 目录下(个人习惯)
- 解压
tar zxvf /opt/elasticsearch-7.14.0-linux-x86_64.tar.gz
-
启动
-
启动
./bin/elasticsearch
报错,新版本的es不允许以 root 启动,
[ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [origin.centos] 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.14.0.jar:7.14.0] at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.14.0.jar:7.14.0] at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75) ~[elasticsearch-7.14.0.jar:7.14.0] at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116) ~[elasticsearch-cli-7.14.0.jar:7.14.0] at org.elasticsearch.cli.Command.main(Command.java:79) ~[elasticsearch-cli-7.14.0.jar:7.14.0] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.14.0.jar:7.14.0] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81) ~[elasticsearch-7.14.0.jar:7.14.0] Caused by: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:103) ~[elasticsearch-7.14.0.jar:7.14.0] at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:170) ~[elasticsearch-7.14.0.jar:7.14.0] at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:399) ~[elasticsearch-7.14.0.jar:7.14.0] at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.14.0.jar:7.14.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:103) at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:170) at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:399) 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 /opt/elasticsearch-7.14.0/logs/elasticsearch.log
-
这里为其创建一个用户 elastic
useradd elastic
-
设置密码
passwd elastic
根据提示设置完密码
-
目录授权
chown -R elastic /opt/elasticsearch-7.14.0
-
创建es 存储日志及数据的目录
mkdir -p /data/es chown -R elastic /data/es mkdir -p /var/log/es chown -R elastic /var/log/es
-
修改 elasticsearch.yml 配置日志及数据目录,位于 Paths 节点下
# ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): # path.data: /data/es # # Path to log files: # path.logs: /var/log/es #
-
切换用户
su elastic
-
再启动
./bin/elasticsearch
启动成功,但是有警告信息
这个放到后面再说
-
-
访问ES
我们先在虚拟机本地访问,可以看到成功访问到curl localhost:9200 { "name" : "origin.centos", "cluster_name" : "elasticsearch", "cluster_uuid" : "XVgPBZ78RdWRssPtuSeJTg", "version" : { "number" : "7.14.0", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1", "build_date" : "2021-07-29T20:49:32.864135063Z", "build_snapshot" : false, "lucene_version" : "8.9.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
然后我们在宿主机访问 虚拟机ip:9200 发现访问不了;
其实在虚拟机 通过ip也访问不了[root@origin ~]# curl 192.168.0.101:9200 curl: (7) Failed connect to 192.168.0.101:9200; Connection refused
这是因为,ES默认,只能通过本地回环地址访问
-
修改ES网络配置
vim /opt/elasticsearch-7.14.0/config/elasticsearch.yml network.host: 0.0.0.0
保存退出
-
再启动ES
./bin/elasticsearch
会发现,报错咯,大概意思是要解决以下这三个问题才能启动
- 第一个
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
则需要配置 elasticsearch 进程,里面以是用户的角度去配置的 可打开的最大文件数vim /etc/security/limits.conf # 用户名 elastic ,不限制用户 用 * 替代 elastic soft nofile 65536 elastic hard nofile 65536
- 第二个
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
这个值是系统层面的,和用户进程没啥关系
保存退出。vim /etc/sysctl.conf vm.max_map_count = 262144
刷新配置sysctl -p
- 第三个
the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
大概意思是,默认集群节点发现(这就是由于上面开启其他主机访问的缘故)配置不适用于生产环境,至少配置以下一项。
修改配置文件,这里设置 discovery.seed_hosts 这个属性vim /opt/elasticsearch-7.14.0/config/elasticsearch.yml # 本机ip discovery.seed_hosts: ["192.168.0.101"]
- 第一个
-
再次启动ES
./bin/elasticsearch
到这里,我已经能够在其他主机上通过ip访问 ES了
后台启动 & 停止
启动
- nohup ./bin/elasticsearch > /dev/null 2>&1 &
- nohup /opt/elasticsearch-7.14.0/bin/elasticsearch > /dev/null 2>&1 &
- ./bin/elasticsearch -d -p pid(记录pid的一个文件,可以用于脚本管理启停) #相对路径
- /opt/elasticsearch-7.14.0/bin/elasticsearch -d #绝对路径
nohup 记录pid
路径既可以是相对路径也可以是绝对路径
nohup /opt/elasticsearch-7.14.0/bin/elasticsearch >/dev/null 2>&1 & echo $! > pid
nohup /opt/elasticsearch-7.14.0/bin/elasticsearch >/dev/null 2>&1 & echo $! > /opt/elasticsearch-7.14.0/pid
停止
kill [-9] pid
kill cat pid(启动时记录pid的文件)
cat pid(启动时记录pid的文件) | xargs kill [-9]
其他启动错误
max number of threads
max number of threads [3818] for user [elastic] is too low, increase to at least [4096] 类似问题,则需要配置 当前用户启动的进程允许开启多少个线程
vim /etc/security/limits.conf
# 用户名 elastic ,不限制用户 用 * 替代
elastic soft nproc 4096
elastic hard nproc 4096
内存不足 Cannot allocate memory
将ES所需内存调小后,再启动ES。
vim ./config/jvm.options
-Xms2g
-Xmx2g
failed to obtain node locks
这是由于当前节点上已经有ES实例绑定了数据目录。
此时要看是否确实需要在节点上运行多个实例。
如果需要
- 则修改配置文件 elasticsearch.yml
配置当前节点可以运行的es实例数
node.max_local_storage_nodes: 2
否则,需停止当前正在运行的ES进程,再启动。