一、安装Elasticsearch
1、下载
https://www.elastic.co/cn/downloads/past-releases
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.1.1-linux-x86_64.tar.gz
2、解压
tar -zxvf elasticsearch-7.1.1-linux-x86_64.tar.gz
mv elasticsearch-7.1.1 /usr/local
3、修改虚拟机配置
cd /usr/local/elasticsearch-7.1.1
vim config/jvm.options
-Xms1G -Xmx1G
根据机器配置修改为:
-Xms256M -Xmx256M
4、修改打开连接数
vim /etc/security/limits.conf
elsearch soft nproc 65535 elsearch hard nproc 65535 elsearch soft nofile 65535 elsearch hard nofile 65535
如不修改可能出现如下错误:
max number of threads [3700] for user [ubuntu] is too low, increase to at least [4096]
5、修改环境变量(需重启)
vim /etc/sysctl.conf
vm.max_map_count=262144 sysctl -p
如不修改可能出现如下错误:
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
6、修改配置文件
vim config/elasticsearch.yml
cluster.name: my-application node.name: node-1 path.data: /mnt/elsearch/data path.logs: /mnt/elsearch/logs bootstrap.memory_lock: false bootstrap.system_call_filter: false network.host: 127.0.0.1 http.port: 9200 cluster.initial_master_nodes: ["node-1"]
7、新建普通用户
groupadd elsearch useradd elsearch -g elsearch -m -s /bin/bash echo elsearch:password | chpasswd elsearch mkdir -p /mnt/elsearch/data mkdir -p /mnt/elsearch/logs chown -R elsearch:elsearch /mnt/elsearch chown -R elsearch:elsearch /usr/local/elasticsearch-7.1.1 su elsearch
8、启动
./bin/elasticsearch
后台启动
./bin/elasticsearch -d
二、安装分词库
1、下载对应版本源码
https://github.com/medcl/elasticsearch-analysis-ik/releases
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.1.1/elasticsearch-analysis-ik-7.1.1.zip
2、解压至对应目录
mkdir /usr/local/elasticsearch-7.1.1/plugins/ik unzip elasticsearch-analysis-ik-7.1.1.zip -d /usr/local/elasticsearch-7.1.1/plugins/ik chown -R elsearch:elsearch /usr/local/elasticsearch-7.1.1
3、重启elasticsearch使插件生效
4、创建一个索引
curl -XPUT http://127.0.0.1:9200/index?pretty=true
5、创建一个映射
curl -XPOST http://127.0.0.1:9200/index/_mapping?pretty=true -H 'Content-Type:application/json' -d' { "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } }'
6、索引一些文档
curl -XPOST http://127.0.0.1:9200/index/_doc/1?pretty=true -H 'Content-Type:application/json' -d' {"content":"美国留给伊拉克的是个烂摊子吗"} '
curl -XPOST http://127.0.0.1:9200/index/_doc/2?pretty=true -H 'Content-Type:application/json' -d' {"content":"公安部:各地校车将享最高路权"} '
curl -XPOST http://127.0.0.1:9200/index/_doc/3?pretty=true -H 'Content-Type:application/json' -d' {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"} '
curl -XPOST http://127.0.0.1:9200/index/_doc/4?pretty=true -H 'Content-Type:application/json' -d' {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"} '
7、查询
curl -XPOST http://127.0.0.1:9200/index/_doc/_search?pretty -H 'Content-Type:application/json' -d' { "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["", ""], "post_tags" : ["", ""], "fields" : { "content" : {} } } } '
8、添加服务脚本
Centos:
vim /usr/lib/systemd/system/elasticsearch.service
Ubuntu:
vim /lib/systemd/system/elasticsearch.service
脚本内容:
[Unit] Description=The Elasticsearch server After=network.target [Service] Type=simple User=elsearch WorkingDirectory=/usr/local/elasticsearch-7.10.1 ExecStart=/usr/local/elasticsearch-7.10.1/bin/elasticsearch ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID Restart=on-failure TimeoutSec=1min LimitCORE=infinity LimitNOFILE=65536 LimitNPROC=65536 LimitMEMLOCK=infinity PrivateTmp=true [Install] WantedBy=multi-user.target
重新加载systemd配置:
systemctl daemon-reload
配置开机启动:
systemctl enable elasticsearch.service
服务管理:
#启动 systemctl start elasticsearch.service #停止 systemctl stop elasticsearch.service #重启 systemctl restart elasticsearch.service
服务启动失败排查:
#查看某个Unit的日志 journalctl -u elasticsearch.service journalctl -u elasticsearch.service --since today #实时滚动显示某个Unit的最新日志 journalctl -u elasticsearch.service -f
三、安装kibana
1、下载
https://www.elastic.co/cn/downloads/past-releases
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.1.1-linux-x86_64.tar.gz
2、解压至对应目录
tar -zxvf kibana-7.1.1-linux-x86_64.tar.gz mv kibana-7.1.1-linux-x86_64 /usr/local/kibana-7.1.1 cd /usr/local/kibana-7.1.1
3、修改配置文件
vim config/kibana.yml
#对外服务监听端口 server.port: 5601 #绑定可以访问5601端口服务的IP地址,0.0.0.0表示任何地址在没有防火墙限制的情况下都可以访问,生产环境别这样设置,不安全。 server.host: “0.0.0.0” #默认值为主机名称,表示kibana实例绑定的主机,可以是IP地址或者主机名称. server.name: “es-master” #用来处理ES请求的服务URL elasticsearch.url: “http://192.168.1.1:9200” #用来控制证书的认证,可选的值为full,none,certificate。此处由于没有证书,所以设置为null,否则启动会提示错误. elasticsearch.ssl.verificationMode: none #开启中文界面 i18n.locale: "zh-CN"
4、运行kibana
./bin/kibana
四、安装elasticsearch-head插件
1、安装nodejs及npm
apt install nodejs apt install npm
# 如果速度很慢,可以替换为阿里源
npm config set registry https://registry.npm.taobao.org npm config get registry
2、下载head安装包
wget https://github.com/mobz/elasticsearch-head/archive/master.zip unzip master.zip -d /mnt/elsearch mv /mnt/elsearch/elasticsearch-head-master /mnt/elsearch/elasticsearch-head
3、安装grunt
cd /mnt/elsearch/elasticsearch-head npm install -g grunt --save-dev npm install grunt -version # 如果没提示可以不操作 npm audit fix
4、修改head链接地址
vim Gruntfile.js
watch: { "scripts": { files: ['src/**/*', 'test/spec/*' ], tasks: ['default'], options: { spawn: false } }, "grunt": { files: [ 'Gruntfile.js' ] } }, connect: { server: { options: { port: 9100, hostname: '*', # 添加此行,此注释需要删除 base: '.', keepalive: true } } } });
vim _site/app.js
把
4360 this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
改为:
4360 this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.1.1:9200";
注:192.168.1.1为服务器IP
5、修改elasticsearch配置
vim /usr/local/elasticsearch-7.1.1/config/elasticsearch.yml
最下方添加
# 是否支持跨域 http.cors.enabled: true # *表示支持所有域名 http.cors.allow-origin: "*"
6、启动elasticsearch-head
grunt server
后台
grunt server &
五、注意
1、安全相关
https://www.elastic.co/cn/blog/getting-started-with-elasticsearch-security