即使我将它们作为环境变量传递,我的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服务器时,既未创建数据库又未创建用户 我已经在网上搜索,但找不到解决方法
答案 0 :(得分:0)
环境变量用于配置对数据库中现有资源的访问。如果您需要在posshop
(zadik
)实例中有一个数据库db
和一个用户mysql:8
,则需要先在数据库服务器上创建它们,您可以访问它们,例如来自web
。
使用Docker Compose这样做有点不方便,但以我的经验,成功的解决方案是使用例如一个docker卷以保留数据库服务器配置,在服务器配置为使用数据库进行持久化时创建数据库和用户。然后,在依赖于它的配置中(随您使用)随后引用它。
将另一个容器添加到您的Docker Compose中来创建这些资产和块是可能的,但更具挑战性,例如web
从开始到完成。
答案 1 :(得分:0)
您的docker-compose.yml文件正确(至少对于db服务而言)。它将创建您配置的所有内容-root密码,用户和数据库。
参见下文:
为数据库服务使用与您完全相同的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>
其他说明:如另一个答案所述,您还应该创建一个卷来保留数据(尽管这不是问题)。