1. 首页
  2. >
  3. 编程技术
  4. >
  5. Java

ElasticSearch安装详解及采坑

ElasticSearch是一个分布式的,高性能,高可用的,可伸缩的搜索和分析系统

(1)可以作为大型分布式集群(数百台服务器)技术,处理PB级的数据,服务大公司;也可以运行在单机上服务于小公司

(2)Elasticsearch不是什么新技术,主要是将全文检索、数据分析以及分布式技术,合并在了一起,才形成了独一无二的ES:lucene(全文检索),商用的数据分析软件,分布式数据库

(3)对用户而言,是开箱即用的,非常简单,作为中小型应用,直接3分钟部署一下ES,就可以作为生产环境的系统来使用了,此时的场景是数据量不大,操作不是太复杂

(4)数据库的功能面对很多领域是不够用的(事务,还有各种联机事务型的操作);

特殊的功能,比如全文检索,同义词处理,相关度排名,复杂数据分析,海量数据的近实时处理,Elasticsearch作为传统数据库的一个补充,提供了数据库所不能提供的很多功能

下载:

wget  https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.1.0.tar.gz

解压:

tar -zxvf elasticsearch-6.1.0.tar.gz -C ~/training/

修改配置文件:config/elasticsearch.yml,需要修改的没有多少:

cluster.name: william
node.name: node-1
node.attr.rack: r1
path.data: /trainging/elasticsearch-6.1.0/data
path.logs: /trainging/elasticsearch-6.1.0/logs
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: xxxxx
http.port: 9200
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true

每个参数的具体意思如下,转自别人的博客

elasticsearch的config文件夹里面有两个配置文 件:elasticsearch.yml和logging.yml,第一个是es的基本配置文件,第二个是日志配置文件,es也是使用log4j来记录日 志的,所以logging.yml里的设置按普通log4j配置文件来设置就行了。下面主要讲解下elasticsearch.yml这个文件中可配置的 东西。

cluster.name: elasticsearch
配置es的集群名称,默认是elasticsearch,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群。

node.name: "Franz Kafka"
节点名,默认随机指定一个name列表中名字,该列表在es的jar包中config文件夹里name.txt文件中,其中有很多作者添加的有趣名字。

node.master: true
指定该节点是否有资格被选举成为node,默认是true,es是默认集群中的第一台机器为master,如果这台机挂了就会重新选举master。

node.data: true
指定该节点是否存储索引数据,默认为true。

index.number_of_shards: 5
设置默认索引分片个数,默认为5片。

index.number_of_replicas: 1
设置默认索引副本个数,默认为1个副本。

path.conf: /path/to/conf
设置配置文件的存储路径,默认是es根目录下的config文件夹。

path.data: /path/to/data
设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开,例:
path.data: /path/to/data1,/path/to/data2

path.work: /path/to/work
设置临时文件的存储路径,默认是es根目录下的work文件夹。

path.logs: /path/to/logs
设置日志文件的存储路径,默认是es根目录下的logs文件夹

path.plugins: /path/to/plugins
设置插件的存放路径,默认是es根目录下的plugins文件夹

bootstrap.mlockall: true
设置为true来锁住内存。因为当jvm开始swapping时es的效率 会降低,所以要保证它不swap,可以把ES_MIN_MEM和ES_MAX_MEM两个环境变量设置成同一个值,并且保证机器有足够的内存分配给es。 同时也要允许elasticsearch的进程可以锁住内存,linux下可以通过`ulimit -l unlimited`命令。

network.bind_host: 192.168.0.1
设置绑定的ip地址,可以是ipv4或ipv6的,默认为0.0.0.0。


network.publish_host: 192.168.0.1
设置其它节点和该节点交互的ip地址,如果不设置它会自动判断,值必须是个真实的ip地址。

network.host: 192.168.0.1
这个参数是用来同时设置bind_host和publish_host上面两个参数。

transport.tcp.port: 9300
设置节点间交互的tcp端口,默认是9300。

transport.tcp.compress: true
设置是否压缩tcp传输时的数据,默认为false,不压缩。

http.port: 9200
设置对外服务的http端口,默认为9200。

http.max_content_length: 100mb
设置内容的最大容量,默认100mb

http.enabled: false
是否使用http协议对外提供服务,默认为true,开启。

gateway.type: local
gateway的类型,默认为local即为本地文件系统,可以设置为本地文件系统,分布式文件系统,hadoop的HDFS,和amazon的s3服务器,其它文件系统的设置方法下次再详细说。

gateway.recover_after_nodes: 1
设置集群中N个节点启动时进行数据恢复,默认为1。

gateway.recover_after_time: 5m
设置初始化数据恢复进程的超时时间,默认是5分钟。

gateway.expected_nodes: 2
设置这个集群中节点的数量,默认为2,一旦这N个节点启动,就会立即进行数据恢复。

