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" ]
}'