lellefood
lellefood

Reputation: 2086

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock

I'm trying to run a Ruby on Rails project with docker and I don't understand how to make this stuff working probably because I'm not familiar with docker.
I'm on Ubuntu 16.04, since now:

The error I get is:

#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "host"=>"localhost", "username"=>"root", "password"=>"root", "pool"=>5, "timeout"=>5000,  "database"=>"project"}, {}
Created database 'project'
rails aborted!
Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Looking here of stackoverflow many says I have to be sure the mysql server is running. How can i do this? The result of docker ps is the following (omitted the COMMAND ans CREATED columns)

CONTAINER ID        IMAGE        STATUS              PORTS                              NAMES
9f03056e2b54        project_web  Up About an hour    0.0.0.0:3035->3035/tcp             project_webpack
465cfbac7cbd        mysql        Up About an hour    3306/tcp                           project_mysql

My database.yml:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: mysql2
  host: localhost
  username: root
  password: root
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: project_local

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test: &test
  <<: *default
  database: project_test

production:
  <<: *default
  database: db/production.sqlite3

staging:
  adapter: mysql2
  host: <MY HOST>
  database: project_dev
  username: root
  password: password
  pool: 5
  timeout: 5000
cucumber:
  <<: *test

And my docker-compose.yml:

version: '3.5'
services:
  web:
    build: .
    image: project_web
    container_name: project_web
    # restart: always
    command: >
      ./wait-for-it.sh db:3306 -- bash -c "
        rails server -p 3000 -b 0.0.0.0 --pid /app/tmp/pids/server.pid
        "
    ports: ["3000:3000"]
    volumes:
      - .:/app
      - ~/tmp:/host_tmp
      - bundle_cache:/bundle
    depends_on:
      - db
    environment:
      - DB_HOST=db
      - DB_USERNAME=root
      - DB_PASSWORD=root
  db:
    container_name: project_mysql
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
    volumes:
      - ./db-data:/var/lib/mysql
    restart: always
volumes:
  bundle_cache:
  # Mount volume with default driver

Upvotes: 1

Views: 2568

Answers (3)

sealocal
sealocal

Reputation: 12387

With a similar docker-compose.yml, my solution was to remove the named volume that is defined in the db service:

$ docker volume ls # confirm the named volume is present
DRIVER      VOLUME_NAME
local       db-data

$ docker-compose down # stop/remove your containers for this project

$ docker volume rm db-data # remove the (possibly) problematic volume

$ docker-compose up # start the project in the foreground and hopefully not see the socket connection error in the logs

Upvotes: 0

Rafael Delbel
Rafael Delbel

Reputation: 337

Try to change in your database.yml the host to 0.0.0.0 This solved my problem.

In the context of servers, 0.0.0.0 means all IPv4 addresses on the local machine. If a host has two IP addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.

Upvotes: 2

Anubis
Anubis

Reputation: 481

Before running docker-compose try to run the container itself:

docker run --rm -it mysql-server:latest bash

this should give you a normal shell environment inside the container. from there you can try mysql, configure your server, start or stop it and everthing else you can do on a "normal" linux.

ps: if you need mor tools try apt-get update and then install what your need.

Upvotes: 0

Related Questions