1.下面编写一个 compose.yaml 文件,并使用 Compose 部署 MySQL 8.0 服务器,该文件的内容如下

[root@host1 ~]# vi compose.yaml
[root@host1 ~]# cat compose.yaml
services:
  mysql:  # 与services保持2个空格缩进(子项)
    image: mysql:8  # 与mysql保持2个空格缩进(孙项,共4个空格)
    container_name: mysql8
    ports:
      - "3306:3306"  # 列表项与ports保持2个空格缩进(共6个空格)
    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
    environment:
      - MYSQL_ROOT_PASSWORD=root
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - mysql8-data:/var/lib/mysql

volumes:  # 与services同级,无缩进
  mysql8-data:  # 与volumes保持2个空格缩进
    driver: local

2.解析该配置文件

[root@host1 ~]# docker compose config
name: root
services:
  mysql:
    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
    container_name: mysql8
    environment:
      MYSQL_ROOT_PASSWORD: root
    image: mysql:8
    networks:
      default: null
    ports:
      - mode: ingress
        target: 3306
        published: "3306"
        protocol: tcp
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime
        read_only: true
        bind:
          create_host_path: true
      - type: volume
        source: mysql8-data
        target: /var/lib/mysql
        volume: {}
networks:
  default:
    name: root_default
volumes:
  mysql8-data:
    name: root_mysql8-data
    driver: local

3.启动该服务

[root@host1 ~]# docker compose up -d
[+] Running 11/11
 ✔ mysql Pulled                                                                                67.6s 
   ✔ 500d7b2546c4 Pull complete                                                                12.2s 
   ✔ a4e035766269 Pull complete                                                                12.2s 
   ✔ 328796243e0b Pull complete                                                                12.3s 
   ✔ 998cf6c9454c Pull complete                                                                13.0s 
   ✔ b702ebe6dd8f Pull complete                                                                13.0s 
   ✔ 7894cca000d1 Pull complete                                                                13.1s 
   ✔ 28b5340945f9 Pull complete                                                                16.4s 
   ✔ f3b274c62c3c Pull complete                                                                16.4s 
   ✔ 318326631da0 Pull complete                                                                58.7s 
   ✔ 582874d21796 Pull complete                                                                58.7s 
[+] Running 3/3
 ✔ Network root_default       Created                                                           0.1s 
 ✔ Volume "root_mysql8-data"  Created                                                           0.0s 
 ✔ Container mysql8           Started                                                           6.6s

4.停止并清理服务

[root@host1 ~]# docker compose down --volumes
[+] Running 3/3
 ✔ Container mysql8         Removed                                                             0.0s 
 ✔ Volume root_mysql8-data  Removed                                                             0.0s 
 ✔ Network root_default     Removed                                                             0.2s

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