Elasticsearch5.5 安裝與配置

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

[root@master local]# lshtml

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 startjava

> 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、基本概念
  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'
{ }
 
4、中文分詞插件ik (這裏選擇ik)或者smartcn
相關文章
相關標籤/搜索