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

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

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:

  1. FROM mysql/mysql-server:8.0.32
  2. # needed for initialization
  3. ENV MYSQL_ROOT_PASSWORD=test_password
  4. ENV MYSQL_DATABASE=new_database
  5. ENV MYSQL_USER=user
  6. ENV MYSQL_PASSWORD=test_password
  7. RUN microdnf update && microdnf -y install vim
  8. EXPOSE 3306
  9. CMD ["mysqld"]

docker-compose.yml

  1. version: "3.7"
  2. networks:
  3. backend:
  4. driver: ${NETWORKS_DRIVER}
  5. services:
  6. ...
  7. db:
  8. image: mysqltest
  9. container_name: db
  10. build:
  11. context: .
  12. dockerfile: ./path/to/Dockerfile
  13. restart: always
  14. env_file:
  15. - .env
  16. ports:
  17. - "3306:3306"
  18. expose:
  19. - "3306"
  20. volumes:
  21. - db-volume:/var/lib/mysql
  22. - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
  23. - ./docker/db/configs:/etc/mysql
  24. networks:
  25. - backend
  26. ...
  27. volumes:
  28. 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:

  1. FROM mysql/mysql-server:8.0.32
  2. # needed for intialization
  3. ENV MYSQL_ROOT_PASSWORD=test_password
  4. ENV MYSQL_DATABASE=new_database
  5. ENV MYSQL_USER=user
  6. ENV MYSQL_PASSWORD=test_password
  7. RUN microdnf update && microdnf -y install vim
  8. EXPOSE 3306
  9. CMD ["mysqld"]

docker-compose.yml

  1. version: "3.7"
  2. networks:
  3. backend:
  4. driver: ${NETWORKS_DRIVER}
  5. services:
  6. ...
  7. db:
  8. image: mysqltest
  9. container_name: db
  10. build:
  11. context: .
  12. dockerfile: ./path/to/Dockerfile
  13. restart: always
  14. env_file:
  15. - .env
  16. ports:
  17. - "3306:3306"
  18. expose:
  19. - "3306"
  20. volumes:
  21. - db-volume:/var/lib/mysql
  22. - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
  23. - ./docker/db/configs:/etc/mysql
  24. networks:
  25. - backend
  26. ...
  27. volumes:
  28. db-volume:

答案1

得分: 0

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

  1. db:/var/lib/mysql

我还注释掉了

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

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

  1. version: "3.7"
  2. networks:
  3. backend:
  4. driver: ${NETWORKS_DRIVER}
  5. services:
  6. ...
  7. db:
  8. image: mysqltest
  9. container_name: db
  10. build:
  11. context: .
  12. dockerfile: ./path/to/Dockerfile
  13. restart: always
  14. env_file:
  15. - .env
  16. ports:
  17. - "3306:3306"
  18. expose:
  19. - "3306"
  20. volumes:
  21. - db:/var/lib/mysql
  22. # - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
  23. # - ./docker/db/configs:/etc/mysql
  24. networks:
  25. - backend
  26. ...
  27. volumes:
  28. 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:

  1. db:/var/lib/mysql

I also commented out

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

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

  1. version: "3.7"
  2. networks:
  3. backend:
  4. driver: ${NETWORKS_DRIVER}
  5. services:
  6. ...
  7. db:
  8. image: mysqltest
  9. container_name: db
  10. build:
  11. context: .
  12. dockerfile: ./path/to/Dockerfile
  13. restart: always
  14. env_file:
  15. - .env
  16. ports:
  17. - "3306:3306"
  18. expose:
  19. - "3306"
  20. volumes:
  21. - db:/var/lib/mysql
  22. # - ./docker/db/sql-scripts:/docker-entrypoint-initdb.d/
  23. # - ./docker/db/configs:/etc/mysql
  24. networks:
  25. - backend
  26. ...
  27. volumes:
  28. 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:

确定