<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>wobuaixuexi (wobuaixuexi)</title>
    <link>https://ruby-china.org/wobuaixuexi</link>
    <description/>
    <language>en-us</language>
    <item>
      <title>Docker 部署 Rails 项目，一直报错</title>
      <description>&lt;p&gt;&lt;strong&gt;最近学习 rails，写了一个项目，采用 docker 部署，数据库一直报错&lt;/strong&gt;*&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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 &amp;lt;main&amp;gt;'
/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 &amp;lt;main&amp;gt;'
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `&amp;lt;top (required)&amp;gt;'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `&amp;lt;main&amp;gt;'
Tasks: TOP =&amp;gt; db:migrate
(See full trace by running task with --trace)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;然后我在 database.yml 中配置 socket 为&lt;code&gt;var/lib/mysql.sock&lt;/code&gt;会报下面的错&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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 &amp;lt;main&amp;gt;'
/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 &amp;lt;main&amp;gt;'
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `&amp;lt;top (required)&amp;gt;'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `&amp;lt;main&amp;gt;'
Tasks: TOP =&amp;gt; db:migrate
(See full trace by running task with --trace)
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="我的docker-compose.yml"&gt;我的 docker-compose.yml&lt;/h3&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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:
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="我的database.yml"&gt;我的 database.yml&lt;/h3&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default: &amp;amp;default
  adapter: mysql2
  encoding: utf8mb4
  pool: &amp;lt;%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %&amp;gt;
  username: root
  password: 123456
  host: 127.0.0.1
  socket: /tmp/mysql.sock

development:
  &amp;lt;&amp;lt;: *default
  database: mo_fish_backend_development

test:
  &amp;lt;&amp;lt;: *default
  database: mo_fish_backend_test

production:
  adapter: mysql2
  encoding: utf8mb4
  pool: 5
  host: &amp;lt;%= ENV['MYSQL_HOST'] %&amp;gt;
  database: &amp;lt;%= ENV['MYSQL_DATABASE'] %&amp;gt;
  username: &amp;lt;%= ENV['MYSQL_USER'] %&amp;gt;
  password: &amp;lt;%= ENV['MYSQL_ROOT_PASSWORD'] %&amp;gt;
  # sockets: 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;数据库无法连接，导致服务跑不起来，来社区请教，谢谢🙏&lt;/p&gt;</description>
      <author>wobuaixuexi</author>
      <pubDate>Tue, 28 Dec 2021 11:28:41 +0800</pubDate>
      <link>https://ruby-china.org/topics/42035</link>
      <guid>https://ruby-china.org/topics/42035</guid>
    </item>
  </channel>
</rss>
