docker mysql容器启动后退出

时间:2019-05-06 20:44:34

标签: mysql docker

我正在尝试使用初始化脚本启动docker MySQL容器。我的Dockerfile看起来像这样:

FROM node:11
WORKDIR /usr/src/app
COPY ./init.sql /docker-entrypoint-initdb.d/
COPY . .
RUN npm install
ENV PORT=8000
EXPOSE ${PORT}
CMD [ "npm", "start" ]

我将init.sql文件放在两个位置,分别是主目录和docker-entrypoint-initdb.d /文件夹。 init.sql文件仅具有create table和insert语句。我按以下顺序运行Docker命令:

//start network
docker network create --driver bridge mysql-net

//launch docker mysql container with init script to create tables & data
docker run -d --name mysql-server --network mysql-net -p 3306:3306 -e "MYSQL_ROOT_PASSWORD=rootpassword" -e "MYSQL_DATABASE=yelplikeapp" -e "MYSQL_USER=yelplikeapp" -e "MYSQL_PASSWORD=hunter2" -v "/c/Users/marti/Documents/projectFolder:/docker-entrypoint-initdb.d" mysql:5

启动mysql-server容器最初是可以的,但是当我运行'docker ps -a'几秒钟后,我可以看到它一启动就退出:

enter image description here

为什么我的mysql服务器容器退出?我一直试图进入容器并查看数据库,以验证是否成功创建了所有表。但是我在这样做时遇到了麻烦,并认为这是问题的根源。

为了透明起见,我将发布完整的init.sql文件: 当我检查mysql-server的日志时,有一条错误消息说 第1行的错误1050(42S01):表“用户”已存在

drop database yelplikeapp;
create database yelplikeapp;
use yelplikeapp;

SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS businesses;
DROP TABLE IF EXISTS photos;
DROP TABLE IF EXISTS reviews;
SET FOREIGN_KEY_CHECKS = 1;

CREATE TABLE users (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  username VARCHAR(255) NOT NULL,  
  PRIMARY KEY (id)
);

INSERT INTO users(id, username) VALUES 
( NULL, 'brungoTungis' ),
( NULL, 'kramer' ),
( NULL, 'elaine');

CREATE TABLE businesses (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  ownerid MEDIUMINT NOT NULL,
  name VARCHAR(255) NOT NULL,
  address VARCHAR(255) NOT NULL,
  city VARCHAR(255) NOT NULL,
  state VARCHAR(255) NOT NULL,
  zip VARCHAR(255) NOT NULL,
  phone VARCHAR(255) NOT NULL,
  category VARCHAR(255) NOT NULL,
  subcategory VARCHAR(255) NOT NULL,
  website VARCHAR(255),
  email VARCHAR(255),
  PRIMARY KEY (id),
  FOREIGN KEY (ownerid) REFERENCES users(id) ON DELETE CASCADE
);

INSERT INTO businesses(id, ownerid, name, address, city, state, zip, phone, category, subcategory, website, email) VALUES ( NULL, 1, 'taco bell', '1157th ave nw', 'corvallis', 'or', '98023', '423-232-1313', 'fastfood', 'mexican', 'www.tacobell.com', 'tacobell@gmail.gov' ), ( NULL,  1, 'kfc', '1158th ave nw', 'seattle', 'wa', '98223', '423-232-993', 'fastfood', 'chicken', NULL, NULL );

CREATE TABLE photos (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  userid MEDIUMINT NOT NULL,
  businessid MEDIUMINT NOT NULL,
  caption VARCHAR(255),
  PRIMARY KEY (id),
  FOREIGN KEY (userid) REFERENCES users(id),
  FOREIGN KEY (businessid) REFERENCES businesses(id) ON DELETE CASCADE
);

INSERT INTO photos(id, userid, businessid, caption) VALUES 
( NULL, 2, 1, 'coolcaption' ),
( NULL, 3, 2, NULL ),
( NULL, 3, 1, 'badcaption');

CREATE TABLE reviews (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  userid MEDIUMINT NOT NULL,
  businessid MEDIUMINT NOT NULL,
  dollars MEDIUMINT NOT NULL,
  stars MEDIUMINT NOT NULL,
  review VARCHAR(255),
  PRIMARY KEY (id),
  FOREIGN KEY (userid) REFERENCES users(id),
  FOREIGN KEY (businessid) REFERENCES businesses(id)
);

INSERT INTO reviews(id, userid, businessid, dollars, stars, review) VALUES 
( NULL, 2, 1, 2, 3, 'mycoolreview' ), ( NULL, 1, 2, 2, 3, NULL );

1 个答案:

答案 0 :(得分:1)

解决了这个问题,我从dockerfile中删除了copy语句,还删除了init.sql文件。在我的项目文件中有两个,在homeRepo / init.sql和homeRepo / docker-entrypoint-initdb中有一个.d /

我删除了homeRepo / init.sql文件,重新运行了docker mysql-server创建的创建,并且工作正常