Rails mysql container/connection

rails
docker
mysql
2.0

#1

I’m trying to get my db connection setup, and I was following this example from @gssbzn:

The problem is:

Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Here is my base containerInfo:

version: 2
executorType: docker
containerInfo:
  - image: alienfast/ruby:latest
    env:
      - RAILS_ENV=test
  - image: library/mysql:latest
    env:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true

And my database.yml:

test:
  adapter: mysql2
  pool: 5
  timeout: 5000
  encoding: utf8
  port: 3306
  database: dummy_test
  username: root
  password:
  host: localhost

In Spin up environment I see:

Starting container alienfast/ruby:latest
  using image alienfast/ruby@sha256:d9e115e97449ba876b9561f52e33581e7b8f372f989d69fcae402285c78090ce
Starting container library/mysql:latest
  using image mysql@sha256:5e2ec5964847dd78c83410f228325a462a3bfd796b6133b2bdd590b71721fea6

So, my understanding is:

  1. My base container is alienfast/ruby:latest
    2 A mysql container is also started

Is that correct so far?

Should mysql be started and available and bound to 3306 on localhost?


#2

This is my default config for database and all my enviroments inherit from it

default: &default
  adapter: mysql2
  encoding: utf8
  username: <%= ENV['DB_USERNAME'] || 'root' %>
  password: <%= ENV['DB_PASSWORD'] || ENV['MYSQL_ROOT_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] || 'localhost' %>

#3

Many frameworks require you add it to your config, but sometimes you can get away with the MySQL env var MYSQL_HOST.

https://dev.mysql.com/doc/refman/5.7/en/environment-variables.html


#4

@rohara neither of those work. I had this working with a compose configuration and it simply used a link.

The image documentation shows various usages such as starting your client container with:

docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

but I don’t control that.

I’ll add my keys and see if I can figure it out on the host.


#5

The MySQL container has port 3306 exposed automatically. You will also need to set MYSQL_ROOT_HOST=% or you will be unable to connect. The error you showed is referencing a socket file, but that will always be inaccessible with this architecture.


#6

This failed:

containerInfo:
  - image: alienfast/ruby:latest
    env:
      - RAILS_ENV=test
  - image: library/mysql:latest
    env:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_ROOT_HOST=%

Does it need to be set in both? just the db? just the host? Your responses are a bit too brief for me to get a clear picture. A bit more specificity could go a long way to get your point across with fewer messages.

I appreciate the help.


#7

Here’s what I’ve got so far with no success:

containerInfo:
  - image: alienfast/ruby:latest
    env:
      - RAILS_ENV=test
      - MYSQL_ROOT_HOST="%"
  - image: library/mysql:latest
    env:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_ROOT_HOST="%"
defaults: &defaults
  adapter: mysql2
  pool: 5
  timeout: 5000
  encoding: utf8
  port: 3306
  username: <%= ENV['DB_USERNAME'] || 'root' %>
  password: <%= ENV['DB_PASSWORD'] || ENV['MYSQL_ROOT_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] || ENV['MYSQL_ROOT_HOST'] || 'localhost' %>

test:
  <<: *defaults
  database: dummy_test
  password:

#8

Substituting % with localhost also doesn’t work.


#9

You need to define MYSQL_ROOT_HOST=% on the MySQL image to tell it how to define the root user. Without it, you will be denied access. localhost is the default but you’re connecting from a private network IP.

On the base image, you need to define MYSQL_HOST or something similar (looks like DB_HOST based on your snippet) as 127.0.0.1 to use TCP over the Unix socket.


#10

Finally! Thanks @rohara

Here is the working config:

containerInfo:
  - image: alienfast/ruby:latest
    env:
      - RAILS_ENV=test
  - image: library/mysql:5.7.17
    env:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_ROOT_HOST=%

database.yml

defaults: &defaults
  adapter: mysql2
  pool: 5
  timeout: 5000
  encoding: utf8
  port: 3306
  username: <%= ENV['DB_USERNAME'] || 'root' %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] || '127.0.0.1' %>

test:
  <<: *defaults
  database: dummy_test

#11