Dockerfile不从我的initialize.sql创建表

时间:2019-07-17 12:57:59

标签: mysql docker dockerfile

我想这样做customize-your-mysql-database-in-docker

我到了执行docker容器的地步,并向我显示了数据库“ studienarbeit”。但是,当我使用命令show table时,它告诉我数据库中没有表。

所以我认为我的Dockerfile或initializeDB.sql出了问题吗?

Dockerfile:

FROM mysql
ENV MYSQL_DATABASE studienarbeit
COPY ./sql-scripts/ /docker-entrypoint-initdb.d/

InitializeDB.sql:

CREATE TABLE `buchausleihe` (
  `ausleihnummer` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(45) NOT NULL,
  `Typ` varchar(45) NOT NULL,
  `personid` int(11) NOT NULL,
  `seriennummer` int(11) NOT NULL,
  `Ausleihdatum` varchar(45) NOT NULL,
  `Rückgabedatum` varchar(45) NOT NULL,
  UNIQUE KEY `ausleihnummer_UNIQUE` (`ausleihnummer`),
  UNIQUE KEY `seriennummer_UNIQUE` (`seriennummer`),
  KEY `personid_idx` (`personid`),
  CONSTRAINT `personid` FOREIGN KEY (`personid`) REFERENCES `person` (`personid`) ON DELETE CASCADE,
  CONSTRAINT `seriennummer` FOREIGN KEY (`seriennummer`) REFERENCES `buchexemplar` (`seriennummer`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `buchexemplar` (
  `Seriennummer` int(11) NOT NULL,
  `Buchstatus` varchar(45) NOT NULL,
  `buchid` int(11) NOT NULL,
  PRIMARY KEY (`Seriennummer`),
  KEY `buchid_idx` (`buchid`),
  CONSTRAINT `buchid` FOREIGN KEY (`buchid`) REFERENCES `buchtyp` (`buchid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `buchtyp` (
  `Buchid` int(11) NOT NULL AUTO_INCREMENT,
  `Autor` varchar(45) NOT NULL,
  `Titel` varchar(45) NOT NULL,
  `ISBN` int(11) NOT NULL,
  PRIMARY KEY (`Buchid`),
  UNIQUE KEY `ISBN_UNIQUE` (`ISBN`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `person` (
  `PersonID` int(11) NOT NULL AUTO_INCREMENT,
  `Typ` varchar(45) NOT NULL,
  `Name` varchar(45) NOT NULL,
  `Vorname` varchar(45) NOT NULL,
  `Faku` varchar(45) DEFAULT NULL,
  `Matrikelnr` int(11) DEFAULT NULL,
  `Gruppe` varchar(45) DEFAULT NULL,
  `Straße` varchar(45) DEFAULT NULL,
  `Hausnummer` varchar(45) DEFAULT NULL,
  `Ort` varchar(45) DEFAULT NULL,
  `Plz` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`PersonID`),
  UNIQUE KEY `PersonID_UNIQUE` (`PersonID`),
  UNIQUE KEY `Matrikelnr_UNIQUE` (`Matrikelnr`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `student` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(45) NOT NULL,
  `Gruppe` varchar(45) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

1 个答案:

答案 0 :(得分:0)

问题不是docker,您的SQL文件无效。您可以在文件中创建下面的表,并在脚本的开头引用表。

检查您的Docker容器的日志。

enter image description here

use studienarbeit;
CREATE TABLE `buchtyp` (
  `Buchid` int(11) NOT NULL AUTO_INCREMENT,
  `Autor` varchar(45) NOT NULL,
  `Titel` varchar(45) NOT NULL,
  `ISBN` int(11) NOT NULL,
  PRIMARY KEY (`Buchid`),
  UNIQUE KEY `ISBN_UNIQUE` (`ISBN`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `person` (
  `PersonID` int(11) NOT NULL AUTO_INCREMENT,
  `Typ` varchar(45) NOT NULL,
  `Name` varchar(45) NOT NULL,
  `Vorname` varchar(45) NOT NULL,
  `Faku` varchar(45) DEFAULT NULL,
  `Matrikelnr` int(11) DEFAULT NULL,
  `Gruppe` varchar(45) DEFAULT NULL,
  `Straße` varchar(45) DEFAULT NULL,
  `Hausnummer` varchar(45) DEFAULT NULL,
  `Ort` varchar(45) DEFAULT NULL,
  `Plz` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`PersonID`),
  UNIQUE KEY `PersonID_UNIQUE` (`PersonID`),
  UNIQUE KEY `Matrikelnr_UNIQUE` (`Matrikelnr`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `student` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(45) NOT NULL,
  `Gruppe` varchar(45) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `buchexemplar` (
  `Seriennummer` int(11) NOT NULL,
  `Buchstatus` varchar(45) NOT NULL,
  `buchid` int(11) NOT NULL,
  PRIMARY KEY (`Seriennummer`),
  KEY `buchid_idx` (`buchid`),
  CONSTRAINT `buchid` FOREIGN KEY (`buchid`) REFERENCES `buchtyp` (`buchid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

CREATE TABLE `buchausleihe` (
  `ausleihnummer` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(45) NOT NULL,
  `Typ` varchar(45) NOT NULL,
  `personid` int(11) NOT NULL,
  `seriennummer` int(11) NOT NULL,
  `Ausleihdatum` varchar(45) NOT NULL,
  `Rückgabedatum` varchar(45) NOT NULL,
  UNIQUE KEY `ausleihnummer_UNIQUE` (`ausleihnummer`),
  UNIQUE KEY `seriennummer_UNIQUE` (`seriennummer`),
  KEY `personid_idx` (`personid`),
  CONSTRAINT `personid` FOREIGN KEY (`personid`) REFERENCES `person` (`personid`) ON DELETE CASCADE,
  CONSTRAINT `seriennummer` FOREIGN KEY (`seriennummer`) REFERENCES `buchexemplar` (`seriennummer`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

enter image description here