centos7安装ElasticSearch6
发表于:2021-10-30 | 分类: elasticsearch
字数统计: 3.3k | 阅读时长: 15分钟 | 阅读量:

前言

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。
Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。
ElasticSearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。
Elasticsearch是一个高度可伸缩的开源全文搜索和分析引擎。它允许你以近实时的方式快速存储、搜索和分析大量的数据。它通常被用作基础的技术来赋予应用程序复杂的搜索特性和需求

安装配置JDK

为什么是JDK1.8?
在ElasticSearch6.2.4中提到:JDK版本必须为:1.8.0_131 以上
安装步骤请自己参考有关文档

版本

centos7
jdk1.8.202
mysql5.7
elasticsearch-6.2.4
elasticsearch-analysis-ik-6.2.4
kibana-6.2.4-linux-x86_64
logstash-6.2.4
logstash-input-jdbc-4.3.9
logstash-ouput-jdbc-5.4.0
mysql-connector-java-8.0.17

安装elasticsearch

下载

官网地址:下载连接



新建/usr/local/es文件夹,将elasticsearch压缩包上传到该目录下
或者在此目录下执行
elasticsearch-6.2.4下载

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

logstash-6.2.4下载

https://artifacts.elastic.co/downloads/logstash/logstash-6.2.4.tar.gz

kibana-6.2.4下载

https://artifacts.elastic.co/downloads/kibana/kibana-6.2.4-linux-x86_64.tar.gz

解压

tar -zxvf elasticsearch-6.2.4.tar.gz

创建用户

从5.0开始,ElasticSearch 安全级别提高了,不允许采用root帐号启动,所以我们要添加一个用户。

创建bigdata 用户组

groupadd bigdata

创建用户es

useradd es

为es用户设置密码es(密码过短会提示)

passwd es

将es用户添加到bigdata组

usermod -G bigdata es

设置sudo权限

visudo

找到root ALL=(ALL) ALL一行,添加es用户,如下

切换用户

su es

修改目录权限

更改elasticsearch-6.2.4文件夹以及内部文件的所属用户为es, 用户组组为bigdata,-R表示逐级(N层目录)
#注意:如果想创建软连接建议先为源文件夹设置所属用户,并使用root用户授权后再切换到es用户下创建软连接操作


将这个几个压缩包上传到此目录下,并用root用户解压

su root

使用root用户授权

sudo chown -R es:bigdata elasticsearch-6.2.4
sudo chown -R es:bigdata kibana-6.2.4-linux-x86_64
sudo chown -R es:bigdata logstash-6.2.4

切换到es用户下创建软连接操作(可以不做此操作)

root用户创建文件夹:/home/soft/elasticsearch

cd /home
mkdir soft
cd soft
mkdir elasticsearch

切换到es用户下

su es
sudo ln -s /usr/local/es/elasticsearch-6.2.4 /home/soft/elasticsearch/elasticsearch
sudo ln -s /usr/local/es/kibana-6.2.4-linux-x86_64 /home/soft/elasticsearch/kibana
sudo ln -s /usr/local/es/logstash-6.2.4 /home/soft/elasticsearch/logstash

ElasticSearch 配置

修改elasticsearch.yml

进入到elasticsearch的config目录下然后

vim elasticsearch.yml
#修改network.host和http.port
#network.host为当前服务器ip或者0.0.0.0
network.host: 0.0.0.0
http.port: 9200

#最下面添加
http.cors.enabled: true 
http.cors.allow-origin: "*"
node.master: true
node.data: true

启动

/usr/local/es/elasticsearch-6.2.4/bin/elasticsearch

后台启动

/usr/local/es/elasticsearch-6.2.4/bin/elasticsearch -d
curl http://192.168.1.127:9200

查看日志

tail -f /usr/local/es/elasticsearch-6.2.4/logs/elasticsearch.log 

端口

9300:Java程序访问的端口
9200:浏览器、postman访问的端口

