ToB企服应用市场:ToB评测及商务社交产业平台

标题: Centos 7 通过 targz 文件安装 Elastic Search 服务 [打印本页]

作者: 我可以不吃啊    时间: 2023-6-9 03:23
标题: Centos 7 通过 targz 文件安装 Elastic Search 服务
区别于通过发行版自带的仓库, 介绍如何通过 targz 文件安装 Elastic Search 服务, 使用的 Linux 为 Centos 7
下载

https://www.elastic.co/downloads/elasticsearch
选择 Linux x86_64, 下载 elasticsearch-8.8.0-linux-x86_64.tar.gz
安装

解压到 /opt/elasticsearch, 并加上软链
  1. tar xvf elasticsearch-8.8.0-linux-x86_64.tar.gz
  2. cd /opt/
  3. sudo mkdir elasticsearch
  4. cd elasticsearch/
  5. sudo mv ~/backup/elasticsearch-8.8.0 .
  6. sudo chown -R milton:milton elasticsearch-8.8.0/
  7. sudo ln -s elasticsearch-8.8.0 latest
复制代码
这个版本的 Elastic Search 自带 JVM, 版本为 openjdk version "20.0.1" 2023-04-18
配置

可能需要修改的配置
  1. # Use a descriptive name for your cluster:
  2. #cluster.name: my-application
  3. # Use a descriptive name for the node:
  4. node.name: centos7001
  5. # Add custom attributes to the node:
  6. #node.attr.rack: r1
  7. # Path to directory where to store the data (separate multiple locations by comma):
  8. path.data: /home/milton/es_run/data
  9. # Path to log files:
  10. path.logs: /home/milton/es_run/logs
  11. # By default Elasticsearch is only accessible on localhost. Set a different
  12. # address here to expose this node on the network:
  13. network.host: 192.168.9.10
  14. # By default Elasticsearch listens for HTTP traffic on the first free port it
  15. # finds starting at 9200. Set a specific HTTP port here:
  16. #http.port: 9200
  17. # Pass an initial list of hosts to perform discovery when this node is started:
  18. # The default list of hosts is ["127.0.0.1", "[::1]"]
  19. #discovery.seed_hosts: ["centos7001"]
  20. # Bootstrap the cluster using an initial set of master-eligible nodes:
  21. cluster.initial_master_nodes: ["centos7001"]
  22. # For more information, consult the discovery and cluster formation module documentation.
  23. # Allow wildcard deletion of indices:
  24. #action.destructive_requires_name: false
  25. xpack.security.enabled: false
复制代码
node.name 和 cluster.initial_master_nodes, 可以填IP也可以填hostname, 但是要一致
系统配置

以下的配置用于解决下面的问题
1.  max file descriptors 65535

修改/etc/security/limits.conf (或者 /etc/security/limits.d/20-nproc.conf), 增加或修改为以下内容
  1. *          soft    nofile    65535
  2. *          hard    nofile    65535
  3. *          soft    nproc     65535
  4. *          hard    nproc     65535
  5. root       soft    nproc     unlimited
复制代码
需要重启, 用 ulimit -n 检查
2. vm.max_map_count 262144

修改/etc/sysctl.conf 或者 /etc/sysctl.d/99-sysctl.conf文件,增加或修改为以下内容
  1. vm.max_map_count=262144
复制代码
3. the default discovery settings are unsuitable for production use

需要配置 discovery.seed_hosts,discovery.seed_providers,cluster.initial_master_nodes中的至少一个参数
修改配置文件 config/elasticsearch.yml, 配置以下两项
  1. discovery.seed_hosts: ["127.0.0.1"]
  2. cluster.initial_master_nodes: ["node-1"]
复制代码
4. Transport SSL must be enabled if security is enabled

修改配置文件 config/elasticsearch.yml, 增加
  1. xpack.security.enabled: false
复制代码
5. WARN: This node is a fully-formed single-node cluster

如果在日志中看到类似这样的错误
  1. [2023-06-09T07:29:43,781][WARN ][o.e.c.c.Coordinator      ] [centos7001] This node is a fully-formed single-node cluster with cluster UUID [6ejfGD71SVe6OpypK-1HmA], but it is configured as if to discover other nodes and form a multi-node cluster via the [discovery.seed_hosts=[192.168.123.123]] setting. Fully-formed clusters do not attempt to discover other nodes, and nodes with different cluster UUIDs cannot belong to the same cluster. The cluster UUID persists across restarts and can only be changed by deleting the contents of the node's data path(s). Remove the discovery configuration to suppress this message.
复制代码
说明这是一个单节点的ES, 但是配置文件中配置其去发现另一个节点. 需要将 discovery.seed_hosts 中设置的节点去掉
运行

直接运行, 这样会将日志直接输出到控制台
  1. /opt/elasticsearch/latest/bin/elasticsearch
复制代码
后台运行, 在命令后加 -d -p pid-file, 在输出一段控制台日志后, 如果没有报错, 会转入后台运行
  1. /opt/elasticsearch/latest/bin/elasticsearch -d -p /opt/elasticsearch/latest/logs/pid
复制代码
停止

根据记录的 pid 停止, 启动时记录用的哪个文件, 这里就用对应的文件
  1. pkill -F /opt/elasticsearch/latest/logs/pid
复制代码
访问服务

