简体   繁体   English

Gitlab Ci 管道问题:Nodejs 无法连接到 Mysql 数据库

[英]Gitlab Ci Pipeline issue: Nodejs can't connect to Mysql Database

I'm trying to set up a Gitlab Ci Pipeline where i can test my nodejs app.我正在尝试设置一个 Gitlab Ci 管道,我可以在其中测试我的 nodejs 应用程序。 The problem is, that app can't connect to the Database.问题是,该应用程序无法连接到数据库。 I tried to configure everthing like here .我试图像这里一样配置一切。 I dont really know why it can't find the Database and haven't found any other examples or sources for that.我真的不知道为什么它找不到数据库,也没有找到任何其他示例或来源。 The problem is, that the app needs the db to run, otherwise it stopps.问题是,应用程序需要数据库才能运行,否则它会停止。

For the Database connection im using typeorm:对于使用 typeorm 的数据库连接:

"type": "mysql",
   "host": "db",
   "port": 3306,
   "username": "root",
   "password": "logistics",
   "database": "logistics",
   "synchronize": true,
   "logging": true,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }

this is my gitlab.yml:这是我的 gitlab.yml:

# https://hub.docker.com/r/library/node/tags/
image: node:13

variables:
  # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: "logistics"
  MYSQL_ROOT_PASSWORD: "logistics"
  MYSQL_USER: "paramedic"
  MYSQL_PASSWORD: "logistics"
  MYSQL_PORT: "3306"
  
# Check out: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-a-service
services:
  - name: mysql:5.7
  
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
  paths:
    - backend/node_modules/

test_backend:
  script:
    - cd backend/
    - yarn install
    - yarn start

This is the error Message:这是错误消息:

$ yarn start
 yarn run v1.22.4
 warning package.json: No license field
 $ ts-node-dev --project=tsconfig.json --transpileOnly --respawn src/index.ts
 Using ts-node version 8.10.2, typescript version 3.9.2
 Error: getaddrinfo ENOTFOUND db
     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
     --------------------
     at Protocol._enqueue (/builds/stjorueh/rettungsdienstverwalter/backend/node_modules/mysql/lib/protocol/Protocol.js:144:48)
     at Protocol.handshake (/builds/stjorueh/rettungsdienstverwalter/backend/node_modules/mysql/lib/protocol/Protocol.js:51:23)
     at PoolConnection.connect (/builds/stjorueh/rettungsdienstverwalter/backend/node_modules/mysql/lib/Connection.js:116:18)
     at Pool.getConnection (/builds/stjorueh/rettungsdienstverwalter/backend/node_modules/mysql/lib/Pool.js:48:16)
     at /builds/stjorueh/rettungsdienstverwalter/backend/src/driver/mysql/MysqlDriver.ts:884:18
     at new Promise (<anonymous>)
     at MysqlDriver.createPool (/builds/stjorueh/rettungsdienstverwalter/backend/src/driver/mysql/MysqlDriver.ts:881:16)
     at MysqlDriver.<anonymous> (/builds/stjorueh/rettungsdienstverwalter/backend/src/driver/mysql/MysqlDriver.ts:343:36)
     at step (/builds/stjorueh/rettungsdienstverwalter/backend/node_modules/tslib/tslib.js:139:27)
     at Object.next (/builds/stjorueh/rettungsdienstverwalter/backend/node_modules/tslib/tslib.js:120:57) {
   errno: -3008,
   code: 'ENOTFOUND',
   syscall: 'getaddrinfo',
   hostname: 'db',
   fatal: true
 }

Try to use mysql as a DB host, not db .尝试使用mysql作为数据库主机,而不是db You can read explanation here: https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#how-services-are-linked-to-the-job您可以在此处阅读说明: https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#how-services-are-linked-to-the-job

Alternatively, you can specify alias for mysql service或者,您可以为 mysql 服务指定别名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM