elasticsearch环境搭建及起步

ElasticSearch 概述

ElasticSearch是一个高可扩展的开源的全文搜索分析引擎。

  • 它允许你快速的存储、搜索和分析大量数据。ElasticSearch通常作为后端程序,为需要复杂查询的应用提供服务。
  • Elasticsearch是一个基于Lucene的开源分布式搜索引擎,具有分布式多用户能力。Elasticsearch是用java开发,提供Restful接口,能够达到实时搜索、高性能计算;同时Elasticsearch的横向扩展能力非常强,不需要重启服务,基本上达到了零配置。

ElasticSearch 安装

  1. 在VMware上安装centos7

  2. 安装vim,输入yum install -y vim

  3. 安装git,输入:yum install -y git

  4. 安装jdk1.8+

    • 查看CentOS自带JDK是否已安装,输入:yum list installed |grep java

    • 若有自带安装的JDK,卸载CentOS系统自带Java环境。

    • 卸载JDK相关文件,输入:yum -y remove java-1.8.0-openjdk*

    • 卸载tzdata-java,输入:yum -y remove tzdata-java.noarch。当结果显示为Complete!即卸载完毕。

      注:“*”表示卸载掉java1.8.0的所有openjdk相关文件。

    • 新建目录mkdir /usr/java/

    • 将下载的jdk的rpm包上传至/usr/java文件夹中,修改权限,输入chmod +x jdk-8u111-linux-x64.rpm

    • 输入rpm -ivh jdk-8u101-linux-x64.rpm安装

    • 输入PATH="${PATH}:/usr/java/jdk1.8.0_111",修改环境变量

    • 查看刚安装的Java版本信息,输入:java -version 可查看Java版本。

  5. 打开xshell通过xftp将elasticsearch-2.1.1.tar.gz传到虚拟机/opt中,cd到opt目录,输入tar zxvf elasticsearch-2.1.1.tar.gz,解压至当前目录。

    注:不可以使用root用户启动elasticsearch,这是出于系统安全考虑设置的条件。由于ElasticSearch可以接收用户输入的脚本并且执行,为了系统安全考虑,建议创建一个单独的用户用来运行xkcoding

    如果是用root账号启动,会报以下错误

    1
    Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root. at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93) at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:144) at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:285) at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35) Refer to the log for complete error details.
  6. 创建xkcoding用户组及xkcoding用户

    1
    2
    groupadd xkcoding
    useradd xkcoding -g xkcoding -p elasticsearch-2.1.1
  7. 更改elasticsearch-2.1.1文件夹及内部文件的所属用户及组为xkcoding:xkcoding

    1
    2
    cd /opt
    chown -R xkcoding:xkcoding elasticsearch-2.1.1
  8. 切换到elsearch用户再启动

    1
    2
    3
    su xkcoding
    cd elasticsearch-2.1.1
    ./bin/elasticsearch -d #以守护进程的方式运行
  9. 打开一个新的终端进行测试

    1
    curl 'http://localhost:9200/?pretty'
  10. 查看返回数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    {
    "name" : "node-1",
    "cluster_name" : "togeek",
    "version" : {
    "number" : "2.1.1",
    "build_hash" : "40e2c53a6b6c2972b3d13846e450e66f4375bd71",
    "build_timestamp" : "2015-12-15T13:05:55Z",
    "build_snapshot" : false,
    "lucene_version" : "5.3.1"
    },
    "tagline" : "You Know, for Search"
    }
  11. 需要从windows本机访问centos7虚拟机,因此需要关闭防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下

    • 直接关闭防火墙

      1
      2
      systemctl stop firewalld.service #停止firewall
      systemctl disable firewalld.service #禁止firewall开机启动
    • 设置 iptables service

      1
      yum -y install iptables-services
    • 如果要修改防火墙配置,如增加防火墙端口9200

      1
      vi /etc/sysconfig/iptables
    • 增加规则

      1
      -A INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT
    • 保存退出后

      1
      2
      systemctl restart iptables.service #重启防火墙使配置生效
      systemctl enable iptables.service #设置防火墙开机启动
    • 最后重启系统使设置生效即可。

  12. 在windows的cmd中测试,返回json,测试通过!

-------------本文结束  感谢您的阅读-------------
xkcoding wechat
欢迎来我的公众号「xkcoding小凯扣丁」逛逛
o(╯□╰)o 赞助一杯咖啡 ~~