Nacos 安装与鉴权配置指南

318 字
2 分钟
Nacos 安装与鉴权配置指南

一、安装#

Terminal window
# 解压到安装目录
tar -zxvf nacos-server-x.x.x.tar.gz -C /opt/

目录说明:

目录说明
bin启动脚本
conf配置文件
target启动依赖
data运行数据

二、启动#

Terminal window
# 单机模式启动
sh /opt/nacos/bin/startup.sh -m standalone

三、访问#

  • 地址:http://ip:8848/nacos/
  • 默认账号密码:nacos / nacos
Note

阿里云服务器需在安全组中开放 8848 端口。

四、持久化到 MySQL#

默认使用内嵌数据库,重启后数据丢失。生产环境建议持久化到 MySQL(5.6.5+)。

Note

尚未安装 MySQL?参见:CentOS MySQL 5.7 安装Ubuntu MySQL 8.1 安装

步骤:

  1. 在 MySQL 中创建 nacos 数据库,执行 nacos/conf/nacos-mysql.sql 初始化
  2. 修改 conf/application.properties,添加以下配置:
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=nacos
db.password.0=your-password
  1. 重启 Nacos

五、开启鉴权#

修改 conf/application.properties 文件。

开启鉴权#

nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=true

配置 Token 密钥#

Warning

2.2.0.1 版本后必须手动配置 Token 密钥,否则无法启动。密钥需为 Base64 编码字符串,原始长度不低于 32 字符。

# 2.1.0 版本后使用此配置项
nacos.core.auth.plugin.nacos.token.secret.key=VGhpc0lzTXlDdXN0b21TZWNyZXRLZXkwMTIzNDU2Nzg=

生成 Base64 密钥:

Terminal window
echo -n 'YourCustomSecretKey0123456789012' | base64

配置节点身份标识#

nacos.core.auth.server.identity.key=example
nacos.core.auth.server.identity.value=example

完整鉴权配置示例#

### 开启鉴权
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=true
nacos.core.auth.caching.enabled=true
nacos.core.auth.enable.userAgentAuthWhite=false
### 节点身份标识(集群节点间通信白名单)
nacos.core.auth.server.identity.key=example
nacos.core.auth.server.identity.value=example
### Token 配置
nacos.core.auth.plugin.nacos.token.cache.enable=false
nacos.core.auth.plugin.nacos.token.expire.seconds=18000
nacos.core.auth.plugin.nacos.token.secret.key=VGhpc0lzTXlDdXN0b21TZWNyZXRLZXkwMTIzNDU2Nzg=

配置完成后重启 Nacos 即可生效。

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!

赞助
Nacos 安装与鉴权配置指南
https://blog.olinl.com/posts/nacos-guide/
作者
顾拾柒
发布于
2026-01-22
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
顾拾柒
The world is big, you have to go and see.
公告
欢迎来到我的博客!
分类
标签
站点统计
文章
81
分类
7
标签
158
总字数
83,114
运行时长
0
最后活动
0 天前
站点信息
构建平台
Vercel
博客版本
Firefly v6.12.1
文章许可
CC BY-NC-SA 4.0

文章目录