Linux安装配置ElasticSearch6.4.0、ElasticSearch-Head、ik_max_word详细步骤
一、安装JDK
1、下载jdk包并解压:
tar -zxvf jdk-8u201-linux-x64.tar.gz
2、设置环境变量
vi /etc/profile
在profile中添加如下内容:
#set java environment
export JAVA_HOME=/usr/local/jdk1.8.0_201
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATH
3、让修改生效
source /etc/profile
4、查看配置是否成功
java -version
二、安装ElasticSearch
1、下载安装包并解压
tar -zvxf elasticsearch-6.4.0.tar.gz
2、修改配置
vi /apprun/elasticsearch-6.4.0/config/elasticsearch.yml
取消以下配置的注释
cluster.name: my-application #集群的名称
node.name: node-1 #节点的名称
network.host: 172.xx.xx.xx #监听的ip地址,设置地址为本机地址,如果是0.0.0.0,则表示监听全部ip
在配置文件末尾增加:
#配置 elasticsearch 允许跨域访问
http.cors.enabled: true
http.cors.allow-origin: "*"
3、启动elasticsearch
elasticsearch不能以root账户启动,所以需新建一个用户启动
useradd es
chown -R es:es /apprun/elasticsearch-6.4.0/
切换到es用户下,启动elasticsearch
su es
source /etc/profile
./bin/elasticsearch
后台启动命令:
nohup ./bin/elasticsearch &
或
./bin/elasticsearch -d
查看是否启动成功:
访问:http://172.xx.xx.xx:9200/
出现以下信息则启动成功:
{
"name" : "node-1",
"cluster_name" : "my-application",
"cluster_uuid" : "rLnbUhbZSMGKjbU7ITMNRg",
"version" : {
"number" : "6.4.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "595516e",
"build_date" : "2018-08-17T23:18:47.308994Z",
"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出现的报错以及解决办法
1、org.elasticsearch.bootstrap.StartupException: ElasticsearchException[Failed to create native process factories for Machine Learning]; nested: FileNotFoundException[/tmp/elasticsearch.HrMK2JRR/controller_log_23671 (No such file or directory)];
可能是由于x-pack中的machine learning功能依赖于2.9以上版本GLIBC,环境中缺乏glibc库所致。
解决:
通过ldd命令查看controller的依赖:
ldd /apprun/elasticsearch-6.4.0/modules/x-pack-ml/platform/linux-x86_64/bin/controller
办法1:如果不需要使用machine learning功能,则可以在elasticsearch.yml中设置禁用,在config/elasticsearch.yml加入以下参数:
xpack.ml.enabled: false
办法2:安装或者升级glibc;如果使用centos4.3版本,则果断升级操作系统;
2、max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
虚拟内存太小
切换到root下,需要输入服务器登陆密码
su
password:
打开系统配置文件:
vi /etc/sysctl.conf
加入配置:
vm.max_map_count=262144
生效:
sysctl -p
三、安装中文分词器ik_max_word
1、到elasticsearch目录下,使用elasticsearch-plugin安装ik_max_word:
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
此时elasticsearch目录下出现analysis-ik
cd /apprun/elasticsearch-6.4.0/plugins/analysis-ik
2、重启elasticsearch,中文分词器生效
elasticsearch服务可通过kill命令关掉,再安装上述方法启动。
四、安装ElasticSearch-Head插件
因为安装 ElasticSearch-Head 需要使用到 npm 包管理器,所以需要提前安装好 NodeJS,安装好 nodejs 之后,再开始安装 ElasticSearch-Head 。
1、安装nodejs
(1)到NodeJs官网(https://nodejs.org/en/download/),复制下载链接当前node.js最新版本为10.15.3,折腾一下午一直报错,后来经过前端大牛鉴定,该版本有bug(心塞…)。果断换一个低版本安装,历史版本可以在这里找到。
(2)直接用命令下载安装包
cd /usr/local/
wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz
(3)解压
***.tar.xz格式文件需要两部解压步骤
xz -d node-v10.9.0-linux-x64.tar.xz
tar -xvf node-v10.9.0-linux-x64.tar
解压后的目录更名为node
mv node-v10.9.0-linux-x64 node
(4)配置环境目录,可全局使用npm命令
将安装目录/usr/local/node下的node和npm连接至/usr/bin/
ln -s /usr/local/node/node-v10.9.0-linux-x64/bin/node /usr/bin/node
ln -s /usr/local/node/node-v10.9.0-linux-x64/bin/npm /usr/bin/npm
验证一下
任意路径下查看版本号
node -v
npm -v
第一个输出版本号v10.9.0,第二个输出6.2.0,说明已正确安装。
2、安装 ElasticSearch-Head
(1)下载安装包并解压
unzip elasticsearch-head-master.zip
(2)
cd elasticsearch-head
npm install
若在 npm install 的过程中报错:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
解决办法:
npm install [email protected] --ignore-scripts
然后重新执行
npm install
(3)依赖安装完成后,需要修改一些配置
修改Gruntfile.js,允许外网访问 elasticsearch-head
vi elasticsearch-head/Gruntfile.js
找到 grunt.initConfig 中的 connect 配置,在 options 中添加 hostname: ‘*’,(注意,冒号后面有个空格)
connect: {
server: {
options: {
hostname: '*',
port: 9100,
base: '.',
keepalive: true
}
}
}
修改默认连接地址,配置 elasticsearch-head 监控的elasticsearch节点IP地址
vi elasticsearch-head/_site/app.js
找到 this.base_uri 的配置(可以通过搜索找到:vi命令行模式下输入 /this.base_uri)
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
将 localhost 修改成 elasticsearch 所在的服务器地址
至此 ElasticSearch 和 ElasticSearch-Head 已完成基本的配置,可以启动并查看效果
(1)启动ElasticSearch-Head
su es
cd elasticsearch-head
npm run start
若要后台运行则使用如下命令启动
nohup npm run start &
验证elasticsearch-head是否启动成功:
web访问:http://172.xx.xx.xx:9100/
出现如下界面:2、启动elasticsearch
cd /apprun/elasticsearch-6.4.0
./bin/elasticsearch -d
在页面点击连接,出现node-1,表示启动成功可以查看当前的端口情况
netstat -tunlp
目前9100、9200、9300端口正在运行,
9100端口:ElasticSearch-Head 占用端口
9200、9300端口:ElasticSearch 占用端口,9200是http协议的RESTful接口,9300是tcp通讯端口,集群间和TCPClient都走的它
注:这里一定要注意启动顺序
要保证elasticsearch未启动时启动head,验证head页面可以打开之后,再启动elasticsearch,然后刷新页面(或者点击连接),即可将head监控页面成功连接至elasticsearch服务。
若顺序错误elasticsearch-head会在运行中出现中断,只报一个词的提示:Aborted
查了很多资料,纠结了很久,大量尝试得出的经验教训,但还是不太清楚为什么会出现这种情况…
安装全部完成,以上。