启动报错解决

  1. Centos6 安装启动失败

    ERROR: [1] bootstrap checks failed
    [1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk

    解决方案 参考文档

    [payment@gameServer elasticsearch-6.2.4]$ vim config/elasticsearch.yml
    # ----------------------------------- Memory -----------------------------------
    #
    # Lock the memory on startup:
    #
    bootstrap.memory_lock: false
    bootstrap.system_call_filter: false
    #
    # Make sure that the heap size is set to about half the memory available
    # on the system and that the owner of the process is allowed to use this
    # limit.
    #
    # Elasticsearch performs poorly when the system is swapping the memory.
  2. ERROR: bootstrap checks failed

    max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
    max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]  

解决方案:Root用户修改

vim /etc/security/limits.conf 
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096  
  1. max number of threads [1024] for user [lish] likely too low, increase to at least [4096]
    解决方案:Root用户修改
vim /etc/security/limits.d/90-nproc.conf
*          soft    nproc     4096
root       soft    nproc     unlimited
  1. 最大虚拟内存区域vm.max_map_count [65530]可能太低,增加到至少[262144]
    解决方案:Root用户修改
    vim /etc/sysctl.conf

加载设置好的系统参数

sysctl -p

安装中文分词器(IK Analysis)

离线安装

如下地址下载最新包(自行检查对应版本号)

https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.4/elasticsearch-analysis-ik-6.2.4.zip

解压到/usr/local/es/elasticsearch-6.2.4/plugins安装目录下

在线安装(推荐):

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.4/elasticsearch-analysis-ik-6.2.4.zip

重启ElasticSearch服务

ps -ef|grep elasticsearch
./bin/elasticsearch -d && tail -f logs/elasticsearch.log

检查分词器

curl -XGET http://192.168.1.127:9200/_analyze?pretty -H 'Content-Type:application/json' -d'               
{
  "analyzer": "ik_smart",
  "text": "听说看这篇博客的哥们最帅、姑娘最美"
}'

解释(来源GitHub):
ik_max_word 和 ik_smart 什么区别?
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。

安装Kibana替代head

简介

Kibana是一个开源的分析与可视化平台,设计出来用于和Elasticsearch一起使用的。
你可以用kibana搜索、查看、交互存放在Elasticsearch索引里的数据,
使用各种不同的图表、表格、地图等kibana能够很轻易地展示高级数据分析与可视化。

下载

wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.4-linux-x86_64.tar.gz

解压、给用户授权

上面已经做过了此操作

修改配置文件

cd /usr/local/es/kibana-6.2.4-linux-x86_64
vim config/kibana.yml
#修改如下参数
#端口
server.port: 5601
#地址
server.host: "192.168.1.127"
#ElasticSearch地址
elasticsearch.url: "http://192.168.1.127:9200"

查看配置

grep '^[a-z]' config/kibana.yml

启动Kibana

./bin/kibana &

使用Kibana

浏览器访问 http://192.168.1.127:5601

测试检查集群健康状态(使用Dev Tools)

GET _cat/health?v

安装logstash

下载、解压

此步骤上面已做过

Logstash插件分类

inputs:输入

codecs:解码

filters:过滤

outputs:输出

logstash中支持少量的数据类型

bool 类型:debug => true
string类型: host => “hostname”
number类型: port => 5044
array类型: match => [“datetime” , “unitime”]
hash类型: options => { key1 => “value1”, key2 => “value2” }

在logstash-6.2.4/bin下新建一个目录config-mysql

进入logstash-6.2.4/bin

cd /usr/local/es/logstash-6.2.4/bin
./logstash -e 'input { stdin { } } output { stdout {} }'
mkdir config-mysql

判断是否安装gem

gem –v

安装gem

需要用root账号,或者sudo yum install gem

yum install gem

查看gem版本

gem -v

查看当前ruby仓库镜像

gem sources -l

用淘宝镜像替代国外的ruby仓库镜像

gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

替换镜像后查看当前ruby仓库镜像

gem sources -l

升级gem版本

gem update --system

升级完毕后查看gem版本

gem -v

镜像捆绑:linux命令

bundle config mirror.https://rubygems.org https://gems.ruby-china.com

如果没有bundle,就安装bundle

gem install bundler

在线安装插件logstash-input-jdbc-4.3.9

./logstash-plugin install logstash-input-jdbc

打包插件logstash-input-jdbc-4.3.9

命令失败可以多尝试几次就OK了

./logstash-plugin prepare-offline-pack --overwrite --output logstash-input-jdbc-4.3.9.zip logstash-input-jdbc

