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:
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:
ERROR Nacos failed to start, please see /home/nacos/logs/nacos.log for more details.Environment Background#
- Operating System: macOS (Darwin 25.0.0)
- Docker Version: Docker Desktop
- Nacos Version: nacos/nacos-server
.2.3-slim - MySQL Version: Local MySQL 8.0
- Database: pmhub-nacos
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:
db.url.0=jdbc:mysql://host.docker.internal:3306/pmhub-nacosdb.user=rootdb.password=Baoyu273511a# custom.envMYSQL_SERVICE_HOST=192.168.164.128MYSQL_SERVICE_DB_NAME=nacosMYSQL_SERVICE_PASSWORD=1234562. 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:
spring.datasource.platform=mysqldb.num=1db.url.0=jdbc:mysql://host.docker.internal:3306/pmhub-nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTCdb.user=rootdb.password=YourMySQLPasswordIn 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:
# Export SQL script from Nacos containerdocker run --rm --entrypoint cat nacos/nacos-server:v2.2.3-slim /home/nacos/conf/mysql-schema.sql > /tmp/nacos-mysql.sql
# Import to local MySQLmysql -h 127.0.0.1 -u root -pYourPassword pmhub-nacos < /tmp/nacos-mysql.sqlAfter import, the database should contain these 12 tables:
- config_info
- config_info_aggr
- config_info_beta
- config_info_tag
- config_tags_relation
- group_capacity
- his_config_info
- permissions
- roles
- tenant_capacity
- tenant_info
- users
Step 3: Simplify Docker Run Configuration#
Start Nacos container using the simplest approach:
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-slimStep 4: Verify Startup Results#
Check Nacos startup logs:
docker logs nacosSuccess indicator is seeing this message:
Nacos started successfully in cluster mode. use external storageVerify Service Status#
1. Check Web Interface#
curl -s http://localhost:8848/nacos/ | grep -o "<title>.*</title>"# Should return: <title>Nacos</title>2. Check Database Connection#
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:
- Username: nacos
- Password: nacos
Common Troubleshooting#
Issue 1: Still shows “No DataSource set”#
Solution: Check if configuration file is correctly mounted in container
# Check container configurationdocker exec nacos cat /home/nacos/conf/application.properties | grep dbIssue 2: Network Connection Failure#
Solution: Ensure correct network addressing is used
- macOS/Windows:
host.docker.internal - Linux: Use host IP or
--network host
Issue 3: Database Tables Don’t Exist#
Solution: Re-import SQL script
# Check if tables existmysql -h 127.0.0.1 -u root -p -e "USE pmhub-nacos; SHOW TABLES;"
# If tables don't exist, re-importdocker 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-nacosSummary#
Key points for Nacos Docker container connecting to local MySQL:
- Unified Configuration: Avoid conflicts between multiple configuration files
- Correct Network Address: Use
host.docker.internalto access host machine - Complete Schema: Ensure Nacos-required database tables are imported
- 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.
Nacos
MySQL