Docker deploys Nacos (stand-alone) and uses MySQL database to store configuration information

previous words

By default, you already know Docker and docker-compose
Nacos version: v2.2.3
MySQL version: 8.2.0

1. Download

  1. Open Nacosofficial website

Official website address: official website

Insert image description here

  1. click manual

  2. left sideNacos Docker
    Insert image description here

  3. Clone the project locally

# 克隆项目,如果提示连接不到 github 请自行解决
git clone https://github.com/nacos-group/nacos-docker.git

# 进入项目目录
cd nacos-docker

Insert image description here

注意:
If you get an error from git clone here: fatal: unable to access*** github Failure when receiving data from,
please refer to my article to configure the proxy (provided you have high technology, otherwise go to the github warehouse to download the source code or use a third-party github acceleration Service):
Click to jump

  1. You can see that the project has been downloaded locally

Insert image description here

  1. Use IDE to open the project for easy editing (here I use IDEA to open it)

Insert image description here

2. Create a database (taking MySQL8 as an example)

Because I already have a database locally and do not plan to use the nacos-docker project to help me create a database with Docker, so I need to modify its docker-compose.yaml file and manually create a database to store nacos data locally.
Insert image description here

  1. Find the official MySQL table structure file (see the picture directly)
    Insert image description here

  2. Create database manually (I use Navicat16 operation)

I don’t choose the sorting rule, just let it default.

Insert image description here

  1. Execute the official sql file

Insert image description here

  1. Put it into Navicat and execute it
    Insert image description here

  2. You can see the created data table
    Insert image description here

3. Modify project configuration

I configure it locally, so I only configure the stand-alone mode. Please configure multiple cluster modes by yourself and modify the relevant modes.

  1. Modify example/standalone-mysql-8.yamlfile

Remove the MySQL part of the configuration, because we no longer need docker to help me deploy the MySQL database.

Insert image description here

Code can be copied directly

version: "3.8"
services:
  nacos:
    image: nacos/nacos-server:${
    
    NACOS_VERSION}
    container_name: nacos-standalone-mysql # 这里写你想要的容器名称
    env_file:
      - ../env/nacos-standlone-mysql.env
    volumes:
      - ./standalone-logs/:/home/nacos/logs # 这里看你需求修改成你自己的目录映射,这里我演示,不改
    ports:
      - "8848:8848"
      - "9848:9848"
    restart: always
    network_mode: host # 加上这一行,使用 host 模式,这样子容器内部直接访问 localhost 就是宿主机本身,方便一些,反正端口不冲突

  1. Modify env/nacos-standlone-mysql.envfile

Insert image description here

Code:


PREFER_HOST_MODE=hostname
MODE=standalone # 单机模式
SPRING_DATASOURCE_PLATFORM=mysql # 使用 MySQL 作为存储
MYSQL_SERVICE_HOST=127.0.0.1 # 本机的话直接使用这个或者 localhost,刚刚设置的 host 网络模式可以直接访问到宿主机
MYSQL_SERVICE_DB_NAME=nacos_config # 存储 Nacos 配置信息的数据库名称
MYSQL_SERVICE_PORT=3306 # 数据库访问端口
MYSQL_SERVICE_USER=root # 访问你数据库的用户,默认用 root 即可,你设置有别的用户也可以
MYSQL_SERVICE_PASSWORD=root # root 密码
# 下面这行可以跟着我写,保险起见,加上时区,官方给的只有 UTC 时区在 .properties 中
MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true

  1. Modify mysql.envfile

Insert image description here

Code:

MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=nacos_config
MYSQL_USER=root
MYSQL_PASSWORD=root
LANG=C.UTF-8
  1. Reviseexample/init.d/application.properties

I didn't check carefully whether this file has been loaded (my intuition is that this file is not loaded, and the configuration of this file is used build/conf/application.properties. This file refers to externally defined variables and does not need to be modified manually). To be safe, I should modify it.

Insert image description here

After modification:

Insert image description here

db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
db.user.0=root
db.password.0=root

  1. After the above modifications, it is completed

4. Build image & run container

The following is the command to execute in the project root directory

Insert image description here

# 执行以下命令,会以standalone-mysql-8.yaml中定义的容器信息进行创建并运行容器
docker-compose -f example/standalone-mysql-8.yaml up # 为了方便看控制台,我们用这个演示
# 或者
docker-compose -f example/standalone-mysql-8.yaml up -d # 后台启动

After executing the command, the container deployment is successful:

Insert image description here

View the container:
Insert image description here

I am not using DockerDesktop here, so the interface is different. Mine is OrbStack, which saves resources and is faster than DockerDesktop. Except that the interface is not that beautiful and the port mapping is not as intuitive as DockerDesktop, everything else is fine.
Friends who want to know more can go to the official website: https://orbstack.dev/ or click on the official website link to jump

  • Note: OrbStack currently does not support Windows platform

Browser access address: http://localhost:8848/nacos

Or click directly to jump to the address

Insert image description here
Let’s create a new namespace test

Insert image description here
Insert image description here

View the database:

Insert image description here

5. Complete

At this point, we have completed deploying the Nacos container using Docker. If it helps you, don’t forget to like it~

Acho que você gosta

Origin blog.csdn.net/qq_17229141/article/details/134619635
Recomendado
Clasificación