Elasticsearch5.5 安裝與配置


 
 
一、Elastic基本功能
Elasticsearch 是一個實時分布式搜索和分析引擎,一般用於全文搜索、結構化搜索,分析或者三者混用。
它的底層是基於Apache Lucene(TM)的開源搜索引擎,但是lucene只是一個庫,需要java開發然后集成到應用。
 
Elasticsearch的特性描述:
分布式實時文件存儲,每個字段都能被索引並且可以被搜索
分布式的實時分析搜索引擎
可以擴展到上百台服務器,處理PB級結構化或者非結構化數據
 
二、Elastic安裝准備
  系統准備:
  CentOS 7.2
  jdk8
  elasticsearch 5.5
 
  1.首先需要java環境,需要安裝jdk8,下載對應的tar包
 

[root@master local]# ls

bin jdk-8u152-linux-x64.tar.gz lib share etc lib64 mysql src games libexec qcloud include
[root@master local]# pwd
/usr/local
[root@master local]# tar zxvf jdk-8u152-linux-x64.tar.gz
#解壓完成后,配置JAVA_HOME的環境變量
[root@master ~]# vim /etc/profile
#我一般是習慣放在文件最后
#===============JAVA_HOME======================
#這些環境變量配置的就是剛剛tar包解壓之后的目錄下
export JAVA_HOME=/usr/local/jdk1.8.0_152
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
"/etc/profile" 83L, 1972C written
 
[root@master ~]# source /etc/profile
#查看一下版本是否正確
[root@master ~]# java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
=================================================
到目前為止,jdk8安裝完畢
 
  2.安裝elastic 下載解壓即可
--2018-03-27 11:29:36-- https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
Resolving artifacts.elastic.co (artifacts.elastic.co)... 184.73.245.233, 184.73.156.41, 54.235.82.130, ...
Connecting to artifacts.elastic.co (artifacts.elastic.co)|184.73.245.233|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 33511694 (32M) [application/zip]
Saving to: ‘elasticsearch-5.5.1.zip’
 
100%[===============================================================================>] 33,511,694 2.14MB/s in 36s
 
2018-03-27 11:30:15 (900 KB/s) - ‘elasticsearch-5.5.1.zip’ saved [33511694/33511694]
 
[root@master ~]# ls
all.sql anaconda-ks.cfg elasticsearch-5.5.1.zip MHA-Manager.qcow2 zhou
[root@master ~]# mv elasticsearch-5.5.1.zip /usr/local/
[root@master ~]# cd /usr/local/
[root@master local]# ls | egrep ela
elasticsearch-5.5.1.zip
[root@master local]# unzip elasticsearch-5.5.1.zip
[root@master local]# cd elasticsearch-5.5.1/
 
#=======================================================
#啟動過程中出現的錯誤:
1》errno12 虛擬機內存不足
[root@master elasticsearch-5.5.1]# ./bin/elasticsearch
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/local/elasticsearch-5.5.1/hs_err_pid21903.log
[root@master elasticsearch-5.5.1]# sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144
#解決辦法:
由於默認配置是2G內存,虛擬機內存不夠,可以更改為1G
[root@master elasticsearch-5.5.1]# cd config/
[root@master config]# ls
elasticsearch.yml jvm.options log4j2.properties
[root@master config]# pwd
/usr/local/elasticsearch-5.5.1/config
可以先free -m查看一下剩余的內存,再設置elastic的內存值
[root@master config]# vim jvm.options
-Xms1g
-Xmx1g
"jvm.options" 111L, 3064C
 
2》不能用root運行elastic java.lang.RuntimeException: can not run elasticsearch as root
解決方案:新建一個用戶elauser,並且將elasticsearch的目錄屬性全部更改成elauser
(如若目錄夾屬性未更改,有可能會出現如下錯誤)
#############################################
3》elasticsearch解壓后的目錄夾屬性需要一起更改
main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
解決方案:
[root@ELK local]# chown -R elauser:elauser ./elasticsearch-5.5.1
[elauser@ELK local]$ ll | egrep elasticsearch-5.5.1
drwxr-xr-x   7 elauser elauser      4096 Apr 10 15:20 elasticsearch-5.5.1
-rw-r--r--   1 root    root     33511694 Jul 25  2017 elasticsearch-5.5.1.zip
###############################################
 
切換到elauser,將java環境變量寫在~/.bash_profile,如下所示(或者直接在最開始配置環境變量的時候,寫在/etc/profile 這樣所有的用戶都能使用了)
[elauser@master bin]$ vim ~/.bash_profile
#===============JAVA_HOME======================
export JAVA_HOME=/usr/local/jdk1.8.0_152
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
#export PATH=$PATH:$JAVA_HOME/bin
#================================================
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$JAVA_HOME/bin
export PATH
"~/.bash_profile" 17L, 447C written
新開一個窗口
[elauser@master root]$ java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
[elauser@master root]$ cd /usr/local/elasticsearch-5.5.1/bin/
[elauser@master bin]$ ./elasticsearch
[2018-03-27T14:04:55,441][INFO ][o.e.n.Node ] [] initializing ...
[2018-03-27T14:04:55,639][INFO ][o.e.e.NodeEnvironment ] [BJ7MQFi] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [38.7gb], net total_space [49gb], spins? [unknown], types [rootfs]
 
