简体   繁体   中英

Unable to connect to MySql using gitlab-ci.yml

I'm trying to create gitlab-ci file for my Rails project. A part of my .gitlab-ci.yml:

image: ruby:2.3.4

variables:
  RAILS_ENV: test

services:
  - mysql:5.7


before_script:
  - apt-get update -qq && apt-get install -yqq nodejs libmysqlclient-dev
  - ruby -v
  - which ruby
  - gem install bundler --no-ri --no-rdoc
  - bundle install --jobs $(nproc) "${FLAGS[@]}"
  - cp config/database.gitlab_ci.yml config/database.yml
  - bundle exec rake db:create db:schema:load
  - bundle exec rake db:migrate --quiet

I haven't been able to get it to connect to my MySql db. No matter what I tried.

Here's a part of my database.gitlab_ci.ym:

test: &test
    adapter: mysql2
    database: my_db
    encoding: utf8
    username: my_user
    password: 1234
    host: localhost

I've always gotten this error:

  #<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
  Couldn't create database for {"adapter"=>"mysql2", "database"=>"my_db", "encoding"=>"utf8", "username"=>"my_db", "password"=>1234, "host"=>"localhost"}, {:charset=>"utf8"}
  (If you set the charset manually, make sure you have a matching collation)
  rake aborted!
  Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

How to fix it?

Your issue seems similiar to this . The workaround mentioned there is that you need to connect via TCP, not socket.

Also I'd suggest you that you check gitlab's documentation related to MySQL service.

Your mysql service won't be available at localhost .
You have to use the service name mysql as hostname.
See Documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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