[docker-compose] nacos + mysql8

在使用docker-compose配置Nacos与MySQL8时,遇到No DataSource set的问题,原因是未设置MYSQL_SERVICE_DB_PARAM。解决方案包括提供完整的docker-compose.yml配置,设置文件目录结构,并通过init.sql自动初始化nacos_config数据库。注意,如需本地访问,需暴露MySQL端口,并解决navicat等客户端连接慢的问题。
摘要由CSDN通过智能技术生成

第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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值