Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 964
Omanokoto Avatar asked Oct 17 '22 14:10

Omanokoto


1 Answers

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

like image 126
Sascha Frinken Avatar answered Oct 21 '22 03:10

Sascha Frinken