项目需求:存大量的聊天信息,后期便于前端提取;
对比了MongoDB和Elasticsearch优劣,最终选定Elasticsearch。
一、Elasticsearch不能用root来启动,如果用root启动会报以下错误:
Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:94)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:160)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:286)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
Refer to the log for complete error details.
二、安装配置
1、解压
cd /opt/resources
tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz
mv elasticsearch-7.8.0 /usr/local/elastic/elasticsearch-7.8.0
2、修改elasticsearch.yml配置文件
vim /usr/local/elastic/elasticsearch-7.8.0/config/elasticsearch.yml
-------------------------------------------------------------------------------------------------------------------------
cluster.name: my-application #为集群提供一个名称
node.name: node-1 #此节点名称
path.data: /usr/local/elastic/path/to/data #数据存放的地址
path.logs: /usr/local/elastic/path/to/logs #日志存放地址
network.host: 0.0.0.0 #网络绑定这样设置就好了
cluster.initial_master_nodes: ["node-1"] #将es-node1设置为master节点
-------------------------------------------------------------------------------------------------------------------------
3、修改系统进程内存限制
vim /etc/security/limits.conf
-----------------------------------------------------------------------------------------------------------------------
添加数据
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
-----------------------------------------------------------------------------------------------------------------------
vim /etc/sysctl.conf
-----------------------------------------------------------------------------------------------------------------------
vm.max_map_count=262145 #(es需要262144,我们在基础上+1,保证它启动)
-----------------------------------------------------------------------------------------------------------------------
sysctl -p #刷新
4、创建用户
由于elasticsearch不允许root用户操作,所以需要建立子用户
useradd esuser #增加一个子用户
chown -R esuser:esuser /usr/local/elastic #赋权
su esuser #切换成子用户
#进入 es/bin命令启动
cd /usr/local/elastic/elasticsearch-7.8.0/bin
./elasticsearch -d #-d表示后台启动
5、校验
http:ip:9200 如果返回json字符串则安装成功
curl http://192.168.56.13:9200

6、配置运行内存
主要是用于记录,es默认的内存配置是1g,在实际的应用过程中很快就占满了。
官方记录的有两种方式,不过我更倾向于第一种
官方链接:https://www.elastic.co/guide/en/elasticsearch/reference/master/jvm-options.html
修改jvm.options
找到es安装目录下的这个文件 config/jvm.options
将原来
-Xms1g 改成 -Xms3g
-Xmx1g 改成 -Xmx3g
重新启动es即可
7、配置x-pack基础安全
单独写了一章:https://www.jianshu.com/p/40e35c6a32a4
8、配置开机启动
1、在 /etc/init.d 目录下,创建脚本文件 elasticsearch
vim /etc/init.d/elasticsearch
添加如下:
#!/bin/sh
#ckconfig: 2345 10 90
#description: elasticsearch
export JAVA_HOME=/usr/local/jdk
export JAVA_BIN=/usr/local/jdk/bin
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
case "$1" in
start)
su - es<<!
cd /usr/local/elastic/elasticsearch-7.8.0
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
stop)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
;;
restart)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
su - es<<!
cd /usr/local/elastic/elasticsearch-7.8.0
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
*)
echo "start|stop|restart"
;;
esac
exit $?
保存退出
2、在 /etc/init.d 目录下赋予新创建的elasticsearch文件执行权限
chmod 777 elasticsearch
3、添加到开机启动任务
chmod +x elasticsearch
chkconfig --add elasticsearch 【添加系统服务】
4、关闭和启动服务
service elasticsearch start 【启动】
service elasticsearch stop 【停止】
service elasticsearch restart 【重启】
5 、设置服务是否开机启动
chkconfig elasticsearch on 【开启】
chkconfig elasticsearch off 【关闭】
6、重启机器,检测elasticsearch是否自启
ps -ef | grep elasticsearch 【查看是否有es的进程】
结束进程命令用kill -9 进程ID;










网友评论