#====================================================================
以上錯誤是參考該網址:https:// blog.csdn.net/qq_21387171/article/details/53577115
似乎啟動成功了。
 
#打開另外一個終端,測試一下
[root@master local]# curl ' http://localhost:9200/?pretty';
{
"name" : "BJ7MQFi",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "p6zkUDhtR26SDfiCKCu5Zg",
"version" : {
"number" : "5.5.1",
"build_hash" : "19c13d0",
"build_date" : "2017-07-18T20:44:24.823Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
 
#默認情況下elastic只能本地訪問,如需遠程訪問,則需要修改其配置文件elasticsearch.yml
[elauser@master config]$ pwd
/usr/local/elasticsearch-5.5.1/config
[elauser@master config]$ ls
elasticsearch.yml jvm.options log4j2.properties scripts
[elauser@master config]$ vim elasticsearch.yml
[elauser@master config]$ egrep 'network' elasticsearch.yml
#network.host: 192.168.0.1
network.host: 0.0.0.0
# For more information, consult the network module documentation.
 
#關閉elastic:
1.可以直接在啟動頁面Ctrl-C  
2.調用shutdown API來關閉 
curl -XPOST ' http://localhost:9200/_shutdown' 然而執行失敗了,發現shutdown這個api被去掉了。
3.簡單粗暴的話,不如殺進程
ps -ef | grep elastic;然后找到進程號,kill -9 進程號
4.網上有介紹head插件,點擊動作關停 即可
 
3.配置elasticsearch
######################## 去掉不必要的注釋 ###############################################
[elauser@ELK config]$ pwd
/usr/local/elasticsearch-5.5.1/config
[elauser@ELK config]$ cat elasticsearch.yml
# ======================== Elasticsearch Configuration =========================
# ---------------------------------- Cluster -----------------------------------
# Use a descriptive name for your cluster: 給集群定義一個name
cluster.name: demon
#
# ------------------------------------ Node ------------------------------------
# Use a descriptive name for the node: 給節點定義一個name
#node.name: node-1
node.name: elk-1
# ----------------------------------- Paths ------------------------------------
# Path to directory where to store the data (separate multiple locations by comma): 數據目錄夾
#path.data: /path/to/data
path.data: /usr/local/elasticsearch-5.5.1/data
#
# Path to log files: 日志目錄夾
path.logs: /usr/local/elasticsearch-5.5.1/logs
#
# ----------------------------------- Memory -----------------------------------
# Lock the memory on startup: 開啟時鎖定內存
bootstrap.memory_lock: true
# ---------------------------------- Network -----------------------------------
# Set the bind address to a specific IP (IPv4 or IPv6): 綁定ip
network.host: 0.0.0.0
# Set a custom port for HTTP: 設定端口
http.port: 9200
#================= add ===================  這里切記注意冒號后面有空格
http.cors.enabled: true
http.cors.allow-origin: "*"
#=========================================
##################################################################################
 
出現報錯[ERROR][o.e.b.Bootstrap ] [elk-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked
 
#查一下日志文件
[root@ELK logs]# pwd
/usr/local/elasticsearch-5.5.1/logs
[root@ELK logs]# more demon.log
 
#解決辦法:
[root@ELK logs]# egrep 'unlimit' /etc/security/limits.conf
* soft memlock unlimited
* hard memlock unlimited
[root@ELK logs]# ulimit -l unlimited
#然后還需要新開一個窗口才會讀取到這個配置文件
 
#然后看一下9200端口是否啟動了,ok就表示啟動成功了
[root@ELK logs]# netstat -tplan | grep 9200
tcp 0 0 0.0.0.0:9200 0.0.0.0:* LISTEN 10881/java
 
#瀏覽器訪問也可以測試一下
[root@ELK ~]# curl  http://127.0.0.1:9200
{
"name" : "elk-1",
"cluster_name" : "demon",
"cluster_uuid" : "OCxZx2K5Q_C7u0jPxWvQvw",
"version" : {
"number" : "5.5.1",
"build_hash" : "19c13d0",
"build_date" : "2017-07-18T20:44:24.823Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
 
4.與elasticsearch進行交互
#利用API來查看一下狀態
[root@ELK ~]# curl -i -XGET 'localhost:9200/_count?pretty'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 95
{
"count" : 0,
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
}
}
 
5.安裝插件
#安裝elasticsearch-head插件
[root@ELK ~]# yum install -y npm
[root@ELK ~]# yum install -y git
[root@ELK ~]# git clone git:// github.com/mobz/elasticsearch-head.git
Cloning into 'elasticsearch-head'...
remote: Counting objects: 4224, done.
remote: Total 4224 (delta 0), reused 0 (delta 0), pack-reused 4224
Receiving objects: 100% (4224/4224), 2.16 MiB | 88.00 KiB/s, done.
Resolving deltas: 100% (2329/2329), done.
#這個如若服務器的gpgcheck選項沒關,是一定要執行的。目的是導入elasticsearch官網的公鑰,install package的時候是需要密鑰檢測的
[root@ELK elasticsearch-head]# rpm --import  https://artifacts.elastic.co/GPG-KEY-elasticsearch
[elauser@ELK local]$ ll | egrep ela
drwxr-xr-x 9 elauser elauser 4096 Apr 10 15:39 elasticsearch-5.5.1
-rw-r--r-- 1 root root 33511694 Jul 25 2017 elasticsearch-5.5.1.zip
drwxr-xr-x 7 elauser elauser 4096 Apr 11 11:55 elasticsearch-head
[elauser@ELK local]$ cd elasticsearch-head/
[elauser@ELK elasticsearch-head]$ ls
Dockerfile Gruntfile.js LICENCE proxy src
Dockerfile-alpine grunt_fileSets.js package.json README.textile test
elasticsearch-head.sublime-project index.html plugin-descriptor.properties _site
[elauser@ELK elasticsearch-head]$ pwd
/usr/local/elasticsearch-head
 
[elauser@ELK elasticsearch-head]$ npm install
 
#====================================
#出現報錯1:
[elauser@ELK elasticsearch-head]$ npm install
npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.so.10 not defined in file libssl.so.10 with link time reference
#解決方案:
[root@ELK ~]# yum update openssl -y
 
#出現報錯2:
[elauser@ELK elasticsearch-head]$ npm install
> phantomjs-prebuilt@2.1.16 install /usr/local/elasticsearch-head/node_modules/phantomjs-prebuilt
> node install.js
PhantomJS not found on PATH
Saving to /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Receiving...
  [==--------------------------------------] 5%
Error making request.
Error: read ECONNRESET
    at exports._errnoException (util.js:1020:11)
    at TLSWrap.onread (net.js:568:26)
#解決方案:
[root@ELK elasticsearch-head]# npm config set registry= http://registry.npmjs.org
#==========================================
 
啟動插件之前,還需要修改一下配置文件(如果那個add部分你之前沒添加的話,這里就要加一下)
改完注意重啟elasticsearch(如若重啟歐報錯,記得檢查一下配置文件是否正確)

[elauser@ELK elasticsearch-head]$ npm run start

> elasticsearch-head@0.0.0 start /usr/local/elasticsearch-head
> grunt server
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
 
#檢查一下端口,是否正常運行了
[root@ELK ~]# netstat -tplan | grep 9200
tcp 0 0 0.0.0.0:9200 0.0.0.0:* LISTEN 16538/java
[root@ELK ~]# netstat -tplan | grep 9100
tcp 0 0 0.0.0.0:9100 0.0.0.0:* LISTEN 16747/grunt
 
 
#有兩個配置文件可以參考一下,也可以不更改

 

#這個不需要更改,但是這里改了之后忘記改回去了

 

#瀏覽器訪問一下

 

看到如上集群健康就ok了。
 
 
三、基本概念
  3.1 node、cluster
  elastic本質是一個分布式數據庫。一個節點node就是運行一個elastic實例,集群cluster就是一組具有相同cluster.name的節點集合,協同工作,共享數據並且提供故障轉移和擴展的功能。
  3.2 index
  elastic會對所有的字段進行索引,處理之后寫入一個反向索引(Inverted Index)。查找數據的時候直接查找該索引。elastic數據管理的頂層單位就叫做index(索引),也稱作數據庫,每個index的名字必須小寫。
#查看當前節點的所有index
[elauser@master bin]$ curl -X GET ' http://localhost:9200/_cat/indices?v';
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
#新建index(向elastic發出一個put請求,新建一個weather的index)
[elauser@master bin]$ curl -X PUT 'localhost:9200/weather'
{"acknowledged":true,"shards_acknowledged":true}
返回的JSON對象中,acknowledged字段表示操作成功
#刪除index(把上面新建的weather index 刪除掉)
[elauser@master bin]$ curl -X DELETE 'localhost:9200/weather'
{"acknowledged":true}
 
3.3 document
  index里面的單條記錄被稱為document。同一個index里面的document最好保持相同的schema,利於搜索。
  document使用JSON格式表示:
{
    “user”:“張三”
    “title”:“工程師”
    “desc”:“數據庫管理”
}
3.4 type
  type是一個邏輯分組,用來區分document。不同的type應該有相似的schema。性質完全不同的數據應該存成兩個index。
#查看當前每個index所包含的type
[elauser@master bin]$ curl 'localhost:9200/_mapping?pretty=true'
{ }
 
四、中文分詞插件ik (這里選擇ik)或者smartcn
 
  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM