环境准备

由于elasticsearch由Java实现,所以需要Java环境,在之前博文中,我已经介绍了Java的安装,博文链接

创建用户和密码

elasticsticsearch不能用root用户去启动,否则会报错,所以在这里我将会单独创建一个名为elasticsearch的用户,去运行elasticsearch程序,当前用户是root,所以无需切换,直接运行如下命令:

[root@lnmp ~]# useradd elasticsearch
# 创建密码,这里会有两次密码输入提示,确保一致即可创建完成!
[root@lnmp ~]# passwd elasticsearch

下载和安装elasticsearch

[root@lnmp ~]# cd /var/soft
[root@lnmp soft]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.tar.gz
[root@lnmp soft]# sha1sum elasticsearch-5.5.2.tar.gz 
[root@lnmp soft]# tar xf elasticsearch-5.5.2.tar.gz

创建程序目录,并修改相应权限

[root@lnmp soft]# mkdir /usr/local/elasticsearch-5.5.2
[root@lnmp soft]# mv elasticsearch-5.5.2/* /usr/local/elasticsearch-5.5.2
# 修改属主为elasticsearch
[root@lnmp soft]# chown -R elasticsearch /usr/local/elasticsearch-5.5.2
[root@lnmp soft]# cd /usr/local/elasticsearch-5.5.2
[root@lnmp elasticsearch-5.5.2]# ls -l
total 244
drwxr-xr-x  2 elasticsearch root            4096 Sep 11 09:33 bin
drwxr-xr-x  3 elasticsearch root            4096 Sep 11 10:22 config
drwxrwxr-x  3 elasticsearch elasticsearch   4096 Sep 11 10:22 data
drwxr-xr-x  2 elasticsearch root            4096 Aug 14 20:35 lib
-rw-r--r--  1 elasticsearch root           11358 Aug 14 20:30 LICENSE.txt
drwxr-xr-x  2 elasticsearch root            4096 Sep 11 10:06 logs
drwxr-xr-x 13 elasticsearch root            4096 Aug 14 20:35 modules
-rw-r--r--  1 elasticsearch root          194187 Aug 14 20:35 NOTICE.txt
drwxr-xr-x  2 elasticsearch root            4096 Aug 14 20:35 plugins
-rw-r--r--  1 elasticsearch root            9549 Aug 14 20:30 README.textile  README.textile

elasticsearch内存配置

elasticsearch5.5版本默认是需要分配2G内存,否则启动时会报错,类似如下:

INFO: os::commit_memory(0x00000000ea660000, 362414080, 0) failed; error='Cannot allocate memory' (errno=12)

如果你的内存不够,打开并编辑/usr/local/elasticsearch-5.5.2/config/jvm.options文件,将文件开始位置的配置修改,则可以解决,例如我将2G的默认配置修改称为512M,只需要删除或者使用#注释掉原来配置再添加自己的配置项即可,类似如下:

# 注释掉的原来的2G配置
#-Xms2g
#-Xmx2g

# 修改称为512M内存大小
-Xms512m
-Xmx512m

登录elasticsearch用户,并将程序目录加入环境变量

[elasticsearch@lnmp ~]$ who -m
elasticsearch pts/2        2017-09-11 14:07

确保当前用户为elasticsearch后,编辑~/.bash_profile文件,在export $PATH之前加入如下行,保存退出即可:

export PATH=/usr/local/elasticsearch-5.5.2/bin:$PATH

使新增的环境变量生效

[elasticsearch@lnmp ~]$ source ~/.bash_profile

运行elasticsearch

[elasticsearch@lnmp ~]$ elasticsearch

# 省略.....
[2017-09-11T11:27:04,584][INFO ][o.e.n.Node               ] initialized
[2017-09-11T11:27:04,585][INFO ][o.e.n.Node               ] [Q6tSN9S] starting ...
[2017-09-11T11:27:04,769][INFO ][o.e.t.TransportService   ] [Q6tSN9S] publish_address {127.0.0.1:9300}, bound_addresses {127.0.0.1:9300}
[2017-09-11T11:27:04,826][WARN ][o.e.b.BootstrapChecks    ] [Q6tSN9S] max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[2017-09-11T11:27:04,827][WARN ][o.e.b.BootstrapChecks    ] [Q6tSN9S] max number of threads [1024] for user [elasticsearch] is too low, increase to at least [2048]
[2017-09-11T11:27:04,827][WARN ][o.e.b.BootstrapChecks    ] [Q6tSN9S] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2017-09-11T11:27:04,827][WARN ][o.e.b.BootstrapChecks    ] [Q6tSN9S] system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[2017-09-11T11:27:07,913][INFO ][o.e.c.s.ClusterService   ] [Q6tSN9S] new_master {Q6tSN9S}{Q6tSN9SYTnGsceAPmXQZZA}{iy1q-lT-SCSSp-gxeRflMQ}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2017-09-11T11:27:07,952][INFO ][o.e.h.n.Netty4HttpServerTransport] [Q6tSN9S] publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}
[2017-09-11T11:27:07,953][INFO ][o.e.n.Node               ] [Q6tSN9S] started
[2017-09-11T11:27:07,966][INFO ][o.e.g.GatewayService     ] [Q6tSN9S] recovered [0] indices into cluster_state

看到如上信息,并且命令行光标位于行首并不停闪动,表示启动成功!这里暂时不介绍后台运行。

测试elasticsearch

新开一个登录窗口,任何用户登录都行,这里需要使用到curl,如果没有,以centos为例自行yum安装即可,确保没问题后,最终执行如下命令:

[root@lnmp ~]# curl http://localhost:9200
{
  "name" : "Q6tSN9S",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "hV33j18ZSnaz9KvnLF5C7w",
  "version" : {
    "number" : "5.5.2",
    "build_hash" : "b2f0c09",
    "build_date" : "2017-08-14T12:33:14.154Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}

参考于官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/5.5/zip-targz.html