操作系统版本:CentOS 7 64位
Elasticsearch 版本: 6.4.2
JDK 版本:open JDK 8
java环境已经配置好了。 如何配置java环境?请您看这篇博文
1.下载安装包
登录官方网站,下载链接: https://www.elastic.co/cn/downloads/past-releases#elasticsearch ,本实例中,使用的版本是elasticsearch 6.4.2 ,如图:
2.安装elasticsearch
解压文件到 /usr/local/ ,并使用vim修改config文件夹下elasticsearch.yml文件,操作步骤如下所示
# 编辑elasticsearch配置文件
[root@localhost config]# vim /usr/local/elasticsearch-6.4.2/config/elasticsearch.yml
# 修改位置约为 33行和37行,放开注释,配置data和log日志路径
path.data: /usr/local/elasticsearch-6.4.2/data
path.logs: /usr/local/elasticsearch-6.4.2/logs
# 修改位置约为55行和59行,放开注释,配置网络和端口
network.host: 0.0.0.0
http.port: 9200
修改完成后,保存退出vim
# 修改用户权限
[root@localhost ~]# visudo
在配置文件约100行,在“root ALL=(ALL) ALL”配置下方新增一行配置:
elasticsearch ALL=(ALL) ALL
保存并退出
3.修改启动问题
由于elasticsearch的策略问题,不允许超级用户root启动,此处需要解决的是elasticsearch使用普通用户启动的配置更改:如下所示
# 创建一个普通用户elasticsearch ,用户组指向aoto用户组,家目录指向aoto家目录(aoto用户是安装操作系统时创建的一个普通用户)
[root@localhost ~]# useradd elasticsearch
# 重置用户密码(需要连续输入两次,本实例使用的是aoto@123)
[root@localhost ~]# passwd elasticsearch
# 修改文件权限
[root@localhost ~]# chown -R elasticsearch /usr/local/elasticsearch-6.4.2
4.解决启动报错
此时,切换到elasticsearch启动依然会报错,报错信息如图所示:
首先解决报错1]“max file descritor [4096] for ...”,配置linux系统线程和文件参数
# 打开配置文件
[root@localhost ~]# vim /etc/security/limits.conf
# 在 #End of file 之前 加入以下配置
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
保存并退出
解决第二个错误2] “max virtual memory areas ...”,配置系统允许的软件允许内存
# 打开配置文件
[root@localhost ~]# vi /etc/sysctl.conf
# 在文件末尾添加以下配置项
vm.max_map_count = 655360
fs.file-max=655360
保存并退出
[root@localhost ~]# sysctl -p # 加载参数
# 切换到elasticsearch用户
[root@localhost ~]# su elasticsearch
# 启动elasticsearch服务(后台启动,后面加参数 -d)
[root@localhost ~]# /usr/local/elasticsearch-6.4.2/bin/elasticsearch
启动后,显示“publish_address {192.168.1.105:9200}”,如图所示
5.使用本地curl指令校验
重新打开一个terminal窗口,键入curl 127.0.0.1:9200,查看输出信息,如下所示:
[root@localhost ~]# curl 127.0.0.1:9200
{
"name" : "whLftvS",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "66ZoA9keQC6kEG4whr8yyg",
"version" : {
"number" : "6.4.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "04711c2",
"build_date" : "2018-09-26T13:34:09.098244Z",
"build_snapshot" : false,
"lucene_version" : "7.4.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
显示上述信息,表示elasticsearch已经部署到本地,本地访问成功,接下来需要配置远程访问。
6.配置远程访问
elasticsearch之所以无法进行远程访问,是因为9200端口被防火墙规则阻挡,此时有两种选择,关闭防火墙或添加端口访问防火墙规则,推荐使用后者,因为实际的生产环境中,出于安全考虑,不允许粗鲁的直接关闭防火墙。
# 简单介绍一下防火墙关闭方法,不推荐使用
[root@localhost ~]# systemctl status firewalld # 查看防火墙状态
#如果上一步输出的信息中含有绿色字体Active: active (running),则需要关闭防火墙
[root@localhost ~]# systemctl stop firewalld.service # 关闭防火墙
[root@localhost ~]# systemctl disable firewalld.service # 禁止防火墙开机自启动
#第二种,添加防火墙规则,推荐使用
# 添加9200端口访问规则
[root@localhost ~]# firewall-cmd --zone=public --add-port=9200/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload # 重新加载防火墙规则
success
此时,在其他设备上访问http://serverip:9200,即可得到配置信息,如图所示
至此,elasticsearch服务安装与环境访问配置完成