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:

 

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

and this is the database.yml:

test:
  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?

Thanks

Francesco

 

 

2 answers

2 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 '127.0.0.1' 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 Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

706 views 0 4
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot