简体   繁体   中英

Docker rails can't connect mysql

I am trying to connect mysql database. Here I am using two docker container one for mysql and one for rails. I am using this docker compose file:

version: '3'
services:
 db:
   image: mysql:5.7
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: password

  volumes:
    - ./mydata:/var/lib/mysql
  ports:
    - "3307:3306"
web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - .:/myapp
  ports:
    - "3000:3000"

When I am running docker compose up and I can see my containers are running my docker ps command show me:

   CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d1bc75bbb4f7        mysql               "docker-entrypoint.s…"   22 seconds ago      Up 19 seconds       0.0.0.0:3307->3306/tcp   aaupf_db_1
2ccb0882c390        aaupf_web           "bundle exec rails s…"   12 minutes ago      Up 9 minutes        0.0.0.0:3000->3000/tcp   aaupf_web_1

I am using this code in my database.yml:

 development:
adapter: mysql2
encoding: utf8
reconnect: false
database: sample_sqlite3_development
pool: 5
username: root
password: password
host: 127.0.0.1
port: 3307

But I am getting this error in rails:

Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")

I can connect mysql using mysql workbench I don't know what do I need to do in order to connect my rails app to mysql.

Try using host: db in your database.yml. This should link your web and mysql container.

https://docs.docker.com/v17.09/compose/rails/#connect-the-database

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5

development:
  <<: *default
  database: myapp_development


test:
  <<: *default
  database: myapp_test

Firstly, you should check if you are able to login into mysql from console using command

mysql -u root -p

You don't need mysql workbench to connect your docker rails to local database.

For connecting to local db: docker-compose.yml

version: '3'

services:
   db:
    image: mysql:5.7
    restart: always
    environment: 
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: root
      MYSQL_PASSWORD: password
    web:
     build: .
     command: bundle exec rails s -p 3000 -b '0.0.0.0'
     volumes:
     - .:/myapp
     depends_on: 
      - db
     network_mode: "host"
     environment:
      DB_USER: root
      DB_PASSWORD: password
      DB_HOST: 127.0.0.1

The option network_mode: "host" enables connection to local db.

config/database.yml:

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>

 development:
  <<: *default

Try below one:

version: '3'
services:
  db:
    image: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USERNAME: root
      MYSQL_PASSWORD: root
    ports: 
      - "3306:3306"
    volumes:
      - ./tmp/db_data:/var/lib/mysql/data
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM