一:安装
1:安装ES 之前必须安装jdk,最好安装jdk11 或者以上版本(要求服务器内存要高于2g,生产必须要大于4g)
#查看jdk 版本,如果没有或者版本低于1.8则重新安装,已有则不需要安装
java -version
安装jdk11
yum -y install java-11-openjdk
2:下载es压缩包,在你适合的目录里下载
wget:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.15.2-linux-x86_64.tar.gz
3:解压
tar -zxvf elasticsearch-7.15.2-linux-x86_64.tar.gz
4:创建新用户 es(es不能用root 用户运行,需要先创建用户)
useradd es
5:将解压的es文件归属于新用户
chown -R es elasticsearch-7.15.2
切换用户,并进入es文件夹
su es
cd elasticsearch-7.15.2
6:修改jvm配置根据自己机器 这里修改为1g,保证这2个值相等(这里要求设备内存最少2g以上,不然可能会运行失败)
-Xms1g
-Xmx1g
7:单机运行:修改完上面的 就可以启动了
用es 用户 在 elasticsearch-7.15.2 文件夹运行
./bin/elasticsearch
第二:配置外网访问
配置外网访问,目的是方便我们操作;
修改配置:elasticsearch.yml
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#集群名称
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#节点名称
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# 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.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#绑定地址
#network.host: 192.168.0.1
network.host: 0.0.0.0
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#节点发现
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#主节点,这个绑定外网必须要配置,并且得保证跟上面节点名称一致
#cluster.initial_master_nodes: ["node-1", "node-2"]
cluster.initial_master_nodes: ["node-1"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#控制面板需要跨域
http.cors.allow-origin: "*"
http.cors.enabled: true
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization
http.cors.allow-credentials: true
解决错误:
ERROR: [N] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [1024] for user [uaren] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[4]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
[5]:future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/nlp/java/jdk1.8.0_162/jre] does not meet this requirement
[6]:Exception in thread "main" SettingsException[Failed to load settings from [elasticsearch.yml]]; nested: MarkedYAMLException[mapping values are not allowed here
1:原因: 每个进程最大同时打开文件数太小,可通过下面2个命令查看当前数量
查看 进程数
ulimit -Hn
ulimit -Sn
解决:max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
修改 /etc/security/limits.conf配置文件
sudo vim /etc/security/limits.conf
在配置文件加上(已经有或者大于这个数则不需要修改):
* hard nofile 65536
* soft nofile 65536
解决:max number of threads [1024] for user [uaren] is too low, increase to at least [4096]
# sudo vim /etc/security/limits.conf
在配置文件加上:
* hard nproc 4096
* soft nproc 4096
解决: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
# 修改/etc/sysctl.conf文件,增加配置vm.max_map_count=655360
sudo vim /etc/sysctl.conf
在配置文件加上:
vm.max_map_count=655360
#保存之后 生效
sysctl -p
# 注意:增加配置,用户退出后重新登录生效
解决: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
修改elasticsearch.yml配置,参照上面即可解决
完成之后启动es即可在浏览器访问;这里要把对应端口访问放开( 9200 跟1358 Dejavu面板用的)
#启动
./bin/elasticsearch
#浏览器访问,正常就会返回信息
http://ip:9200
三:安装监控面Dejavu
1、先修改Elasticsearch配置文件,增加如下配置(上面的参考配置已经增加)
http.cors.allow-origin: "*"
http.cors.enabled: true
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization
http.cors.allow-credentials: true
2:安装docker
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
3:运行docker
systemctl start docker.service
4:运行Dejavu 镜像
docker run -p 1358:1358 -d appbaseio/dejavu
5:浏览器访问
http://ip:1358
6:链接es 前需要给es 建立至少一个索引,这里直接用postman 添加
#http put 方式
http://1.14.197.220:9200/megacorp/employee/1
#body raw
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
添加成功即可用dejavu链接了
ps:如果发现浏览器链接不了1358 这端口,可能是服务器的Dejavu 服务挂了,可以重启下docker服务即可访问,这里注意需要安全组放开端口1358
#查找docker 进程信息
ps -aux|grep docker
#找到绑定1358端口的进程 并杀死
root 8855 0.1 1.6 1108104 31740 ? Ssl 09:50 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 9122 0.0 0.0 808636 1172 ? Sl 09:51 0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 1358 -container-ip 172.17.0.2 -container-port 1358
root 10277 0.0 0.0 112708 984 pts/2 R+ 09:53 0:00 grep --color=auto docker
kill -9 9122
#重启Dejavu 服务
docker run -p 1358:1358 -d appbaseio/dejavu
四:安装php拓展并使用
1:安装拓展直接用compose 即可
composer require elasticsearch/elasticsearch
安装完后简单封装一个EsModel 即可使用
<?php
/**
* Created by PhpStorm.
* 05
* 封装es 操作模型,非完整
*/
namespace app\common\model;
use Elasticsearch\ClientBuilder;
class ElasticSearch{
private $cline=null;
public function __construct($host="127.0.0.1",$port="9200")
{
$host=[$host.":".$port];
$this->cline = ClientBuilder::create()->setHosts($host)->build();
}
public function addIndex($pms){
return $this->cline->indices()->create($pms);
}
public function addIndexData($index,$data){
$pms=[
'index'=>$index,
'body'=>$data
];
return $this->cline->index($pms);
}
/**
* @param $pms
* @return array
*/
public function search($pms){
return $this->cline->search($pms);
}
}
下面是简单使用测试
//测试es使用
public function testes(){
$index_params = [
'index' => 'my_index',
'body' => [
'mappings' => [
'properties' => [
'user_id' => [
'type' => 'string',
'ignore_above' => 20,
],
'keyword' => [
'type' => 'string',
'ignore_above' => 20,
],
],
],
],
];
// 添加索引
echo json_encode(model("ElasticSearch")->addIndex($index_params));
exit();
//添加数据
$add_data_pms=[
'keyword'=>'测试工程师',
'user_id'=>'1001'
];
echo json_encode(model("ElasticSearch")->addIndexData("my_index",$add_data_pms));
exit();
//搜索测试
$sparams = [
'index' => 'my_index',
'type' => '_doc',
'body' => [
'query' => [
'match' => [
'keyword' => '测试'
]
]
]
];
$d=model("ElasticSearch")->search($sparams);
echo json_encode($d);
exit();
}
PS:操作demo 可以在es拓展源码里找
es一些名词解析
index:索引,一般一个索引是一个类型,比如员工索引,书籍索引等 索引里可以分类型就是 type
type:索引类型,新版本可能会弃用,默认不设置就可以
Document Index 里面单条的记录称为Document(文档)。等同于关系型数据库表中的行。
properties:属性,每个文档都有属性,就像数据库表的字段,es里属性里也可以有属性,可以多层嵌套
下面是实例解析
$index_params = [
'index' => 'my_index', //这个是索引名称
'body' => [//这是索引内容
'mappings' => [ //这个只是代表映射关系表述
'properties' => [//索引拥有的属性
'user_id' => [ //属性 字段
'type' => 'string',//字段类型
'ignore_above' => 20,//字段限制
],
'keyword' => [//另一个字段
'type' => 'string',
'ignore_above' => 20,
],
],
],
],
];
简单使用先分享到这里,下面是es官网全文指南文档,深入学习请自觉…
ES官方指南