在线安装插件logstash-output-jdbc-5.4.0

./logstash-plugin install logstash-output-jdbc

打包插件logstash-output-jdbc-5.4.0

命令失败可以多尝试几次就OK了

./logstash-plugin prepare-offline-pack --overwrite --output logstash-output-jdbc-5.4.0.zip logstash-output-jdbc

查看logstash插件合集

./logstash-plugin list

查看logstash插件合集版本

./logstash-plugin list  --verbose

离线安装插件

上传zip包到指定目录下
我是放在了/usr/local/es/logstash-6.2.4/bin下面

/usr/local/es/logstash-6.2.4/bin/logstash-plugin install file:///usr/local/es/logstash-6.2.4/bin/logstash-output-jdbc-5.4.0.zip

上传MySQL驱动

添加配置文件

增量old_zeng.conf

input {  
    stdin {  
    }  
    jdbc {  
      # mysql 数据库链接,es为数据库名  
      jdbc_connection_string => "jdbc:mysql://192.168.1.124:3306/dwj_fast?serverTimezone=GMT"  
      # 用户名和密码  
      jdbc_user => "root"  
      jdbc_password => "123456"  
      # 驱动  
      jdbc_driver_library => "/usr/local/es/logstash-6.2.4/bin/config-mysql/mysql-connector-java-8.0.17.jar"
      # 驱动类名(8.0版本的驱动要加cj)  
      jdbc_driver_class => "com.mysql.cj.jdbc.Driver"  
      jdbc_paging_enabled => "true"  
	  #是否把列名变成小写
	  lowercase_column_names => false
      jdbc_page_size => "50000"  
      # 执行的sql 文件路径+名称  
	  statement_filepath => "/usr/local/es/logstash-6.2.4/bin/config-mysql/zeng.sql" 
      # 设置监听间隔  各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新  
      schedule => "* * * * *"  
      # 设定ES索引类型  
      type => "jdbc"
	  #增量更新
      use_column_value => true
      tracking_column_type => "numeric"
      tracking_column => "lastUpdateTime"
      # (自己定义的路径) 
      last_run_metadata_path => "/usr/local/es/logstash-6.2.4/bin/config-mysql/syncpoint_table"
    }  
}  
  
filter {  
    json {  
        source => "message"  
        remove_field => ["message"]  
    }  
}  
  
output {  
    elasticsearch {  
        hosts => ["192.168.1.127:9200"]  
        #ES索引名称(自己定义的) 
        index => "analysis_paging_test"  
        #自增ID编号
        document_id => "%{unid}"  
        # 类型名称(类似数据库表名)(自己定义的) 
        document_type => "analysisDatainfo"
    }  
    stdout {  
        codec => json_lines  
    }  
}  

全量xin.conf

input {  
    stdin {  
    }  
    jdbc {  
      # mysql 数据库链接,es为数据库名  
      jdbc_connection_string => "jdbc:mysql://192.168.1.124:3306/dwj_fast?serverTimezone=GMT"  
      # 用户名和密码  
      jdbc_user => "root"  
      jdbc_password => "123456"  
      # 驱动  
      jdbc_driver_library => "/usr/local/es/logstash-6.2.4/bin/config-mysql/mysql-connector-java-8.0.17.jar"
      # 驱动类名(8.0版本的驱动要加cj)  
      jdbc_driver_class => "com.mysql.cj.jdbc.Driver"  
      jdbc_paging_enabled => "true"  
	  #是否把列名变成小写
	  lowercase_column_names => false
      jdbc_page_size => "50000"  
      # 执行的sql 文件路径+名称  
	  #statement => "select dict_code,dict_label,dict_type from sys_dict_data"
      #statement_filepath => "/usr/local/es/logstash-6.2.4/bin/config-mysql/xin.sql" 
      # 设置监听间隔  各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新  
      schedule => "* * * * *"  
      # 设定ES索引类型  
      type => "jdbc"  
    }  
}  
  
filter {  
    json {  
        source => "message"  
        remove_field => ["message"]  
    }  
}  
  
