经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
Centos 7 通过 targz 文件安装 Elastic Search 服务
来源:cnblogs  作者:Milton  时间:2023/6/9 8:43:12  对本文有异议

区别于通过发行版自带的仓库, 介绍如何通过 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
  • cluster.name: my-application 集群名称
  • node.name 要改成当前服务器的hostname
  • path.data: /somew/data 数据路径
  • path.logs: /somewhere/logs 日志路径
  • network.host: 192.168.123.123 监听的网口, 默认只监听127.0.0.1
  • http.port: 9200 监听的端口, 默认为9200
  • discovery.seed_hosts: ["192.168.123.123"] 集群主机列表, 和下面的cluster.initial_master_nodes必须写一个, 不然启动会报错. 如果只是单节点, 这行可以注释掉
  • cluster.initial_master_nodes: ["centos7001"] 启动时初始化的参与选主的node 对应的 hostname, 要能解析为IP

node.name 和 cluster.initial_master_nodes, 可以填IP也可以填hostname, 但是要一致

系统配置

以下的配置用于解决下面的问题

  1. max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
  2. max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
  3. 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
  4. Transport SSL must be enabled if security is enabled. Please set [xpack.security.transport.ssl.enabled] to [true] or disable security by setting [xpack.security.enabled] to [false]

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中的至少一个参数

  • discovery.seed_hosts: 集群主机列表
  • discovery.seed_providers: 基于配置文件配置集群主机列表
  • cluster.initial_master_nodes: 启动时初始化的参与选主的node

修改配置文件 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' --data '{
  2. "settings": {
  3. "number_of_shards": 3,
  4. "number_of_replicas": 2
  5. }
  6. }'

带索引字段,

  1. curl -H 'Content-Type: application/json' -X PUT 'http://192.168.123.123:9200/commodity008?pretty' --data '{
  2. "settings": {
  3. "number_of_shards": 2,
  4. "number_of_replicas": 1
  5. },
  6. "mappings": {
  7. "properties": {
  8. "name":{
  9. "type": "text"
  10. },
  11. "studymodel":{
  12. "type": "keyword"
  13. },
  14. "price":{
  15. "type": "double"
  16. },
  17. "timestamp": {
  18. "type": "date",
  19. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  20. },
  21. "pic":{
  22. "type":"text",
  23. "index": false
  24. }
  25. }
  26. }
  27. }'

对于嵌套存在的字段, 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' --header 'Content-Type: application/json' --data '{
  2. "name": "commodity008001",
  3. "studymodel": "202306",
  4. "price": 123.12,
  5. "timestamp": "2023-05-25 19:11:35",
  6. "pic": "23/06/01/a123b1fde0428.jpg"
  7. }'

查询

可以通过URL路径区分不同索引

  • /_search 所有索引
  • /commodity008/_search commodity008索引
  • /commodity007,commodity008/_search commodity007 和 commodity008
  • /commodity*/_search 以 commodity 开头的索引

查询所有索引下的内容

  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' --data '{
  2. "query" : {
  3. "match" : {
  4. "name": "commodity008001"
  5. }
  6. }
  7. }'

带偏移和结果数量, 请求加上 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. }

参考

原文链接:https://www.cnblogs.com/milton/p/17461658.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号