DBA课程-day19-elasticsearch第一天

mongo

1.sql和nosql区别
2.应用场景
- 用户评论
- 游戏装备
- 问卷调查
- 日志
3.mongo特点
4.安装部署
5.配置文件
6.CURD增删改查
7.用户授权
- admin root角色
- 自定义 read write角色
8.副本集
- 创建目录
- 修改配置
- 启动所有节点
- 初始化副本集配置参数
- 测试主从复制是否正常
- 模拟故障转移
- 扩容和收缩
- 权重调整和主库降级
- 仲裁节点
9.备份恢复
- 全备
- oplog备份
- mysql导出csv,导入到mongo
10.模拟误删除操作
- 全备
- 模拟误删除
- 备份oplog
- 切换到local下,查找误删除语句的时间点
- 处理备份文件
- 恢复到误操作时间点以前
- 检验数据是否恢复成功

elasticsearch

1.什么是搜索?

- 百度
- 淘宝,京东

2.如果用数据库做搜索会怎么样?

- 天气 
- 关键词 
- 全表扫描  老男孩教育 老教育  老%%教育  老教育%  老张在老男孩教育 

3.什么是全文检索,倒排索引和Lucene?

    1.老男孩教育
    2.老男孩教育linux学院
    3.老男孩教育python学院
    4.老男孩教育DBA
    5.老男孩教育oldzhang

老               1,2,3,4,5
老男孩         1,2,3,4,5
教育          1,2,3,4,5
学院          2,3
linux           2
python          3
DBA             4
oldzhang        5

老男孩
python 

4.什么是Elasticsearch?

特点:
1.搜索
2.全文检索
3.分析数据
4.处理海量数据PB
5.高可用高性能分布式搜索引擎数据库

应用场景:
1.网页搜索
2.新闻搜索
3.商品标签
4.日志收集分析展示

5.安装部署

### 安装java
yum install -y java-1.8.0-openjdk.x86_64 

### 下载安装软件
mkdir  -p /data/es_soft/
cd /data/es_soft/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.rpm
#yum install elasticsearch-6.6.0.rpm -y
rpm -ivh elasticsearch-6.6.0.rpm

### 配置启动
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
systemctl status elasticsearch.service
systemctl is-active elasticsearch.service

### 检查是否启动成功
ps -ef|grep elastic
lsof -i:9200
curl localhost:9200

6.查看配置文件位置

[root@db01 ~]# rpm -qc elasticsearch 
/etc/elasticsearch/elasticsearch.yml        #主配置文件
/etc/elasticsearch/jvm.options              #java虚拟机配置
/etc/init.d/elasticsearch                   #init.d启动脚本
/etc/sysconfig/elasticsearch                #环境变量相关信息,不需要动
/usr/lib/sysctl.d/elasticsearch.conf        #环境变量相关
/usr/lib/systemd/system/elasticsearch.service   #systemd启动脚本

7.调整jvm配置

/etc/elasticsearch/jvm.options
-Xms1g
-Xmx1g

8.调整elasticsearch配置

/etc/elasticsearch/elasticsearch.yml
[root@db01 ~]# grep "^[a-z]" /etc/elasticsearch/elasticsearch.yml
node.name: node-1
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.51 
http.port: 9200
mkdir /data/elasticsearch/ -p 
chown -R elasticsearch:elasticsearch /data/elasticsearch/
systemctl start elasticsearch

启动报错1

