【问题标题】:MySQL "create table" does not work in docker imageMySQL“创建表”在 docker 映像中不起作用
【发布时间】:2019-07-31 07:26:50
【问题描述】:

我用mysql制作docker镜像。

我在启动 docker 文件时创建了init.sql 文件来创建表。

但是在构建 docker 镜像后,没有创建表。

我想通过DockerFiledocker-compose 创建表。

这里是github中的源代码。 https://github.com/jpskgc/article

我创建了int.sql 文件和docker-compose.yml 来使用这个init.sql文件。

article
 ├ db
 │  └ init.sql
 │  
 └ docker-compose.yml
//docker-compose.yml
version: '3'
services:
  db:
    image: mysql
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: article
      MYSQL_USER: docker
      MYSQL_PASSWORD: docker
    volumes:
      - ./db:/docker-entrypoint-initdb.d
//init.sql
CREATE DATABASE IF NOT EXISTS article;
use article;
CREATE TABLE IF NOT EXISTS `articles` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, uuid varchar(36), `title` VARCHAR(100) NOT NULL,`content` TEXT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table IF NOT EXISTS images (id int AUTO_INCREMENT NOT NULL PRIMARY KEY, article_uuid varchar(36), image_name varchar(50));

我想在docker-compose up --build的过程中创建表。 命令docker-compose up --build后,评估到'http://localhost:3000/',后端有错误,提示表不存在。

api_1     | 2019/07/31 06:29:58 [Recovery] 2019/07/31 - 06:29:58 panic recovered:
api_1     | GET /api/articles HTTP/1.1
api_1     | Host: localhost:2345
api_1     | Accept: application/json, text/plain, */*
api_1     | Accept-Encoding: gzip, deflate, br
api_1     | Accept-Language: en-US,en;q=0.9
api_1     | Connection: keep-alive
api_1     | Origin: http://localhost:3000
api_1     | Referer: http://localhost:3000/
api_1     | User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
api_1     | 
api_1     | 
api_1     | Error 1146: Table 'article.articles' doesn't exist
api_1     | /app/main.go:90 (0xb1c6d1)
api_1     |     main.func2: panic(err.Error())
api_1     | /go/src/github.com/gin-gonic/gin/context.go:147 (0x8f97c9)
api_1     |     (*Context).Next: c.handlers[c.index](c)
api_1     | /go/src/github.com/gin-gonic/gin/recovery.go:83 (0x90d259)
api_1     |     RecoveryWithWriter.func1: c.Next()
api_1     | /go/src/github.com/gin-gonic/gin/context.go:147 (0x8f97c9)
api_1     |     (*Context).Next: c.handlers[c.index](c)
api_1     | /go/src/github.com/gin-gonic/gin/logger.go:240 (0x90c300)
api_1     |     LoggerWithConfig.func1: c.Next()
api_1     | /go/src/github.com/gin-gonic/gin/context.go:147 (0x8f97c9)
api_1     |     (*Context).Next: c.handlers[c.index](c)
api_1     | /go/src/github.com/gin-gonic/gin/gin.go:391 (0x9036c9)
api_1     |     (*Engine).handleHTTPRequest: c.Next()
api_1     | /go/src/github.com/gin-gonic/gin/gin.go:352 (0x902dbd)
api_1     |     (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
api_1     | /usr/local/go/src/net/http/server.go:2774 (0x6dcc77)
api_1     |     serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
api_1     | /usr/local/go/src/net/http/server.go:1878 (0x6d8860)
api_1     |     (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
api_1     | /usr/local/go/src/runtime/asm_amd64.s:1337 (0x45a090)
api_1     |     goexit: BYTE    $0x90   // NOP
api_1     | 
api_1     | [GIN] 2019/07/31 - 06:29:58 | 500 |    123.5997ms |      172.18.0.1 | GET      /api/articles

【问题讨论】:

  • 你在哪里实际执行 int.sql?
  • 能否添加日志docker logs db
  • 也许在 docker-compose.yml 的卷指令中: - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
  • >Nico 我按照您的建议更改了volumes 指令并运行命令docker-compose up --build。但同样的错误发生。
  • 尝试通过命令行连接你的mysql实例:mysql -u root -p --port=3306 然后执行show databases; => 发生了什么?

标签: mysql docker docker-compose


【解决方案1】:

作为替代解决方案,我更改了服务器端代码以创建表。

    _, err = db.Exec("CREATE DATABASE IF NOT EXISTS article;")
    if err != nil {
        panic(err)
    }

    _, err = db.Exec("use article;")
    if err != nil {
        panic(err)
    }

    _, err = db.Exec("CREATE TABLE IF NOT EXISTS `articles` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,uuid varchar(36), `title` VARCHAR(100) NOT NULL,`content` TEXT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;")
    if err != nil {
        panic(err)
    }

    _, err = db.Exec("create table IF NOT EXISTS images (id int AUTO_INCREMENT NOT NULL PRIMARY KEY, article_uuid varchar(36), image_name varchar(50)); ")
    if err != nil {
        panic(err)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-27
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 2020-06-03
    • 1970-01-01
    相关资源
    最近更新 更多