can't connect to mysql server - ruby container

i'm trying to use the mysql service following instructions, but my rails tests fails because they can't connect:


#<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"=>"pipelines", "username"=>"test_user", "password"=>"test_user_password", "host"=>"localhost", "port"=>3306}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}


this is the relevant pipeline configuration:


         - mysql
      image: mysql
        MYSQL_DATABASE: pipelines
        MYSQL_ROOT_PASSWORD: my-secret-pw
        MYSQL_USER: test_user
        MYSQL_PASSWORD: test_user_password

and this is the database.yml:

  adapter: mysql2
  database: pipelines
  username: test_user
  password: test_user_password
  host: localhost
  port: 3306

from the pipeline log the mysql instance seems to run OK, so i'm probably doing something wrong, any idea?





2 answers

3 votes
Matt Ryall Atlassian Team Apr 26, 2017
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

This error message indicates that the Ruby MySQL client is trying to connect via a local, file-based "Unix socket", which won't work in Bitbucket Pipelines. The MySQL server is running in a separate container, so the client needs to use a TCP/IP connection for it to work.

I hit the same problem when using the command-line 'mysql' client in Pipelines, which connects via Unix sockets instead of via TCP if the hostname is 'localhost'. I'm guessing that the Ruby guys copied this logic into their client.

It should work if you change 'localhost' to '' in your database.yml.

I'm having the same issue although I think you should use the name of the service 'mysql' as the host of the mysql database.


But i'd set the DB_HOST environment variable on the project's pipelines settings and it's not working and my project gets this value to set the host to connect to the database.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 06, 2018 in Bitbucket

Upgrade Best Practices

Hello! My name is Mark Askew and I am a Premier Support Engineer for products Bitbucket Server/Data Center, Fisheye & Crucible. Today, I want to bring the discussion that Jennifer, Matt, and ...

655 views 5 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you