• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

用docker部署mysql、redis和nacos

互联网 diligentman 5天前 7次浏览

开发Spring Cloud Alibaba微服务应用时,mysql、redis和nacos要先搭建好,本文使用docker搭建;

nacos有多种部署方式,见官方文档:nacos docker,这里用的是单机模式mysql模式,所以会与mysql会有关联;

docker-compose.yaml定义文件如下:

version: "3.6"
services:
  mysql:
    image: mysql:${MYSQL_VERSION}
    container_name: mysql
    ports:
      - "${MYSQL_HOST_PORT}:3306"    volumes:
      - ${MYSQL_CONF_FILE}:/etc/mysql/conf.d/mysql.cnf:ro      - ${MYSQL_DATA_DIR}:/var/lib/mysql/:rw    restart: on-failure
    networks:
      - default    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      TZ: Asia/Shanghai

  redis:
    image: redis:${REDIS_VERSION}
    container_name: redis
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ${REDIS_LOG_DIR}:/var/log/redis/:rw      - ${REDIS_CONF_FILE}:/usr/local/etc/redis/redis.conf:ro      - ${REDIS_DIR}:/var/lib/redis/6379/:rw    command:
      - /usr/local/etc/redis/redis.conf    restart: on-failure
    networks:
      - default    ports:
      - "${REDIS_HOST_PORT}:6379" 
  nacos:
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos
    environment:
      - "PREFER_HOST_MODE=${PREFER_HOST_MODE}"      - "MODE=${MODE}"      - "SPRING_DATASOURCE_PLATFORM=${SPRING_DATASOURCE_PLATFORM}"      - "MYSQL_SERVICE_HOST=${MYSQL_SERVICE_HOST}"      - "MYSQL_SERVICE_DB_NAME=${MYSQL_SERVICE_DB_NAME}"      - "MYSQL_SERVICE_PORT=${MYSQL_SERVICE_PORT}"      - "MYSQL_SERVICE_USER=${MYSQL_SERVICE_USER}"      - "MYSQL_SERVICE_PASSWORD=${MYSQL_SERVICE_PASSWORD}"    volumes:
      - ${NACOS_LOG_DIR}:/home/nacos/logs:rw      - ${NACOS_PROPERTIES}:/home/nacos/init.d/custom.properties    depends_on:
      - mysql    ports:
      - 8848:8848    restart: on-failure
    
networks:
  default: 

环境变量.env:

################################################
###       environment config file            ###
################################################

#################### MySQL #####################
MYSQL_VERSION=5.7.24
MYSQL_HOST_PORT=3306
MYSQL_ROOT_PASSWORD=123456
MYSQL_DATA_DIR=./mysql
MYSQL_CONF_FILE=./conf/mysql.cnf


#################### Redis #####################
REDIS_VERSION=5.0.9
REDIS_HOST_PORT=6379
REDIS_CONF_FILE=./conf/redis.conf
REDIS_LOG_DIR=./log/redis
REDIS_DIR=./redis

#################### NACOS #####################
NACOS_VERSION=1.2.1
NACOS_LOG_DIR=./log/nacos
PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_HOST=mysql #此处要主要的是要写mysql服务的host,不能写127.0.0.1;直接写mysql即可;
MYSQL_SERVICE_DB_NAME=nacos
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=123456
NACOS_PROPERTIES=./conf/custom.properties 

文件树结构如下:

├── conf
│   ├── conf.d
│   ├── custom.properties
│   ├── mysql.cnf
│   ├── nginx.conf
│   └── redis.conf
├── docker-compose.yml
├── log
│   ├── nacos
│   └── redis
├── mysql
│   ├── auto.cnf
│   ├── ca-key.pem
│   ├── ca.pem
│   ├── client-cert.pem
│   ├── client-key.pem
│   ├── ib_buffer_pool
│   ├── ib_logfile0
│   ├── ib_logfile1
│   ├── ibdata1
│   ├── ibtmp1
│   ├── mysql
│   ├── mysql.error.log
│   ├── mysql.slow.log
│   ├── nacos
│   ├── performance_schema
│   ├── private_key.pem
│   ├── public_key.pem
│   ├── server-cert.pem
│   ├── server-key.pem
│   └── sys
├── redis
│   ├── dump.rdb
│   └── test.txt
└── www

值得注意的是,这里nacos版本是1.2.1,同时初始化的sql脚本nacos-mysql,可以从nacos-mysql脚本取得;

启动服务:

docker-compose -f docker-compose.yml up -d
Starting redis ... done
Starting mysql ... done
Starting nacos ... done

查看服务和端口情况:

lsof -nP|grep LISTEN
idea        411 bigticket  122u     IPv4 0xf5b8d5ea88576b3b          0t0                 TCP 127.0.0.1:6942 (LISTEN)
idea        411 bigticket  744u     IPv4 0xf5b8d5ea89a9225b          0t0                 TCP 127.0.0.1:63342 (LISTEN)
idea        411 bigticket 1049u     IPv4 0xf5b8d5ea8b092cfb          0t0                 TCP 127.0.0.1:49427 (LISTEN)
privoxy     656 bigticket    3u     IPv4 0xf5b8d5ea896a4503          0t0                 TCP 127.0.0.1:1087 (LISTEN)
ss-local    663 bigticket    8u     IPv4 0xf5b8d5ea86d77ecb          0t0                 TCP 127.0.0.1:1086 (LISTEN)
com.docke  1141 bigticket    7u     IPv4 0xf5b8d5ea808b6de3          0t0                 TCP 127.0.0.1:49948 (LISTEN)
com.docke  1145 bigticket   14u     IPv4 0xf5b8d5ea8976d25b          0t0                 TCP 127.0.0.1:6443 (LISTEN)
com.docke  1145 bigticket   21u     IPv6 0xf5b8d5ea88f29063          0t0                 TCP *:6379 (LISTEN)
com.docke  1145 bigticket   24u     IPv6 0xf5b8d5ea88f26ba3          0t0                 TCP *:3306 (LISTEN)
com.docke  1145 bigticket   25u     IPv6 0xf5b8d5ea88f25943          0t0                 TCP *:8848 (LISTEN)

mysql和redis用客户端测试连接,顺利连上;

浏览器输入http://127.0.0.1:8848/nacos/,初始账号密码:nacos/nacos

用docker部署mysql、redis和nacos


喜欢 (0)