在 Mac 上安装并使用 Elasticsearch 主要有两种方法:一种是使用 Homebrew 包管理器,另一种是直接从官网下载压缩包进行手动安装
方法一:使用 Homebrew 安装
- 安装 Homebrew
如果在 Mac 上还没有安装 Homebrew,可以通过终端执行以下命令:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
根据提示操作,输入电脑密码,就完成了安装
- 安装 Elasticsearch
- 添加 Elastic 的 Homebrew 源(tap):
brew tap elastic/tap
- 安装 Elasticsearch:
brew install elastic/tap/elasticsearch-full
- 添加 Elastic 的 Homebrew 源(tap):
耐心等待一段时间后,就完成了安装
- 启动 Elasticsearch
使用 Homebrew 安装后,可以用 brew services
来启动 Elasticsearch,这样它会在后台持续运行,并且可以设置为开机自启:
brew services start elastic/tap/elasticsearch-full
方法二:手动下载安装
- 安装 Java 环境
Elasticsearch 是基于 Java 开发的,需要 Java 环境才能运行。幸运的是,从较新版本开始,Elasticsearch 的压缩包内已经内置了 JDK,所以现在无需手动安装 Java。
-
下载 Elasticsearch
-
访问 Elastic 官网的下载页面:https://www.elastic.co/cn/downloads/elasticsearch
-
在页面上找到 macOS aarch64 (Apple Silicon) 或 macOS x86_64 (Intel) 版本,根据 Mac 芯片类型选择并下载
.tar.gz
格式的压缩包。
-
-
解压并运行
下载完毕后解压压缩包并进入解压后的目录,双击运行 elasticsearch 即可启动 Elasticsearch
启动后的重要步骤:验证和使用
无论使用的哪种方式安装,当第一次启动 Elasticsearch 时,请密切关注终端的输出信息
- 获取密码和 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
- 验证 Elasticsearch 是否正在运行
打开一个新的终端窗口,使用 curl
命令来测试。系统会提示您输入 elastic
用户的密码。
curl --insecure -u elastic https://localhost:9200
--insecure
: 因为 Elasticsearch 默认使用自签名 SSL 证书,这个参数是告诉curl
信任它。-u elastic
: 指定用户名为elastic
。
如果一切正常,您会看到一段 JSON 格式的输出,包含了您的节点名称、集群名称和版本信息等,这表明您的 Elasticsearch 实例已经成功运行了!
- 停止 Elasticsearch
-
手动安装: 在 Elasticsearch 运行的终端窗口中按
Ctrl + C
。 -
Homebrew 安装:
brew services stop elastic/tap/elasticsearch-full
评论区
请登录后发表评论