cluster.routing.allocation.node_initial_primaries_recoveries: 4
初始化数据恢复时,并发恢复线程的个数,默认为4。

cluster.routing.allocation.node_concurrent_recoveries: 2
添加删除节点或负载均衡时并发恢复线程的个数,默认为4。

indices.recovery.max_size_per_sec: 0
设置数据恢复时限制的带宽,如入100mb,默认为0,即无限制。

indices.recovery.concurrent_streams: 5
设置这个参数来限制从其它分片恢复数据时最大同时打开并发流的个数,默认为5。

discovery.zen.minimum_master_nodes: 1
设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)

discovery.zen.ping.timeout: 3s
设置集群中自动发现其它节点时ping连接超时时间,默认为3秒,对于比较差的网络环境可以高点的值来防止自动发现时出错。

discovery.zen.ping.multicast.enabled: false
设置是否打开多播发现节点,默认是true。

discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]
设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点。

下面是一些查询时的慢日志参数设置
index.search.slowlog.level: TRACE
index.search.slowlog.threshold.query.warn: 10s
index.search.slowlog.threshold.query.info: 5s
index.search.slowlog.threshold.query.debug: 2s
index.search.slowlog.threshold.query.trace: 500ms

index.search.slowlog.threshold.fetch.warn: 1s
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug:500ms
index.search.slowlog.threshold.fetch.trace: 200ms

启动 bin/elasticsearch

错误:Error: Could not find or load main class org.elasticsearch.tools.JavaVersionChecker。 不要放在root目录下即可Caused by: java.lang.RuntimeException: can not run elasticsearch as root. 不能使用root用户

所以创建一个用户,并将elasticsearch copy到不是root的目录下:

[root@VM_0_11_centos training]# adduser william
[root@VM_0_11_centos training]# cp -a elasticsearch-6.1.0/ /trainging/
[root@VM_0_11_centos training]# cd /trainging/
[root@VM_0_11_centos trainging]# chown -R william:william elasticsearch-6.1.0/

但是又出现了错误:

[william@VM_0_11_centos elasticsearch-6.1.0]$ bin/elasticsearch
which: no java in (/root/training/jdk1.8.0_161/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
could not find java; set JAVA_HOME or ensure java is in PATH

因为新创建的这个用户没有权限访问jdk中的命令,把安装在root目录下的jdk移出到根目录下,并重新配置下环境变量即可

启动又出错:

 max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

原因是用户拥有的内存太小:切换到root用户 修改一下

sysctl -w vm.max_map_count=262144

查看: sysctl -a|grep vm.max_map_count永久解决办法:

在   /etc/sysctl.conf文件最后添加一行
vm.max_map_count=262144

在另一台机器上报了一个这么的错误max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]解决方法:修改/etc/security/limits.conf文件,增加配置,用户退出后重新登录生效

*               soft    nofile          65536
*               hard    nofile          65536

启动起来就可以访问了,在浏览器里面输入你的ip:9200如果访问不了,看下你的防火墙关了么:

systemctl status firewalld查看当前防火墙状态。
 systemctl stop firewalld关闭当前防火墙。
 systemctl disable firewalld开机防火墙不启动。

终于可以访问了:

为了看起来好看,我们安装插件 ElasticSearch-header

首先需要安装 npm

yum install npm

将ElasticSearch-head 下载到本地,它是github的开源项目:

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install

安装的过程中又报错:

 Failed at the phantomjs-prebuilt@2.1.16 install script 'node install.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.

看样子是nodejs 没安装,得安装nodejs

 wget https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz
  tar xf node-v12.16.1-linux-x64.tar.xz 

然后添加环境变量,继续安装ElasticSearch-header: npm install

报错:

npm ERR! Failed at the phantomjs-prebuilt@2.1.16 install script.

解决方法是:

npm install phantomjs-prebuilt@2.1.14 --ignore-scripts

好了 总算安装成功了:

+ phantomjs-prebuilt@2.1.14
added 75 packages from 69 contributors, removed 5 packages and audited 1751 packages in 21.413s
3 packages are looking for funding
  run `npm fund` for details
found 50 vulnerabilities (21 low, 8 moderate, 21 high)
  run `npm audit fix` to fix them, or `npm audit` for details

启动:

npm run start

实际上完全没有必要折腾这么多,可以直接Chrome浏览器下载个扩展插件装Chome浏览器里就可以,效果是一样的,还简单:

ElasticSearch安装详解及采坑

在这里插入图片描述

下载地址:https://github.com/mobz/elasticsearch-head/blob/master/crx/es-head.crx下载之后改为rar为后缀的文件之后解压,在Chrome浏览器里添加已解压的扩展程序就可以了

单节点的ElasticSearch就算搭建好了,该踩的坑一个没拉下!