Java 8或者以上版本,并配置好环境变量,关于如何在CentOS 7下安装jdk及配置环境变量这里不做赘述。
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz
下载完ES的源码包后,使用tar命令解压安装包,并复制到/opt/elasticsearch目录下,重命名为elasticsearch,如下所示:
#进入opt
cd /opt
#解压项目
tar -zxvf elasticsearch-6.5.4.tar.gz
#修改名称
mv elasticsearch-6.5.4 elasticsearch
#进入elasticsearch
cd /elasticsearch
#新建data
mkdir data
1、因为Elasticsearch默认不支持root用户启动,因此我们需要创建一个其他用户,如下所示:
groupadd esgroup
useradd esuser -g esgroup -p espassword
2、更改elasticsearch文件夹及内部文件的所属用户及组:
#返回opt目录
cd /opt
chown -R esuser:esgroup elasticsearch
#切换用户
su esuser
#进入elasticsearch目录
cd /opt/elasticsearch
#启动elasticsearch(后台运行:./bin/elasticsearch -d)
./bin/elasticsearch
启动成功
其他:
再次启动显示已杀死:
需要调整JVM的内存大小:
vi bin/elasticsearch
ES_JAVA_OPTS="-Xms512m -Xmx512m"
再次启动:启动成功
如果显示如下类似信息:
[INFO ][o.e.c.r.a.DiskThresholdMonitor] [ZAds5FP] low disk watermark [85%] exceeded on [ZAds5FPeTY-ZUKjXd7HJKA][ZAds5FP][/opt/elasticsearch-6.2.4/data/nodes/0] free: 1.2gb[14.2%], replicas will not be assigned to this node
需要清理磁盘空间。
如果启动成功,我们可以另一个xshell窗口使用:
#9200是按照es的默认端口,如果你改了端口,则对应你改的那个端口
curl 127.0.0.1:9200
返回如下结果表名启动成功:
{
"name" : "v2tczZJ",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "RpNwVDjZRJyG2IVwPdt3ig",
"version" : {
"number" : "6.5.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "d2ef93d",
"build_date" : "2018-12-17T21:17:40.758843Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
修改elasticsearch的配置文件,主要修改绑定IP与端口号,使用vim打开 /opt/elasticsearch/config/elasticsearch.yml,修改如下内容:
#---------------------Paths-----------------------------
path.data: /opt/elasticsearch/data
path.logs: /opt/elasticsearch/logs
#---------------------Networkd--------------------------
# 绑定本机IP地址或直接设置为0.0.0.0(实现远程访问)
network.host: 0.0.0.0
# 默认端口号为9200
http.port: 9200
#查看端口是否已开:提示 yes,表示开启;no表示未开启。
firewall-cmd --query-port=9200/tcp
#开启端口
firewall-cmd --add-port=9200/tcp --permanent
#配置立即生效
firewall-cmd --reload
按照上述命令安装时,可能会出现一些错误,这里总结一下常见的错误及解决办法:
错误 1:
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
原因:出于对root用户的安全保护,需要用其他用户组用户来启动。
解决方法:需要对elasticsearch安装目录进行用户授权。
chown -R esuser:esgroup elasticsearch
错误2:
Exception in thread "main" 2017-11-12 12:17:55,776 main ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property ‘log4j2.debug‘ to show Log4j2 internal initialization logging.
2017-11-12 12:17:56,319 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
原因:elasticsearch.yml中的配置项的格式有问题
解决方案:请尽量保持冒号前面没空格,后面一个空格,不要用tab键
错误3:
ERROR: [3] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [3802] for user [elsearch] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
原因:系统限制用户的执行内存
解决方法:
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [3802] for user [elsearch] is too low, increase to at least [4096]
上述两个错误,需要修改系统安全限制配置文件/etc/security/limits.conf,如下所示:
vim /etc/security/limits.conf
在其中添加如下内容:
# End of file
esuser hard nofile 65536
esuser soft nofile 65536
* hard nproc 4096
* soft nproc 4096
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
该错误需要修改系统配置文件/etc/sysctl.conf,如下所示:
vim /etc/sysctl.conf
添加如下内容:
vm.max_map_count=655360
使用如下命令使配置生效:
sysctl -p
上述错误修改完配置文件以后最好重启一下系统,然后再尝试启动elasticsearch。
git上面下载ik分词器的压缩包,地址:
https://github.com/medcl/elasticsearch-analysis-ik/releases
与ES版本匹配的6.5.4版本:
下载后上传到Linux上并解压:
#进入elasticsearch下的plugins
cd /opt/elasticsearch/plugins/
#新建ik文件夹
mkdir ik
#进入ik
cd ik
#将项目上传到ik目录上进行解压
unzip elasticsearch-analysis-ik-6.5.4.zip
#删除压缩包(否则软件会把这个压缩包也当成插件)
rm -rf elasticsearch-analysis-ik-6.5.4.zip
没有unzip
命令: yum list | grep zip/unzip #获取安装列表
安装命令: yum install zip #提示输入时,请输入y;
安装命令:yum install unzip #提示输入时,请输入y;
重启es项目:我们可以看到加载插件中多了一个analysis-ik,代表安装成功