Docker starts nacos with an in-memory database by default. After restarting the docker container, the nacos configuration will be lost, which is very inconvenient. So it needs to be modified to use Mysql as the storage of nacos.
1. Database
Create a mysql database, the process is omitted, and the mysql script of nacos is imported into the database.
The mysql script can be found in the nacos container/home/nacos/conf/ mysql-schema.sql
# 启动一个临时nacos容器
~] docker run --name nacos-server -e MODE=standalone -d nacos/nacos-server:v2.2.3
ed3b5a88a244
# 进入容器
~] docker exec -it ed3b5a88a244
# 找到mysql脚本
[root@ed3b5a88a244 conf]# pwd
/home/nacos/conf
[root@ed3b5a88a244 conf]# ls
1.4.0-ipv6_support-update.sql announcement.conf application.properties derby-schema.sql mysql-schema.sql nacos-logback.xml
2. Directory mapping
Copy the configuration file, data, and log directory from the temporary nacos container to the mapping directory
docker cp 9c37c28fd155:/home/nacos/conf /Users/domino/files/docker-data/nacos-mysql/
docker cp 9c37c28fd155:/home/nacos/logs /Users/domino/files/docker-data/nacos-mysql/
docker cp 9c37c28fd155:/home/nacos/data /Users/domino/files/docker-data/nacos-mysql/
3. Modify the configuration
Modify the application.properties configuration file of the mapping directory
Modify the following configurations
# 数据源类型为 mysql
spring.sql.init.platform=${SPRING_DATASOURCE_PLATFORM:mysql}
# 数据库 ip port name 等
db.url.0=jdbc:mysql://${MYSQL_SERVICE_HOST:172.17.0.1}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME:nacos}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false}
# 数据库用户名
db.user.0=${MYSQL_SERVICE_USER:root}
# 数据库密码
db.password.0=${MYSQL_SERVICE_PASSWORD:123456}
4. Start the nacos container
docker run -d -e MODE=standalone -p 8848:8848 -p 9848:9848 -v /Users/domino/files/docker-data/nacos-mysql/conf:/home/nacos/conf -v /Users/domino/files/docker-data/nacos-mysql/logs:/home/nacos/logs -v /Users/domino/files/docker-data/nacos-mysql/data:/home/nacos/data --name nacos-mysql --restart=always nacos/nacos-server:v2.2.3
PS: It must be noted that some documents only say to configure port 8848, but for nacos versions above 2.x, port 9848 must also be configured, otherwise the configuration may not be read, refer to the document https://blog. csdn.net/hmq1350167649/article/details/122432510
After startup, visit http://localhost:8848/nacos to visit the nacos homepage, and use nacos/nacos to log in
5. Restart the test
The purpose of using Mysql as the nacos container storage is to prevent configuration loss after restarting the nacos container. After starting, you can add a configuration, then restart the nacos container, and log in to nacos again after restarting. If the configuration is not lost, it is considered a success.
Reference documents:
https://developer.aliyun.com/article/972817