output {  
    elasticsearch {  
        hosts => ["192.168.1.127:9200]  
        #ES索引名称(自己定义的)
        index => "analysis_paging"  
        #自增ID编号
        document_id => "%{uuid}"  
        # 类型名称(类似数据库表名)
        document_type => "analysisDatainfo"
    }  
    stdout {  
        codec => json_lines  
    }  
}  

增量sqlzeng.sql

SELECT
*
FROM
sale_goods_type 
WHERE update_time >= :sql_last_value

全量sqlxin.sql

SELECT
*
FROM
sale_goods_type

启动

cd /usr/local/es/logstash-6.2.4/bin && ./logstash -f config-mysql/xin.conf  > config-mysql/xin.log 2>&1

kibana查看数据

点击下一步



启动命令

elasticsearch

/usr/local/es/elasticsearch-6.2.4/bin/elasticsearch -d

添加elasticsearch 开机自启

在 /etc/init.d 文件夹下建立脚本

cd /etc/init.d
vim elasticsearch

以下是文件内容

#!/bin/sh
#chkconfig: 2345 80 05
#description: elasticsearch
 
export JAVA_HOME=/usr/local/java/jdk1.8.0_202
export JAVA_BIN=$JAVA_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
 
case "$1" in
start)
    su es<<!
    cd /usr/local/es/elasticsearch-6.2.4
    ./bin/elasticsearch -d
!
    echo "elasticsearch startup"
    ;;  
stop)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    kill -9 $es_pid
    echo "elasticsearch stopped"
    ;;  
restart)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    kill -9 $es_pid
    echo "elasticsearch stopped"
    su es<<!
    cd /usr/local/es/elasticsearch-6.2.4
    ./bin/elasticsearch -d
!
    echo "elasticsearch startup"
    ;;  
*)
    echo "start|stop|restart"
    ;;  
esac
 
exit $?

JDK和elasticsearch的路径改为自己的

su es<<!
cd /usr/local/es/elasticsearch-6.2.4
./bin/elasticsearch -d

这里的su es改为自己启动elasticsearch的用户

保存退出,赋予执行权限

chmod +x /etc/init.d/elasticsearch 

添加到开机启动任务

chkconfig --add /etc/init.d/elasticsearch

删除开机任务是

chkconfig --del /etc/init.d/elasticsearch

检查开机任务

chkconfig --list  

2-5是on,说明是开机自启

如果不设置开机启动的话,也可以使用service elasticsearch start/stop/restart来操作

再注意:elasticsearch的脚本里面必须有

#chkconfig: 2345 80 05
#description: elasticsearch

否则在chkconfig时报服务 elasticsearch 不支持chkconfig

Kibana

方案一:使用exit退出shell终端

/usr/local/es/kibana-6.2.4-linux-x86_64/bin/kibana &

等待一会后,加载出status信息,接着输入“exit”回车,shell界面关闭

log   [08:08:11.224] [info][listening] Server running at http://192.168.1.127:5601
log   [08:08:11.259] [info][status][plugin:elasticsearch@6.2.4] Status changed from yellow to green - Ready

方案二:使用nohup 命令

用途:不挂断地运行命令。
语法:nohup Command [ Arg … ] [ & ]

nohup /usr/local/es/kibana-6.2.4-linux-x86_64/bin/kibana &

logstash

cd /usr/local/es/logstash-6.2.4/bin && ./logstash -f config-mysql/xin.conf  > config-mysql/xin.log 2>&1

cd /usr/local/es/logstash-6.2.4/bin && ./logstash -f config-mysql/zeng.conf  > config-mysql/zeng.log 2>&1

logstash 持续长期运行的方式

利用nohup扔到后台运行。

cd  /usr/local/es/logstash-6.2.4/bin

nohup ./logstash -f config-mysql/zeng.conf  > config-mysql/zeng.log 2>&1 &

nohup ./logstash -f config-mysql/xin.conf  > config-mysql/xin.log 2>&1 &

# 查看日志
tail -f config-mysql/sale_goods_full.log

logstash 运行出现you must change the “path.data” setting.

修改启动命令:  /bin/logstash  -f xxxxx.conf  

为: /bin/logstash  -f xxxxxx.conf  --path.data=/root/

检查配置文件

./logstash -f config-mysql/sale/sale_order.conf -t
上一篇:
后端程序员必备的 Linux 基础知识
下一篇:
mysql索引