部署 Docker 部署 Rails 项目,一直报错

wobuaixuexi · 2021年12月28日 · 最后由 franklinyu 回复于 2022年01月06日 · 788 次阅读

最近学习 rails,写了一个项目,采用 docker 部署,数据库一直报错*

rake aborted!
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1125:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_handling.rb:189:in `connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/tasks/database_tasks.rb:238:in `migrate'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:86:in `block (3 levels) in <main>'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:84:in `each'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:84:in `block (2 levels) in <main>'
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

然后我在 database.yml 中配置 socket 为var/lib/mysql.sock会报下面的错

rake aborted!
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql.sock' (2)
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1125:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_handling.rb:189:in `connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/tasks/database_tasks.rb:238:in `migrate'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:86:in `block (3 levels) in <main>'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:84:in `each'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:84:in `block (2 levels) in <main>'
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我的 docker-compose.yml

version: '3'
services:
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_DATABASE: mo_fish_backend_production
      MYSQL_HOST: db
      MYSQL_USER: root
      MYSQL_ROOT_PASSWORD: 123456
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
    ports:
      - 3306:3306
    volumes:
      - mysql_volume:/var/lib/mysql

  web:
    build: .
    restart: unless-stopped
    ports:
      - "7000:7000"
    links:
      - db
    depends_on:
      - db
    networks:
     - kong-net

  redis:
    image: redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    networks:
     - kong-net

networks:
  kong-net:
    external: true

volumes:
  mysql_volume:

我的 database.yml

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: 123456
  host: 127.0.0.1
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: mo_fish_backend_development

test:
  <<: *default
  database: mo_fish_backend_test

production:
  adapter: mysql2
  encoding: utf8mb4
  pool: 5
  host: <%= ENV['MYSQL_HOST'] %>
  database: <%= ENV['MYSQL_DATABASE'] %>
  username: <%= ENV['MYSQL_USER'] %>
  password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
  # sockets: 

数据库无法连接,导致服务跑不起来,来社区请教,谢谢🙏

看你 docker-compose.yml 里面的配置 MYSQL_HOST 值是 db。

rails 和 db 在不同的容器,连接配置要用 service name 作为主机名。

Rei 回复

谢谢,打扰了,请问下需要在哪个配置文件中添加呢?这个 service name 该怎么写呀?😂

database.yml 里面

default: &default
  # ...
  host: db

去掉 socket: /tmp/mysql.sock

Rei 回复

现在报错:

rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1125:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/connection_handling.rb:189:in `connection'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/tasks/database_tasks.rb:238:in `migrate'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:86:in `block (3 levels) in <main>'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:84:in `each'
/usr/local/bundle/gems/activerecord-6.0.4.4/lib/active_record/railties/databases.rake:84:in `block (2 levels) in <main>'
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

database.yml

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: 123456
  host: db

development:
  <<: *default
  database: mo_fish_backend_development

test:
  <<: *default
  database: mo_fish_backend_test

production:
  adapter: mysql2
  encoding: utf8mb4
  pool: 5
  host: <%= ENV['MYSQL_HOST'] || 'db' %>
  database: <%= ENV['MYSQL_DATABASE'] || 'mo_fish_backend_production' %>
  username: <%= ENV['MYSQL_USER'] || 'root' %>
  password: <%= ENV['MYSQL_ROOT_PASSWORD'] || '123456' %>

docker-compose.yml 里面修改 db service,加上:

networks:
 - kong-net

不然 web 无法访问 db servcie。

其实如果三个 servie 都不加 networks 配置就是可以互相访问的,加了就隔离了。

官方文档 https://docs.docker.com/compose/networking/

Rei 回复

可以了,谢谢你😆

DATABASE_URL 更香

这样还能部署到其他 pass 平台。

#7 说得对。更进一步,新手还没搞清楚 Docker Compose 怎么用的话,建议不要加 network;默认会创建一个新的 network 把这三个服务包起来。

wobuaixuexi 关闭了讨论。 01月07日 10:38
需要 登录 后方可回复, 如果你还没有账号请 注册新账号