安装elasticsearch
默认端口:9200
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-linux-x86_64.tar.gz
tar zxvf elasticsearch-7.0.0-linux-x86_64.tar.gz
mv elasticsearch /soft/elasticsearch
groupadd elasticsearch
useradd es -d /home/es -g elasticsearch -p 000000
visudo
在 root ALL=(ALL) ALL 下添加
es ALL=(ALL) ALL
su es
sudo chown -R es:elasticsearch /soft/elasticsearch
// 完整删除用户 userdel -r es
- 修改/etc/sysctl.conf
vm.max_map_count=655360
sysctl.conf立即生效
/sbin/sysctl -p
编辑 /etc/security/limits.conf,追加以下内容;
* soft nofile 65536
* hard nofile 65536
此文件修改后需要重新登录用户,才会生效
vi /soft/elasticsearch/bin/elasticsearch
#添加jdk判断 root用户能找到jdk 普通用户找不到 所以需要加判断
if [ -x "/usr/bin/java" ]; then
JAVA="/usr/bin/java"
else
JAVA=which java
fi
- 修改config/elasticsearch.yml
cluster.name: logsearch node.name: node-1 cluster.initial_master_nodes: ["node-1"] path.data: /soft/elasticsearch/data path.logs: /soft/elasticsearch/logs bootstrap.memory_lock: false network.host: 0.0.0.0 #network.bind_host: 0.0.0.0(Unable to connect to Elasticsearch at http://localhost:9200错误解决方案) http.port: 9200
/soft/elasticsearch/bin/elasticsearch -d
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --reload
elasticsearch/bin/plugin -install mobz/elasticsearch-head 安装elasticsearch-head
开启x-pack(7.3不需要单独安装x-pack)
在elasticsearch.yml添加:
// xpack.security.enabled: true // 用于kibana 需要ssl 可以不开启
// xpack.security.audit.enabled: true
// xpack.license.self_generated.type: basic
//如果是basic license的话需要加入下面这一行,不然的话restart elasticsearch之后会报错。
xpack.security.transport.ssl.enabled: true
设置密码
/soft/elasticsearch/bin/elasticsearch-setup-passwords interactive
可能有报错
生成systemctl文件
vi /etc/systemd/system/es.service
[Unit]
Description=ElasticSearchAutoStartService
[Service]
Type=forking
User=es
ExecStart=/soft/elasticsearch/bin/elasticsearch -d
[Install]
WantedBy=multi-user.target
启动服务
systemctl enable es
systemctl start es
# 重启系统
reboot
# 查看ElasticSearch进程是否已启动
ps -ef|grep elasticsearch
后台启动
/soft/elasticsearch/bin/elasticsearch -d
安装head
不要放在plugins文件夹
默认端口9100
需要安装jdk11
修改/soft/elasticsearch/bin/elasticsearch 指定jdk版本
参考文档
修改/soft/elasticsearch/config/jvm.options 将 : -XX:+UseConcMarkSweepGC(这是jdk8默认的垃圾回收器)改为:-XX:+UseG1GC(这是jdk11默认的)
cd /soft/bak
否则报错:Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release
参考文档
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip master.zip
mv elasticsearch-head-master /soft/elasticsearch/
yum install npm
cd /soft/elasticsearch/plugins/elasticsearch-head
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install
#SPDX报错修改package.json找到license位置,原来的Apache内容修改为Apache-2.0
Elasticsearch配置,允许跨域访问
vi elasticsearch-7.1.1/config/elasticsearch.yml
#添加如下配置,支持跨域访问
http.cors.enabled: true
http.cors.allow-origin: "*"
cd /soft/elasticsearch-head/
npm install
nohup npm run start &
访问head : http://47.95.196.16:19100/
Elasticsearch-head 远程连接
在elasticsearch-head-5.0.0_site\app.js中搜索9200
修改为远程地址和端口
安装phantomjs
cd /soft/bak
https://github.com/ariya/phantomjs/archive/2.1.1.tar.gz
tar zxvf 2.1.1.tar.gz
mv /soft/bak/phantomjs-2.1.1 /soft/phantomjs
cd /soft/phantomjs
yum -y install fontconfig
安装ik
参考文档
下载插件 wget https://github.com/medcl/elasticsearch-analysis-ik/releases最新版.zip
/soft/elasticsearch/bin/elasticsearch-plugins install /soft/bak/xxx.zip(待验证是否可用)
将最新版.zip解压至/soft/elasticsearch/plugins/ik
启动elasticsearch
安装kibana
默认端口5601
参考文档
中文版只需要在配置文件 kibana.yml 中加入i18n.locale: “zh-CN”
关闭kibana
fuser -n tcp 5601
kill -9 xxx
启动kibana
cd /soft/kibana
nohup /soft/kibana/bin/kibana &
安装logstash
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.3.2.tar.gz
win10安装 Elasticsearch
官网下载win版
在cmd进入目录bin执行elasticsearch.bat
elasticsearch.yml
network.host: 0.0.0.0
cluster.initial_master_nodes: ["node-1"] #bootstrap checks failed报错解决
xpack.ml.enabled: false
http.cors.enabled: true
http.cors.allow-origin: "*"
win10安装 Elasticsearch-head
方法一:
在chrome商店安装
方法二:
下载 https://github.com/mobz/elasticsearch-head
安装nodejs
解压进入文件夹执行 npm install
npm run start
localhost:9100 访问
win10安装logstash
下载 https://www.elastic.co/cn/downloads/logstash
参考 https://blog.csdn.net/plei_yue/article/details/78317980
修改 es 远程地址
input { stdin { } }
output {
elasticsearch { hosts => ["192.168.123.236:9200"] }
stdout { codec => rubydebug }
}
bin/logstash -f logstash.conf
logstash同步mysql
首先创建一个mysql.conf的文件
input {
stdin {
}
jdbc {
# mysql 数据库链接,shop为数据库名
jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/esshop"
# 用户名和密码
jdbc_user => "root"
jdbc_password => "root"
# 驱动
jdbc_driver_library => "D:/elasticsearch/logstash-5.5.2/mysqletc/mysql-connector-java-5.0.8.jar"
# 驱动类名
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 执行的sql 文件路径+名称
statement_filepath => "D:/elasticsearch/logstash-5.5.2/mysqletc/qt.sql"
# 设置监听间隔 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新
schedule => "* * * * *"
# 索引类型
type => "content"
}
}
filter {
json {
source => "message"
remove_field => ["message"]
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "cmscontent"
document_id => "%{id}"
}
stdout {
codec => json_lines
}
}
在logstash文件目录下创建一个文件夹,命名随意,我这里是mysql
创建一个sql文件,我这里命名为qt.sql和上边的配置文件一致
SELECT * FROM qt
最后需要把一个jdbc驱动(mysql-connector-java-5.0.8.jar)放到这个文件夹下,用来连接mysql数据库
常见错误
如果我们在同步mysql数据库中,涉及到时间的字段,格式设置成datetime,我们创建的index时间mapping设置为type:date,format:yyyy-MM-dd HH:mm:ss;那么直接进行同步,就会同步失败。
在mysql直接取出来的时间字段,他是带有时区的时间格式,与es创建mapping中的yyyy-MM-dd HH:mm:ss不符合,所有导致同步失败。
解决方案:在同步过程中,将时间进行处理,我这里通过sql语句将时间进行格式化,在同步到es中
date_format(created_at,’%Y-%m-%d %H:%i:%s’ ) as created_at