Mac 快速安装使用 Elasticsearch

Mac上安装Elasticsearch有两种方法:使用Homebrew(需先安装Homebrew,添加Elastic源后安装)或手动下载压缩包(内置JDK,解压即用)。安装后需注意首次启动时生成的elastic用户密码和注册令牌,可通过curl验证运行状态,Homebrew安装支持后台服务管理。

作品集: Elastic Stack
作者头像
LumiBee
7 天前 · 53 0
分享

在 Mac 上安装并使用 Elasticsearch 主要有两种方法:一种是使用 Homebrew 包管理器,另一种是直接从官网下载压缩包进行手动安装

方法一:使用 Homebrew 安装

  1. 安装 Homebrew

如果在 Mac 上还没有安装 Homebrew,可以通过终端执行以下命令:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

根据提示操作,输入电脑密码,就完成了安装

  1. 安装 Elasticsearch
    1. 添加 Elastic 的 Homebrew 源(tap):brew tap elastic/tap
    2. 安装 Elasticsearch:brew install elastic/tap/elasticsearch-full

耐心等待一段时间后,就完成了安装

  1. 启动 Elasticsearch

使用 Homebrew 安装后,可以用 brew services 来启动 Elasticsearch,这样它会在后台持续运行,并且可以设置为开机自启:

brew services start elastic/tap/elasticsearch-full

方法二:手动下载安装

  1. 安装 Java 环境

Elasticsearch 是基于 Java 开发的,需要 Java 环境才能运行。幸运的是,从较新版本开始,Elasticsearch 的压缩包内已经内置了 JDK,所以现在无需手动安装 Java。

  1. 下载 Elasticsearch

    1. 访问 Elastic 官网的下载页面:https://www.elastic.co/cn/downloads/elasticsearch

    2. 在页面上找到 macOS aarch64 (Apple Silicon) 或 macOS x86_64 (Intel) 版本,根据 Mac 芯片类型选择并下载 .tar.gz 格式的压缩包。

  2. 解压并运行

下载完毕后解压压缩包并进入解压后的目录,双击运行 elasticsearch 即可启动 Elasticsearch

image-20250629153051552

启动后的重要步骤:验证和使用

无论使用的哪种方式安装,当第一次启动 Elasticsearch 时,请密切关注终端的输出信息

  1. 获取密码和 Enrollment Token

从 8.0 版本开始,Elasticsearch 默认会开启安全功能。首次启动时,它会自动为超级用户 elastic 生成一个密码,并为 Kibana 生成一个注册令牌 (enrollment token)。

您会在终端看到类似下面的输出,请务必将它们复制并保存下来

→ Elasticsearch security features have been automatically configured!
  ✅ Authentication is enabled and using native realms.
  ✅ TLS is enabled for HTTP and transport traffic.
  ℹ️  Password for the elastic user is: YOUR_GENERATED_PASSWORD
  ℹ️  Your enrollment token is: YOUR_ENROLLMENT_TOKEN
  • YOUR_GENERATED_PASSWORD 就是您之后访问 Elasticsearch 需要的密码。
  • 如果您打算使用 Kibana,YOUR_ENROLLMENT_TOKEN 将会非常有用。

万一忘记了密码怎么办? 如果终端窗口已关闭且您没有保存密码,可以运行以下命令来重置:

  • Homebrew 安装:

    /usr/local/opt/elasticsearch-full/bin/elasticsearch-reset-password -u elastic
    
  • 手动安装:

    # 在您的 elasticsearch 解压目录下运行
    ./bin/elasticsearch-reset-password -u elastic
    
  1. 验证 Elasticsearch 是否正在运行

打开一个新的终端窗口,使用 curl 命令来测试。系统会提示您输入 elastic 用户的密码。

curl --insecure -u elastic https://localhost:9200
  • --insecure: 因为 Elasticsearch 默认使用自签名 SSL 证书,这个参数是告诉 curl 信任它。
  • -u elastic: 指定用户名为 elastic

如果一切正常,您会看到一段 JSON 格式的输出,包含了您的节点名称、集群名称和版本信息等,这表明您的 Elasticsearch 实例已经成功运行了!

image-20250629153754357

  1. 停止 Elasticsearch
  • 手动安装: 在 Elasticsearch 运行的终端窗口中按 Ctrl + C

  • Homebrew 安装:

    brew services stop elastic/tap/elasticsearch-full
    
阅读量: 53

评论区

登录后发表评论

正在加载评论...
相关阅读

暂无相关文章推荐

返回首页浏览更多文章