第N次踩到同一个坑....,
不得不写点什么了.
如果你是mysql8,请记得把MYSQL_SERVICE_DB_PARAM给写上,不然就会出现 No DataSource set, 然后开始怀疑人生
MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
完整的配置
docker-compose.yml
services:
mysql8:
image: mysql:8.0.20
restart: always
container_name: nacos_mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=256M
--skip-name-resolve
--init-file /docker-entrypoint-initdb.d/init.sql
volumes:
- /etc/localtime:/etc/localtime:ro
- ./mysql/data:/var/lib/mysql
- ./mysql/log:/var/log/mysql
- ./mysql/conf.d:/etc/mysql/conf.d
- ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
## 指定ip
networks:
network:
ipv4_address: 10.10.10.20
nacos:
container_name: nacos
hostname: nacos
image: nacos/nacos-server:v2.1.0
restart: always
depends_on:
- mysql8
environment:
- MODE=standalone
- PREFER_HOST_MODE=hostname
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=mysql8
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_DB_NAME=nacos_config
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=123456
# 连接mysql8时需要设置
- MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
volumes:
- ./nacos/logs:/home/nacos/logs
ports:
- 8848:8848
## 指定ip
networks:
network:
ipv4_address: 10.10.10.10
## 创建网络
networks:
network:
ipam:
config:
- subnet: "10.10.10.0/24"
文件目录结构
[root@localhost nacos]# tree -L 2
├── docker-compose.yml
├── init.sh
├── mysql
│ ├── conf.d
│ ├── data
│ └── log
├── nacos
│ └── logs
└── sql
└── init.sql
自动初始化nacos_config数据库
脚本位置在 ./sql/init.sql
喜欢手动建库表的同学可以不加这2个
command:
--init-file /docker-entrypoint-initdb.d/init.sql
volumes:
- ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
init.sql
不严谨的初始化脚本
create database if not exists nacos_config default character set UTF8mb4 collate utf8mb4_unicode_ci;
use nacos_config;
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.or