How to Persist Nacos Data with MySQL for Production-Ready Spring Cloud
This article explains why the default single‑node Nacos setup is unsuitable for production, walks through configuring MySQL as a centralized data store, provides step‑by‑step configuration examples, and discusses the trade‑offs of using centralized storage versus distributed algorithms.
After covering Nacos as a service registry and configuration center in earlier posts, this article shifts focus to production deployment, specifically data persistence and recommended configurations.
Data Persistence
The default single‑node mode of Nacos is only appropriate for learning or testing; it cannot meet high‑availability requirements in production. Running multiple single‑node instances does not solve consistency issues, so a clustered deployment with a centralized MySQL store is required.
Step 1 : Install MySQL (version 5.6.5 or higher).
Step 2 : Initialize the database using the nacos-mysql.sql script located in the conf directory of the Nacos package.
Step 3 : Edit the conf/application.properties file to add MySQL datasource configuration, including URL, username, and password. Example configuration:
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=With these settings, Nacos stores its data in MySQL and can be started in single‑node mode while persisting configuration data to the database.
Deep Reflection
Unlike distributed middleware such as etcd, Consul, or Zookeeper that use consensus algorithms, Nacos relies on a centralized database to achieve consistency, making it easier to understand but introducing a single point of failure; therefore, MySQL itself must be deployed with high availability.
Reference
Nacos official documentation
Code Example
The client code referenced in this article is available in the alibaba-nacos-config-client project.
GitHub: https://github.com/dyc87112/SpringCloud-Learning/
Gitee: https://gitee.com/didispace/SpringCloud-Learning/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