[2019-07-09T10:49:36,109][ERROR][o.e.b.Bootstrap          ] [node-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked

解决方法:

https://www.elastic.co/guide/en/elasticsearch/reference/6.6/setting-system-settings.html
https://www.elastic.co/guide/en/elasticsearch/reference/6.6/setup-configuration-memory.html

systemctl edit elasticsearch
[Service]
LimitMEMLOCK=infinity
systemctl daemon-reload
systemctl start elasticsearch

启动报错2

[2019-07-09T11:08:14,935][WARN ][o.e.b.JNANatives         ] [node-1] Unable to lock JVM Memory: error=12, reason=Cannot allocate memory

启动报错总结

1.配置文件没有任何修改
2.配置文件没有修改IP地址
3.系统内存只有1个G,启动失败
4.配置文件参数拼写错误,启动失败
5.忘记修改内存锁定,启动失败
6.重启linux
重启能解决90%的问题
另外的8% 重启两遍可以解决
还有2% 只能用量子力学和玄学来解释
名字概念
es          mysql
index       库 
type        表
fields      字段
doc         一行数据

交互方式
RESTful API
curl命令:
    最繁琐
    最复杂
    最容易出错
    不需要安装任何软件,只需要有curl命令

es-head插件
    查看数据方便
    操作相对容易
    需要node环境

kibana
    查看数据以及报表格式丰富
    操作很简单
    需要java环境和安装配置kibana

es-head安装部署

yum install nodejs npm openssl screen -y
node -v
npm  -v
npm install -g cnpm --registry=https://registry.npm.taobao.org
tar zxvf elasticsearch-head.tar.gz -C /opt/
cd /opt/elasticsearch-head
npm run start & 

修改es配置文件
vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true 
http.cors.allow-origin: "*"

重启es
systemctl restart elasticsearch
    
网页打开
10.0.0.51:9100
输入
http://10.0.0.51:9200

API使用
curl -XPUT  'localhost:9200/vipinfo/user/2?pretty' -H 'Content-Type: application/json' -d' {
    "first_name": "zhang",
    "last_name" : "ya",
    "age" : 18,
    "about" : "I like to collect rock albums", "interests": [ "music" ]
}'

curl -XPUT  'localhost:9200/vipinfo/user/3?pretty' -H 'Content-Type: application/json' -d' {
    "first_name": "ya",
    "last_name" : "zhang",
    "age" : 28,
    "about" : "I like to collect rock albums", "interests": [ "music" ]
}'

curl -XPOST  'localhost:9200/vipinfo/user?pretty' -H 'Content-Type: application/json' -d' {
    "first_name": "老男孩Linux",
    "last_name" : "学院",
    "age" : 8,
    "about" : "I like to collect rock albums", "interests": [ "music" ]
}'

查询索引中所有的

curl -XGET localhost:9200/vipinfo/user/_search?pretty

查询指定文档数据

curl -XGET 'localhost:9200/vipinfo/user/1?pretty'
curl -XGET 'localhost:9200/vipinfo/user/2?pretty’

按条件查询文档数据

curl -XGET 'localhost:9200/vipinfo/user/_search?q=last_name:ya&pretty'
curl -XGET 'localhost:9200/vipinfo/user/_search?q=age:28&pretty'

使用Query-string查询

curl -XGET 'localhost:9200/vipinfo/user/_search?pretty' -H 'Content-Type: application/json' -d'           
{
  "query" : { 
    "match" : {
        "last_name" : "ya"
     }
  } 
}
'

curl -XGET 'localhost:9200/vipinfo/user/_search?pretty' -H 'Content-Type: application/json' -d'{ 
  "query" : { 
    "bool": { 
      "must": { 
        "match" : { 
          "first_name" : "zhang" 
          } 
     }, 
     "filter": { 
        "range" : {"age" : { "lt" : 30 }  
          } 
        } 
      } 
    } 
 }'

更新数据的两种方式

#PUT更新,需要填写完整的信息
curl -XPUT 'localhost:9200/vipinfo/user/4?pretty' -H 'Content-Type: application/json' -d'
{
    "first_name" : "zhang",
    "last_name": "ya",
    "age" : 27,
    "about" : "i love my cat", "interests": [ "sports", "music" ]
}

#POST更新,只需要填写需要更改的信息 
curl -XPOST 'localhost:9200/vipinfo/user/4?pretty' -H 'Content-Type: application/json' -d'
{
    "age" : 29
}

curl -XPOST  'localhost:9200/linux58/linux?pretty' -H 'Content-Type: application/json' -d' {
    "id" : 1 ,
    "first_name": "zhang",
    "last_name" : "ya",
    "age" : 18,
    "group" : 1
}'

curl -XPOST  'localhost:9200/linux58/linux?pretty' -H 'Content-Type: application/json' -d' {
    "id" : 2 ,
    "first_name": "li",
    "last_name" : "banzhang",
    "age" : 18,
    "group" : 10
}'

curl -XPOST  'localhost:9200/linux58/linux?pretty' -H 'Content-Type: application/json' -d' {
    "id" : 3 ,
    "first_name": "what",
    "last_name" : "ao",
    "age" : 17,
    "group" : 13
}'

删除指定文档数据

curl -XDELETE 'localhost:9200/vipinfo/user/1?pretty’
{
  "_index" : "vipinfo",
  "_type" : "user",
  "_id" : "1",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 2
}

