Skip to content

Nacos Docker MySQL Configuration Fix

A comprehensive guide to resolving Nacos Docker container connection issues with local MySQL database in macOS environment, including common error analysis and solutions

· 2 min ·

Recently, while configuring Nacos service, I encountered a classic issue: Docker container couldn’t connect to local MySQL database, throwing No DataSource set error. After some troubleshooting, I finally found the complete solution.

Problem Symptoms#

After starting the Nacos container, I found the following errors in the logs:

Terminal window
Caused by: java.lang.IllegalStateException: No DataSource set
at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.jdbc.support.JdbcAccessor.obtainDataSource(JdbcAccessor.java:86)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:376)

This eventually led to Nacos startup failure:

Terminal window
ERROR Nacos failed to start, please see /home/nacos/logs/nacos.log for more details.

Environment Background#

Root Cause Analysis#

After investigation, I identified several main issues:

1. Configuration File Conflicts#

The project had two data source configuration files with inconsistent parameters:

application.properties
db.url.0=jdbc:mysql://host.docker.internal:3306/pmhub-nacos
db.user=root
db.password=Baoyu273511a
Terminal window
# custom.env
MYSQL_SERVICE_HOST=192.168.164.128
MYSQL_SERVICE_DB_NAME=nacos
MYSQL_SERVICE_PASSWORD=123456

2. Missing Database Schema#

Although the MySQL database existed, it was missing the required Nacos table structure.

3. Network Connection Issues#

The Docker container couldn’t properly access the host machine’s MySQL service.

Complete Solution#

Step 1: Unify Data Source Configuration#

First, modify the application.properties file to ensure correct network addressing:

src/conf/application.properties
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://host.docker.internal:3306/pmhub-nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=YourMySQLPassword
NOTE

In macOS Docker Desktop, use host.docker.internal to access host machine services

Step 2: Initialize Database Schema#

Nacos requires specific database table structure to function properly:

Terminal window
# Export SQL script from Nacos container
docker run --rm --entrypoint cat nacos/nacos-server:v2.2.3-slim /home/nacos/conf/mysql-schema.sql > /tmp/nacos-mysql.sql
# Import to local MySQL
mysql -h 127.0.0.1 -u root -pYourPassword pmhub-nacos < /tmp/nacos-mysql.sql

After import, the database should contain these 12 tables:

Step 3: Simplify Docker Run Configuration#

Start Nacos container using the simplest approach:

Terminal window
docker run -d \
--name nacos \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
-v /your/path/nacos/conf/application.properties:/home/nacos/conf/application.properties \
nacos/nacos-server:v2.2.3-slim

Step 4: Verify Startup Results#

Check Nacos startup logs:

Terminal window
docker logs nacos

Success indicator is seeing this message:

Terminal window
Nacos started successfully in cluster mode. use external storage

Verify Service Status#

1. Check Web Interface#

Terminal window
curl -s http://localhost:8848/nacos/ | grep -o "<title>.*</title>"
# Should return: <title>Nacos</title>

2. Check Database Connection#

Terminal window
mysql -h 127.0.0.1 -u root -p -e "USE pmhub-nacos; SELECT COUNT(*) as config_count FROM config_info;"

3. Access Console#

Open browser and visit: http://localhost:8848/nacos

Default login credentials:

Common Troubleshooting#

Issue 1: Still shows “No DataSource set”#

Solution: Check if configuration file is correctly mounted in container

Terminal window
# Check container configuration
docker exec nacos cat /home/nacos/conf/application.properties | grep db

Issue 2: Network Connection Failure#

Solution: Ensure correct network addressing is used

Issue 3: Database Tables Don’t Exist#

Solution: Re-import SQL script

Terminal window
# Check if tables exist
mysql -h 127.0.0.1 -u root -p -e "USE pmhub-nacos; SHOW TABLES;"
# If tables don't exist, re-import
docker run --rm --entrypoint cat nacos/nacos-server:v2.2.3-slim /home/nacos/conf/mysql-schema.sql | mysql -h 127.0.0.1 -u root -p pmhub-nacos

Summary#

Key points for Nacos Docker container connecting to local MySQL:

  1. Unified Configuration: Avoid conflicts between multiple configuration files
  2. Correct Network Address: Use host.docker.internal to access host machine
  3. Complete Schema: Ensure Nacos-required database tables are imported
  4. Accurate Mount Path: Ensure configuration files are correctly mounted in container

Following these steps should successfully resolve Nacos container connection issues with local MySQL. After successful startup, you should see the Nacos started successfully in cluster mode. use external storage message.


Hope this detailed solution helps you! If you have any other questions, feel free to leave a comment for discussion.