mysql - Docker compose 不会为 mysql8 创建用户、密码和数据库
问题描述
我的 docker compose 不会创建 mysql 用户、数据库,即使我已将它们作为环境变量传递。这是我的 docker-compose.yml。
version: '3.3'
services:
web:
image: zadiki1/posshop-webapp
ports:
- "3000:3000"
depends_on:
- db
- phpmyadmin
environment:
HOST: db
MYSQL_DATABASE: 'posshop'
USER_NAME: 'zadik'
PASSWORD: 'zadik'
db:
image: mysql:8
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_USER: 'zadik'
MYSQL_PASSWORD: 'zadik'
MYSQL_DATABASE: 'posshop'
MYSQL_ROOT_HOST: '0.0.0.0'
expose:
- '3306'
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-dot
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: 'root'
PMA_HOST: db
depends_on:
- db
ports:
- 8000:80
当我去mysql服务器时,数据库和用户都没有创建我已经在线搜索但找不到解决方案
解决方案
您的 docker-compose.yml 文件是正确的(至少对于 db 服务)。它将创建您配置的所有内容——root 密码、用户和数据库。
见下文:
为 db 服务使用与您完全相同的 yaml:
User-MacBook-Pro:testmysql myuser$ cat docker-compose.yml
version: '3.3'
services:
db:
image: mysql:8
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_USER: 'zadik'
MYSQL_PASSWORD: 'zadik'
MYSQL_DATABASE: 'posshop'
MYSQL_ROOT_HOST: '0.0.0.0'
expose:
- '3306'
启动容器:
User-MacBook-Pro:testmysql myuser$ docker-compose up -d
Creating network "testmysql_default" with the default driver
Creating testmysql_db_1 ... done
执行到 mysql 容器中:
User-MacBook-Pro:testmysql myuser$ docker-compose exec db bash
连接根和列表数据库(显示 posshop):
root@aeba05c5cc08:/# mysql -u root -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| posshop |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> exit
Bye
连接用户(zadik/zadik):
root@aeba05c5cc08:/# mysql -u zadik -pzadik
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.17 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| posshop |
+--------------------+
2 rows in set (0.01 sec)
mysql> use posshop;
Database changed
mysql>
附加说明:如另一个答案中所述,您还应该创建一个卷来保存数据(尽管这不是问题)。
推荐阅读
- java - 在另一个字符串中进行字符串搜索的计数器
- dart - 约束可拖动区域
- javascript - 带有换行符的 React/Momentjs 日期格式
- bash - 是否可以将剪切命令的结果发送到预定义的目录?
- parsing - EXPRESS 数据建模语言的自定义解析器
- kubernetes - Kubernetes 错误代码 403:用户匿名无法获取路径
- r - 我可以使用 r 中的 data.table 对列进行即时计算吗?
- java - 如何区分 Android 中的 .ai 和 .eps 文件
- css - 修改特定的.custom-checkbox
- javascript - 为什么它不断复制我正在更新的当前行?