nacos 2.5.1 单机模式部署并开启安全认证

Last Modified: 2025/07/08

1、下载二进制包

进入 nacos 下载页面,可以找到 2.x 下载链接,本文下载的版本是2.5.1,点击这里直接下载

2、使用 MySQL 作为数据库

下载完成之后解压并进入解压后的目录,可以看到 conf 文件夹,这里面存放的是配置文件,其中我们需要重点关注就两个:

  • conf/application.properties
  • conf/mysql-schema.sql

nacos 可作为注册中心,同时还可作为配置中心,而且 nacos 还提供了一个配套的 web 管理界面端,用来支持配置文件的管理和服务的管理。数据的持久化(例如配置中心的配置)则依赖于数据库。

Derby 是 Apache 软件基金会提供的一个开源的、纯Java编写的关系型数据库,它被直接打包在Nacos 服务器中,无需单独安装和配置。但是 Derby 数据库仅适用于测试和轻量级应用,因为它不支持高并发访问,且数据存储容量有限。因此我们这里使用 mysql 数据库,这也是为什么要关注 conf/mysql-schema.sql。

使用 mysql 数据库则需要修改 conf/application.properties 中的配置。首先建立一个数据库,假设数据库的名称为 nacos25,接下来只需将 conf/mysql-schema.sql 导入数据库即可。

修改的配置内容如下:

spring.sql.init.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://192.168.2.xx:3306/nacos25?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
### 数据库的账号密码根据实际情况配置
db.user.0=root
db.password.0=123456789xx

3、开启安全认证

开启安全认证主要涉及以下几个属性的配置:

### If turn on auth system:
nacos.core.auth.enabled=true
### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=ZF2mB/wsYH45EDifS69QnmSdOCOh3ttQh/b9Gd9xYLI=
nacos.core.auth.server.identity.value=PFDaMC0/zdeQQlctlLvWDDPw3w6Cpfels+DXIhfdtp8=
nacos.core.auth.plugin.nacos.token.secret.key=k1uIVsmSKe1M5Unf+cl7BkYyIAiLjzRNqoPnHHjMMAk=

可能有人会有疑问,单机模式为什么还需要配置以下这三个属性:

  • nacos.core.auth.server.identity.key
  • nacos.core.auth.server.identity.value
  • nacos.core.auth.plugin.nacos.token.secret.key

当我们配置了 nacos.core.auth.enabled=true,如果不配置这些属性,启动服务的时候会抛异常如下,最终导致启动失败!

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.plugin.auth.impl.token.impl.JwtTokenManager]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: the length of secret key must great than or equal 32 bytes; And the secret key  must be encoded by base64.Please see https://nacos.io/docs/latest/manual/admin/auth/
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:226)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:302)
        ... 112 common frames omitted
Caused by: java.lang.IllegalArgumentException: the length of secret key must great than or equal 32 bytes; And the secret key  must be encoded by base64.Please see https://nacos.io/docs/latest/manual/admin/auth/

以上三个属性的要求都是不低于32位且 base64 编码,可以使用以下命令生成:

head -c 32 /dev/urandom | base64

4、启动服务

sh startup.sh -m standalone

服务启动完毕后,可以访问 http://localhost:8848/nacos 来访问 web 管理端。

开启安全认证之后,首次访问 web 管理端会让你初始化密码,之后访问便需要登录才能访问。不过即便开启了安全认证,nacos 依然不建议开放 8848 端口 (默认的web管理端端口)。

有问题吗?点此反馈!

温馨提示:反馈需要登录