如何使用Docker启动和初始化Mysql 8?

huangapple go评论63阅读模式
英文:

How to start and initialize Mysql 8 using Docker?

问题

我一直在收到这个错误:构建Docker镜像时,从我的Dockerfile运行docker-compose up,然后运行docker-compose up时,出现错误mysqld: File '/var/lib/mysql/auto.cnf' not found (OS errno 13 - Permission denied)。我使用以下命令构建Docker镜像:docker buildx build --platform linux/amd64 --load -t mysqltest -f path/to/Dockerfile .。我使用buildx构建Docker镜像,因为我在运行具有M1芯片的Mac。我希望这个镜像能够在Intel机器上运行。以下是Docker文件:
Dockerfile:

FROM mysql/mysql-server:8.0.32

# needed for initialization
ENV MYSQL_ROOT_PASSWORD=test_password
ENV MYSQL_DATABASE=new_database
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=test_password

RUN microdnf update && microdnf -y install vim

EXPOSE 3306
CMD ["mysqld"]

docker-compose.yml

version: "3.7"

networks:
  backend:
    driver: ${NETWORKS_DRIVER}

services:
  
...

  db:
    image: mysqltest
    container_name: db
    build:
      context: .
      dockerfile: ./path/to/Dockerfile
    restart: always
    env_file:
      - .env
    ports:
      - "3306:3306"
    expose:
      - "3306"
    volumes:
      - db-volume:/var/lib/mysql
      - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
      - ./docker/db/configs:/etc/mysql
    networks:
      - backend
...

volumes:
  db-volume:
英文:

I keep getting this error: mysqld: File '/var/lib/mysql/auto.cnf' not found (OS errno 13 - Permission denied) when building the docker image from my Dockerfile and then running docker-compose up using my docker-compose.yml file. I'm using this command to build the docker image docker buildx build --platform linux/amd64 --load -t mysqltest -f path/to/Dockerfile . I'm using buildx to build the docker image because i'm running a mac with m1 chip. I want this image to be able to run on an intel machine. Below are the docker files:
Dockerfile:

FROM mysql/mysql-server:8.0.32

# needed for intialization
ENV MYSQL_ROOT_PASSWORD=test_password
ENV MYSQL_DATABASE=new_database
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=test_password

RUN microdnf update && microdnf -y install vim

EXPOSE 3306
CMD ["mysqld"]

docker-compose.yml

version: "3.7"

networks:
  backend:
    driver: ${NETWORKS_DRIVER}

services:
  
...

  db:
    image: mysqltest
    container_name: db
    build:
      context: .
      dockerfile: ./path/to/Dockerfile
    restart: always
    env_file:
      - .env
    ports:
      - "3306:3306"
    expose:
      - "3306"
    volumes:
      - db-volume:/var/lib/mysql
      - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
      - ./docker/db/configs:/etc/mysql
    networks:
      - backend
...

volumes:
  db-volume:

答案1

得分: 0

我最终通过在Dockerfile中注释掉CMD ["mysqld"]并在docker-compose.yml中将卷更改为全新的卷来使其工作。看起来是这样的:

db:/var/lib/mysql

我还注释掉了

#      - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
#      - ./docker/db/configs:/etc/mysql

所以这是最终的docker-compose.yml文件:

version: "3.7"

networks:
  backend:
    driver: ${NETWORKS_DRIVER}

services:
  ...

  db:
    image: mysqltest
    container_name: db
    build:
      context: .
      dockerfile: ./path/to/Dockerfile
    restart: always
    env_file:
      - .env
    ports:
      - "3306:3306"
    expose:
      - "3306"
    volumes:
      - db:/var/lib/mysql
#      - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
#      - ./docker/db/configs:/etc/mysql
    networks:
      - backend
...

volumes:
  db:
英文:

I finally got this working by commenting out CMD ["mysqld"] in the Dockerfile and changing the volumes to a brand new volume in the docker-compose.yml. That looks like:

db:/var/lib/mysql

I also commented out

#      - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
#      - ./docker/db/configs:/etc/mysql

So here's the final docker-compose.yml file:

version: "3.7"

networks:
  backend:
    driver: ${NETWORKS_DRIVER}

services:
  ...

  db:
    image: mysqltest
    container_name: db
    build:
      context: .
      dockerfile: ./path/to/Dockerfile
    restart: always
    env_file:
      - .env
    ports:
      - "3306:3306"
    expose:
      - "3306"
    volumes:
      - db:/var/lib/mysql
#      - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
#      - ./docker/db/configs:/etc/mysql
    networks:
      - backend
...

volumes:
  db:

huangapple
  • 本文由 发表于 2023年6月22日 07:15:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定