美文网首页
CentOS7 安装 ElasticSearch6.5.4

CentOS7 安装 ElasticSearch6.5.4

作者: fingerQin | 来源:发表于2021-02-03 17:03 被阅读0次

最近由于项目需要,重新捡起曾经用过的 ElasticSerach 索引引擎。所谓好记性真不如烂笔头儿。

在写文档之后,想送给自己一句话:

老骥伏枥,志在千里。

1)安装 Java

ElasticSearch 是基于 Java 开发。所以,底层它依赖 Java 的环境。所以在安装前,我们一定要先安装 Java

$ yum install -y java-1.8.0-openjdk

2)下载 ElasticSearch 6.5.4 并解压

为什么是 ElastciSearch 6.5.4 ?是因为我们公司项目全部是这个版本。

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz
$ mkdir -p /data/server/elasticsearch
$ tar zxvf elasticsearch-6.5.4.tar.gz -C /data/server/elasticsearch

我们创建了一个 /data/server/elasticserach 目录。下一步就将 elasticsearch 解压到这个目录。以后可以安装多个版本的 elasticsearch

3)创建非 root 的 ElasticSearch 启动账号

因为 ElasticSearch 是不允许使用 root 账号启动的。

通常,在服务器运维过程中我们都会给不同的服务给一个单独的用户组和用户账号。

$ groupadd esgroup
$ useradd esuser -g esgroup
$ chown esuser:esgroup -R /data/server/elasticsearch

我们创建了一个 esgroup 用户组的账号 esuser。并将 /data/server/elasticsearch 目录设置为 esuesr

4)启动

启动之前请将当前用户切换到 esuser用户。否则,会提示不能以 root 启动的错误。

$ su esuser
$ cd /data/server/elasticsearch/elasticsearch-6.5.4/bin
$ ./elasticsearch

如果在启动过程中报错了。几乎都是权限问题导致的。

  • 请检查 elasticsearch 下所有的目录与文件是否都归属于 esuser 用户及 esgroup 用户组。如果不是,请立即使用 root 更正。chown -R esuser:esgrup dir_name
  • 请查看 logs 目录以及目录下文件是否有读写权限。

5)验证访问

启动成功之后,我们现在可以使用 http://127.0.0.1:9200 来访问。

5.1)允许外网跨域访问

由于我们是在服务器安装,所以您想通过外网访问。还需要再进行如下配置:

http.cors.enabled: true
http.cors.allow-origin: "*"

它们的作用:允许域名访问。

关于 http.cors.xxx 系列的配置有如下:

http.cors.enabled   是否支持跨域,默认为false
http.cors.allow-origin  当设置允许跨域,默认为*,表示支持所有域名,如果我们只是允许某些网站能访问,那么可以使用正则表达式。比如只允许本地地址。 /https?:\/\/localhost(:[0-9]+)?/
http.cors.max-age   浏览器发送一个“预检”OPTIONS请求,以确定CORS设置。最大年龄定义多久的结果应该缓存。默认为1728000(20天)
http.cors.allow-methods 允许跨域的请求方式,默认OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers 跨域允许设置的头信息,默认为X-Requested-With,Content-Type,Content-Length
http.cors.allow-credentials 是否返回设置的跨域Access-Control-Allow-Credentials头,如果设置为true,那么会返回给客户端。

注:不推荐在正式服务器开启外网访问的权限。不推荐在正式服务器开启外网访问的权限。不推荐在正式服务器开启外网访问的权限。重要的事情说三遍。

5.2)允许外网访问 9200 端口

防火墙开放 9200 端口

firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --reload

或者关闭防火墙

systemctl stop firewalld.service

systemctl disable firewalld.service 禁止防火墙开机启动

因为我们更改配置。所以,我们要重启 elasticsearch 服务器。

相关文章

网友评论

      本文标题:CentOS7 安装 ElasticSearch6.5.4

      本文链接:https://www.haomeiwen.com/subject/vplmtltx.html