为了账号安全,请及时绑定邮箱和手机立即绑定

我的管道不允许我通过 golang 连接,尽管它可以在开发环境中运行

我的管道不允许我通过 golang 连接,尽管它可以在开发环境中运行

Go
波斯汪 2023-06-26 17:40:30
我有一个 .gitlab-ci.yml 文件,稍后使用 golang 图像和 MySql 图像作为服务......吉拉布-ci.yml...stages:  - test  - build  - artimage: golang:1.9.2  variables:  BIN_NAME: alltools  ARTIFACTS_DIR: artifacts  GO_PROJECT: alltools  GOPATH: /gobefore_script:  - mkdir -p ${GOPATH}/src/${GO_PROJECT}  - mkdir -p ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}  - go get -u github.com/golang/dep/cmd/dep  - go get -u github.com/fatih/color  - go get -u github.com/go-sql-driver/mysql  - cp -r ${CI_PROJECT_DIR}/* ${GOPATH}/src/${GO_PROJECT}/  - cd ${GOPATH}/src/${GO_PROJECT}  - env="root:rootroot@tcp(localhost:3306)/TESTDB"test:  stage: test  services:    - mysql:5.7  variables:    # Configure mysql environment variables (https://hub.docker.com/_/mysql/)    # MYSQL_DATABASE: mydb    MYSQL_ROOT_PASSWORD: rootroot  script:    # Run all tests             go test ./...build:  stage: build  script:    # Compile and name the binary as `hello`    - go build -o alltools    - pwd    - ls -l alltools    # Execute the binary    - ./alltools    # Move to gitlab build directory    - mv ./alltools ${CI_PROJECT_DIR}  artifacts:    paths:      - ./alltools我还在我的 go 应用程序中进行了一个测试,该测试在我的开发机器上运行良好,正如您在上面看到的,我在 gitlab-ci.yml 文件中设置了环境变量(这与我的开发环境匹配)。env="root:rootroot@tcp(localhost:3306)/TESTDB"但是当我运行管道时出现以下错误......$ env="root:rootroot@tcp(localhost:3306)/TESTDB" $ 去测试 ./... ?alltools [没有测试文件] ?alltools/BBData [无测试文件] 拨打 tcp 127.0.0.1:3306: getsockopt: 连接被拒绝我需要更改 gitlab-ci.yml 文件中的环境变量吗?
查看完整描述

2 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

localhost 不是 MySQL 服务器将侦听的主机;它是 MySQL 服务器所监听的主机。它将以 的名称提供mysql

此外,该命令env="root:rootroot@tcp(localhost:3306)/TESTDB"在 shell 中设置一个局部变量。它不会影响环境变量。

设置环境变量

  • export局部变量

  • 或者使用variables字典

  • 或专门为命令设置变量go test

variables:

  # Set your variable here for all jobs ...

  env: root:rootroot@tcp(mysql:3306)/TESTDB 


before_script:

  # ... or export it here ...

  - export env=root:rootroot@tcp(mysql:3306)/TESTDB


test:

  services:

    - mysql:5.7

  variables:

    # ... or set it here for this job only ...

    env: root:rootroot@tcp(mysql:3306)/TESTDB


  script:

    # ... or set it here for the go command only

    - env=root:rootroot@tcp(mysql:3306)/TESTDB go test ./...


查看完整回答
反对 回复 2023-06-26
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

你应该使用:

mysql

代替:

本地主机


查看完整回答
反对 回复 2023-06-26
  • 2 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信