从 Elasticsearch 7.x 版本开始,Elasticsearch 发行版包括了自己的 JDK。因此,您不需要单独安装 Java。以下是在 CentOS 7 上安装 Elasticsearch 7.17.7 的完整步骤:(数据默认保存在/var/lib/elasticsearch下,自行更改)
- 添加 Elasticsearch 仓库:
首先,创建一个新文件:
sudo vi /etc/yum.repos.d/elasticsearch.repo
然后,将以下内容粘贴到文件中:
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
保存并退出。
- 安装 Elasticsearch:
sudo yum install elasticsearch-7.17.7
- 配置 Elasticsearch:
编辑 Elasticsearch 配置文件:
sudo vi /etc/elasticsearch/elasticsearch.yml
根据需要修改一些关键设置,例如:
cluster.name: my-cluster
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node
对于单节点 Elasticsearch 集群,您可以将 discovery.type 设置为 single-node。这将跳过生产环境检查。要进行此更改,请将以下行添加到 /etc/elasticsearch/elasticsearch.yml 文件
直接用sed
sudo sed -i 's/^#cluster.name: my-application/cluster.name: my-cluster/' /etc/elasticsearch/elasticsearch.yml
sudo sed -i 's/^#node.name: node-1/node.name: node-1/' /etc/elasticsearch/elasticsearch.yml
sudo sed -i 's|^#path.data: /path/to/data|path.data: /var/lib/elasticsearch|' /etc/elasticsearch/elasticsearch.yml
sudo sed -i 's|^#path.logs: /path/to/logs|path.logs: /var/log/elasticsearch|' /etc/elasticsearch/elasticsearch.yml
sudo sed -i 's/^#network.host: 192.168.0.1/network.host: 0.0.0.0/' /etc/elasticsearch/elasticsearch.yml
sudo sed -i 's/^#http.port: 9200/http.port: 9200/' /etc/elasticsearch/elasticsearch.yml
sudo sed -i '$adiscovery.type: single-node' /etc/elasticsearch/elasticsearch.yml
验证配置是否正确
grep -E 'cluster.name:|node.name:|path.data:|path.logs:|network.host:|http.port:|discovery.type:' /etc/elasticsearch/elasticsearch.yml
配置之前的:
配置以后得:
- 更改 Elasticsearch 数据和日志目录的所有权:
sudo chown -R elasticsearch:elasticsearch /var/lib/elasticsearch
sudo chown -R elasticsearch:elasticsearch /var/log/elasticsearch
- 关闭防火墙:
在生产环境中,我们建议您配置防火墙以允许特定的端口和 IP 地址。但是,如果您希望在测试环境中关闭防火墙,请按照以下步骤操作:
sudo systemctl stop firewalld
sudo systemctl disable firewalld
6.启动并启用 Elasticsearch 服务:
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
7.确认 Elasticsearch 是否正在运行:
curl -X GET 'http://localhost:9200'
扩展(可选)
快速安装中文分词器
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.7/elasticsearch-analysis-ik-7.17.7.zip
恭喜,安装成功了!根据提示,请重启 Elasticsearch 以激活任何已安装的插件。
sudo systemctl restart elasticsearch
扩展(可选)
设置用户密码
要启用 X-Pack 安全特性并设置密码
在 Elasticsearch 配置文件/etc/elasticsearch/elasticsearch.yml 中添加以下行:
xpack.security.enabled: true
重启 Elasticsearch:
sudo systemctl restart elasticsearch
使用 elasticsearch-setup-passwords 命令设置密码:
sudo /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
Spring Boot连接
spring.data.elasticsearch.cluster-nodes=localhost:9200
spring.data.elasticsearch.cluster-name=my_cluster
spring.data.elasticsearch.username=elastic
spring.data.elasticsearch.password=刚刚设置的密码
改完以后kibana配置文件也要改
然后重启kibana
sudo systemctl restart kibana
重启以后需要登录es的账号和密码