mysql Docker容器没有在数据库中创建表

hmae6n7t  于 2023-04-19  发布在  Mysql
关注(0)|答案(1)|浏览(199)

我正在尝试使用Docker来容器化我的数据库。
我最初使用workbench上的数据库,然后导出了它
schema.sql

DROP TABLE IF EXISTS `track`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `track` (
  `id` int NOT NULL AUTO_INCREMENT,
  `isrc` varchar(20) NOT NULL,
  `name` varchar(100) NOT NULL,
  `duration_ms` int NOT NULL,
  `explicit` tinyint(1) NOT NULL,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `artists` text,
  PRIMARY KEY (`id`),
  UNIQUE KEY `isrc` (`isrc`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `user`
--

DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `user` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `enabled` tinyint(1) NOT NULL,
  `secret` varchar(255) DEFAULT NULL,
  `verification_code` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

我正在使用docker-compose
docker-compose.yml

version: '3.8'
services:
  db:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: newdb
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "3307:3306"
    volumes:
      - ./mysqldata:/var/lib/mysql
      - ./src/main/resources/schema.sql:/docker-entrypoint-initdb.d/

我相信我的架构在正确的位置。
这是确定的路径

/RestAPI-MetaData/src/main/resources/schema.sql

当我跑的时候

docker-compose up -d

Docker构建并运行容器没有问题,并且在我运行它时连接到我的应用程序
这里是我的applications.properties文件,以防万一它是需要的

spring.datasource.url=jdbc:mysql://localhost:3307/newdb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

但是当我登录到我的容器mysql数据库在那里但表是空的

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| newdb              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.02 sec)

mysql> use newdb
Database changed
mysql> show tables
    -> ;
Empty set (0.00 sec)

mysql>
pdtvr36n

pdtvr36n1#

试试这个docker-compose配置:

version: '3.8'
services:
  db:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: newdb
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "3307:3306"
    volumes:
      - mysqldata:/var/lib/mysql
      - ./src/main/resources/schema.sql:/docker-entrypoint-initdb.d/schema.sql

volumes:
  mysqldata:

相关问题