您好我试图在Docker中创建MySQL数据库,这是我的代码
在start.sh中
#!/bin/sh
echo "Starting DB..."
docker run --name db -d \
-e MYSQL_ROOT_PASSWORD=123 \
-e MYSQL_DATABASE=users -e MYSQL_USER=root -e MYSQL_PASSWORD=123 \
-p 3306:3306 \
mysql:latest
echo "Waiting for DB to start up..."
docker exec db mysqladmin --silent --wait=30 -uroot ping || exit 1
echo "Setting up initial data..."
docker exec -i db mysql -uroot users < setup.sql
和setup.sql
create table users (
user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL,
department VARCHAR(100),
name VARCHAR(100),
access_token VARCHAR(30)
);
insert into users (username, department, name, access_token) values ('bzh01', 'Research and Development One', 'Mario1', NULL);
在终端中,结果如下:
$ bash start.sh
Starting DB...
c7a066b70fa4ad3e02acc3165cfe34ce6a48964cb1190df399404b728cb6059f
Waiting for DB to start up...
mysqld is alive
Setting up initial data...
ERROR 1049 (42000): Unknown database 'users'
答案 0 :(得分:0)
好的,我终于解决了这个问题,但方式略有不同。
在我的数据库Dockerfile中:
FROM mysql
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE users
ENV MYSQL_USER root_user
ENV MYSQL_PASSWORD abcd123
ADD setup.sql /docker-entrypoint-initdb.d
然后运行docker-compose.yml来构建
version: '2'
services:
app:
build: ./app
volumes:
- "./app:/src/app"
ports:
- "3030:3000"
depends_on:
- db
environment:
- DATABASE_HOST=db
db:
build: ./test-database