docker中持久化nacos到mysql

本文介绍如何将 Nacos 2.0.2 的配置信息持久化到 MySQL 5.7 数据库中。主要内容包括:进入 Docker 容器,修改 application.properties 文件的具体步骤及配置细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用的mysql为5.7      nacos为2.0.2

网上查的说MySQL8   在nacos中需要改配置所以就用的MySQL5.7

持久化

第一步

进入  容器   docker exec -it nacos /bin/bash

修改conf下的application.properties文件

第二步

我是直接创建了一个新的application.properties文件

# spring
server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
server.contextPath=/nacos
server.port=${NACOS_APPLICATION_PORT:8848}
spring.datasource.platform=${SPRING_DATASOURCE_PLATFORM:""}
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.168.**.***:3306/nacos_config?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT
db.user=root
db.password=123456
### The auth system to use, currently only 'nacos' is supported:
nacos.core.auth.system.type=${NACOS_AUTH_SYSTEM_TYPE:nacos}


### The token expiration in seconds:
nacos.core.auth.default.token.expire.seconds=${NACOS_AUTH_TOKEN_EXPIRE_SECONDS:18000}

### The default token:
nacos.core.auth.default.token.secret.key=${NACOS_AUTH_TOKEN:SecretKey012345678901234567890123456789012345678901234567890123456789}

### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=${NACOS_AUTH_CACHE_ENABLE:false}
nacos.core.auth.enable.userAgentAuthWhite=${NACOS_AUTH_USER_AGENT_AUTH_WHITE_ENABLE:false}
nacos.core.auth.server.identity.key=${NACOS_AUTH_IDENTITY_KEY:serverIdentity}
nacos.core.auth.server.identity.value=${NACOS_AUTH_IDENTITY_VALUE:security}
server.tomcat.accesslog.enabled=${TOMCAT_ACCESSLOG_ENABLED:false}
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
# default current work dir
server.tomcat.basedir=
## spring security config
### turn off security
nacos.security.ignore.urls=${NACOS_SECURITY_IGNORE_URLS:/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**}
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false

nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true

修改的是这个

其中这里的url.0 后面的ip使用Linux的ip就行 

### 在 Docker 中安装和配置 Nacos 并将其数据持久化存储到 MySQL 为了实现这一目标,需遵循一系列操作来确保 NacosMySQL 的正确集成。 #### 准备工作 创建 `nacos-mysql.sql` 文件用于初始化 MySQL 数据库结构。该 SQL 脚本应放置于 `docker-compose.yml` 所在目录下[^2]。此脚本通常包含了创建名为 `nacos_config` 的数据库以及相应的表结构所需的命令。 #### 编写 docker-compose.yml 文件 编写或修改 `docker-compose.yml` 文件以定义服务组合,其中包括 NacosMySQL 两个容器实例: ```yaml version: '3' services: mysql: image: mysql:8.0 container_name: nacos-mysql environment: MYSQL_ROOT_PASSWORD: root_password MYSQL_DATABASE: nacos_config MYSQL_USER: nacos MYSQL_PASSWORD: nacos_password volumes: - ./initdb.d:/docker-entrypoint-initdb.d ports: - "3307:3306" nacos: image: nacos/nacos-server container_name: nacos_server depends_on: - mysql environment: MODE: standalone SPRING_DATASOURCE_PLATFORM: mysql MYSQL_SERVICE_HOST: mysql MYSQL_SERVICE_PORT: 3306 MYSQL_SERVICE_DB_NAME: nacos_config MYSQL_SERVICE_USER: nacos MYSQL_SERVICE_PASSWORD: nacos_password ports: - "8848:8848" ``` 上述 YAML 文件中设置了环境变量以便让 Nacos 连接到 MySQL 实例,并指定了必要的连接参数[^3]。 #### 初始化 MySQL 数据库 当首次启动时,Docker Compose 将自动执行位于 `/docker-entrypoint-initdb.d/` 目录下的所有 `.sql` 文件中的语句,因此只需确保 `nacos-mysql.sql` 存在于本地路径 `./initdb.d/` 下即可完成数据库的初始化设置。 #### 启动服务 通过运行如下命令可以一次性启动所有的服务组件: ```bash docker-compose up -d ``` 这会以后台模式启动所有指定的服务,并使它们相互协作起来[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值