删除索引

curl -XDELETE 'localhost:9200/vipinfo?pretty'
master-eligible nodes / 2 + 1

集群配置文件

### 节点1配置文件
cluster.name: linux58
node.name: node-1
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52"]
discovery.zen.minimum_master_nodes: 1
http.cors.enabled: true 
http.cors.allow-origin: "*"

###节点2配置文件  
cluster.name: linux58
node.name: node-2
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.52,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52"]
discovery.zen.minimum_master_nodes: 1
http.cors.enabled: true 
http.cors.allow-origin: "*"

###节点3配置文件  
cluster.name: linux58
node.name: node-3
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.53,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.53"]
discovery.zen.minimum_master_nodes: 1
http.cors.enabled: true 
http.cors.allow-origin: "*"

调整限制

限定条件
1.索引一旦创建以后,分片数就固定死了,不能调整了
2.但是副本数可以动态调整

curl -XPUT 'localhost:9200/index2/_settings?pretty' -H 'Content-Type: application/json' -d'         
{
"settings" : { 
  "number_of_replicas" : 2
 } 
}'

curl -XPUT 'localhost:9200/index2/_settings?pretty' -H 'Content-Type: application/json' -d'         
{
"settings" : { 
  "number_of_replicas" : 0
 } 
}'

集群状态

默认创建索引的时候
1副本
5分片

green    #所有索引数据都存在,副本满足条件,所有数据都完整
yellow   #所有索引数据完整,但是副本不满足条件
red      #数据不完整,有的索引数据有丢失

curl -XGET 'http://localhost:9200/_cluster/health?pretty'

查看系统检索信息
Cluster Stats API允许从群集范围的角度检索统计信息。
官网地址:
https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-stats.html
操作命令:
curl -XGET 'http://localhost:9200/_cluster/stats?human&pretty'

查看集群的设置
官方地址:
https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-get-settings.html
操作命令:
curl -XGET 'http://localhost:9200/_cluster/settings?include_defaults=true&human&pretty’

查询节点的状态

官网地址:
https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
操作命令:
curl -XGET 'http://localhost:9200/_nodes/procese?human&pretty'  
curl -XGET 'http://localhost:9200/_nodes/_all/info/jvm,process?human&pretty'
curl -XGET 'http://localhost:9200/_cat/nodes?human&pretty'                   

创建索引时调整副本数
curl -XPUT 'localhost:9200/index2?pretty' -H 'Content-Type: application/json' -d'       
{
    "settings" : { 
        "number_of_shards" : 3, 
        "number_of_replicas" : 1
    } 
}'

curl -XPUT 'localhost:9200/index3?pretty' -H 'Content-Type: application/json' -d'       
{
    "settings" : { 
        "number_of_shards" : 3, 
        "number_of_replicas" : 2
    } 
}'

curl -XPUT 'localhost:9200/index3?pretty' -H 'Content-Type: application/json' -d'       
{
    "settings" : { 
        "number_of_shards" : 3, 
        "number_of_replicas" : 2
    } 
}'

启动报错1

[2019-07-09T10:49:36,109][ERROR][o.e.b.Bootstrap          ] [node-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked

报错现象

只有一个节点有数据
报错日志
[2019-07-09T17:29:59,874][INFO ][o.e.c.r.a.DiskThresholdMonitor] [node-1] rerouting shards: [high disk watermark exceeded on one or more nodes]
[2019-07-09T17:30:29,888][WARN ][o.e.c.r.a.DiskThresholdMonitor] [node-1] high disk watermark [90%] exceeded on [hZohqK33S1qxHuGHoC_byQ][node-1][/data/elasticsearch/nodes/0] free: 290.1mb[5.6%], shards will be relocated away from this node

1.节点数小于3
or
2.健康状态不是green

curl -XGET 'localhost:9200/linux58/_search?pretty'
curl -XPUT 'localhost:9200/linux58/_settings?pretty' -H 'Content-Type: application/json' -d'         
{
"settings" : { 
  "number_of_replicas" : 2
 } 
}'

curl -XPOST  'localhost:9200/index3/user?pretty' -H 'Content-Type: application/json' -d' {
    "first_name": "老男孩Linux",
    "last_name" : "学院",
    "age" : 8,
    "about" : "I like to collect rock albums", "interests": [ "music" ]
}'
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容