浏览器打开 http://192.168.123.123:9200/  能看到ES的输出, 就说明运行成功
  1. {
  2.   "name" : "centos70",
  3.   "cluster_name" : "elasticsearch",
  4.   "cluster_uuid" : "_na_",
  5.   "version" : {
  6.     "number" : "8.8.0",
  7.     "build_flavor" : "default",
  8.     "build_type" : "tar",
  9.     "build_hash" : "c01029875a091076ed42cdb3a41c10b1a9a5a22f",
  10.     "build_date" : "2023-05-23T17:16:07.179039820Z",
  11.     "build_snapshot" : false,
  12.     "lucene_version" : "9.6.0",
  13.     "minimum_wire_compatibility_version" : "7.17.0",
  14.     "minimum_index_compatibility_version" : "7.0.0"
  15.   },
  16.   "tagline" : "You Know, for Search"
  17. }
复制代码
查询集群运行状况
  1. curl -XGET "127.0.0.1:9200/_cat/health?v"
复制代码
查询集群所有索引
  1. $ curl -XGET "192.168.123.123:9200/_cat/indices?v"
  2. health status index        uuid                   pri rep docs.count docs.deleted store.size pri.store.size
  3. yellow open   commodity002 XIrCTL_XQq2vteuEflY6vA   1   1          0            0       247b           247b
  4. yellow open   commodity001 Z-LKjzsuR8uMLgVlYYALEw   1   1          0            0       247b           247b
  5. yellow open   commodity004 sSxEiwNBSvernMH6EYsEvw   1   1          0            0       247b           247b
  6. yellow open   commodity003 JSRUndkHQ8mQVdTkN9eCPw   1   1          0            0       247b           247b
复制代码
创建索引

不带参数, ?pretty用于格式化响应的json
  1. curl -X PUT "localhost:9200/commodity?pretty"
复制代码
带参数
  1. curl -H 'Content-Type: application/json' -X PUT 'http://192.168.123.123:9200/commodity007?pretty' \
  2. --data '{
  3.     "settings": {
  4.         "number_of_shards": 3,
  5.         "number_of_replicas": 2
  6.     }
  7. }'
复制代码
带索引字段,
  1. curl -H 'Content-Type: application/json' -X PUT 'http://192.168.123.123:9200/commodity008?pretty' \
  2. --data '{
  3.   "settings": {
  4.     "number_of_shards": 2,
  5.     "number_of_replicas": 1
  6.   },
  7.   "mappings": {
  8.     "properties": {
  9.       "name":{
  10.         "type": "text"
  11.       },
  12.       "studymodel":{
  13.         "type": "keyword"
  14.       },
  15.       "price":{
  16.         "type": "double"
  17.       },
  18.       "timestamp": {
  19.          "type": "date",
  20.          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  21.       },
  22.       "pic":{
  23.         "type":"text",
  24.         "index": false
  25.       }
  26.     }
  27.   }
  28. }'
复制代码
对于嵌套存在的字段, mappings是可以用层级的, 例如对 type1下的obj1的索引
  1. {
  2.   "mappings": {
  3.     "type1": {
  4.       "properties": {
  5.         "obj1": {
  6.           "type": "nested"
  7.         }
  8.       }
  9.     }
  10.   }
  11. }
复制代码
查看索引字段及设置
  1. curl -X GET 'http://192.168.123.123:9200/commodity008?pretty'
复制代码
往索引写入内容

通过路径指定 _id = 1, 对同一个 _id可以再次调用进行更新, 结果中的_version会递增
  1. curl --location --request PUT 'http://192.168.123.123:9200/commodity008/_doc/1?pretty' \
  2. --header 'Content-Type: application/json' \
  3. --data '{
  4.     "name": "commodity008001",
  5.     "studymodel": "202306",
  6.     "price": 123.12,
  7.     "timestamp": "2023-05-25 19:11:35",
  8.     "pic": "23/06/01/a123b1fde0428.jpg"
  9. }'
复制代码
查询

可以通过URL路径区分不同索引
查询所有索引下的内容
  1. curl -X GET 'http://192.168.123.123:9200/_search?pretty'
复制代码
查询一个索引下的内容
  1. curl -X GET 'http://192.168.123.123:9200/commodity008/_search?pretty'
复制代码
带条件查询
  1. curl -H 'Content-Type: application/json' -X GET 'http://192.168.9.10:9200/commodity008/_search?pretty=null' \
  2. --data '{
  3.     "query" : {
  4.         "match" : {
  5.             "name": "commodity008001"
  6.         }
  7.     }
  8. }'
复制代码
带偏移和结果数量, 请求加上 from 和 size 参数
  1. {
  2.   "from":10,
  3.   "size":20,
  4.   "query":{
  5.     "match_all": {}
  6.   }
  7. }
复制代码
排序, 请求加上 sort 参数
  1. {
  2.   "sort":[{"year":"desc"}],
  3.   "query":{
  4.     "match_all": {}
  5.   }
  6. }
复制代码
限制返回的字段, 请求加上 _source 字段
  1. {
  2.   "_source":["title"],
  3.   "query":{
  4.     "match_all": {}
  5.   }
  6. }
复制代码
结果格式
  1. {
  2.     "took": 422,
  3.     "timed_out": false,
  4.     "_shards": {
  5.         "total": 2,
  6.         "successful": 2,
  7.         "skipped": 0,
  8.         "failed": 0
  9.     },
  10.     "hits": {
  11.         "total": {
  12.             "value": 2,
  13.             "relation": "eq"
  14.         },
  15.         "max_score": 1.0,
  16.         "hits": [
  17.             {
  18.                 "_index": "commodity008",
  19.                 "_id": "1",
  20.                 "_score": 1.0,
  21.                 "_source": {
  22.                     "name": "commodity008001",
  23.                     "studymodel": "202307",
  24.                     "price": 123.53,
  25.                     "timestamp": "2023-05-25 19:11:35",
  26.                     "pic": "23/06/01/a123b1fde0428.jpg"
  27.                 }
  28.             },
  29.             ...
  30.         ]
  31.     }
  32. }
复制代码
参考


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4