最近学习 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)
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:
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:
数据库无法连接,导致服务跑不起来,来社区请教,谢谢🙏