@kazchin

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Docker上でRailsがMy SQLに接続しない 助けてください

Docker上でRailsがMy SQLに接続しないんです。

初めて投稿します。
Rails初心者です。Docker上でRailsがMySQLに接続しません。
ググりまくりましたが解決法が見つからず質問する次第です。

おそらく、database.ymlのsocket部分が原因だと思うのですが、
どうやってもダメです。

発生している問題・エラー

ActiveRecord::ConnectionNotEstablished
Can't connect to MySQL server on 'db' (115)
と怒られました。

database。ymlの設定は以下です。

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

自分で試したこと

socketを色々いじりましたが解決しません。
助けてください。

0 likes

1Answer

vi docker-compose.yml
にて command: sh に変更し
docker compose up -d
docker compose exec サービス名 sh

でコンテナの中に入って
mysqlで接続できることは確認しましたか?

また、command: sh の修正前のコマンドを
コンテナの中で実行してはどうでしょう?

0Like

Comments

  1. @kazchin

    Questioner

    ダメでした・・・
    詳しいエラー情報添付します
  2. @kazchin

    Questioner

    #HELP_ME!!

    ActiveRecord::ConnectionNotEstablished
    Can't connect to MySQL server on 'db' (115)
    Extracted source (around line #53):
    51
    52
    53
    54
    55
    56

    raise ActiveRecord::DatabaseConnectionError.hostname_error(config[:host])
    else
    raise ActiveRecord::ConnectionNotEstablished, error.message
    end
    end
    end

    Rails.root: /app

    Application Trace | Framework Trace | Full Trace
    activerecord (7.0.4.3) lib/active_record/connection_adapters/mysql2_adapter.rb:53:in `rescue in new_client'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/mysql2_adapter.rb:43:in `new_client'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/mysql2_adapter.rb:23:in `mysql2_connection'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `public_send'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `new_connection'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:700:in `checkout_new_connection'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:679:in `try_to_checkout_new_connection'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:640:in `acquire_connection'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:341:in `checkout'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `connection'
    activerecord (7.0.4.3) lib/active_record/connection_adapters/abstract/connection_handler.rb:211:in `retrieve_connection'
    activerecord (7.0.4.3) lib/active_record/connection_handling.rb:313:in `retrieve_connection'
    activerecord (7.0.4.3) lib/active_record/connection_handling.rb:280:in `connection'
    activerecord (7.0.4.3) lib/active_record/migration.rb:613:in `connection'
    activerecord (7.0.4.3) lib/active_record/migration.rb:608:in `build_watcher'
    activerecord (7.0.4.3) lib/active_record/migration.rb:590:in `block in call'
    activerecord (7.0.4.3) lib/active_record/migration.rb:589:in `synchronize'
    activerecord (7.0.4.3) lib/active_record/migration.rb:589:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
    activesupport (7.0.4.3) lib/active_support/callbacks.rb:99:in `run_callbacks'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
    web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app'
    web-console (4.2.0) lib/web_console/middleware.rb:19:in `block in call'
    web-console (4.2.0) lib/web_console/middleware.rb:17:in `catch'
    web-console (4.2.0) lib/web_console/middleware.rb:17:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'
    railties (7.0.4.3) lib/rails/rack/logger.rb:40:in `call_app'
    railties (7.0.4.3) lib/rails/rack/logger.rb:25:in `block in call'
    activesupport (7.0.4.3) lib/active_support/tagged_logging.rb:99:in `block in tagged'
    activesupport (7.0.4.3) lib/active_support/tagged_logging.rb:37:in `tagged'
    activesupport (7.0.4.3) lib/active_support/tagged_logging.rb:99:in `tagged'
    railties (7.0.4.3) lib/rails/rack/logger.rb:25:in `call'
    sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/request_id.rb:26:in `call'
    rack (2.2.6.4) lib/rack/method_override.rb:24:in `call'
    rack (2.2.6.4) lib/rack/runtime.rb:22:in `call'
    activesupport (7.0.4.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/server_timing.rb:61:in `block in call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/server_timing.rb:26:in `collect_events'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/server_timing.rb:60:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/static.rb:23:in `call'
    rack (2.2.6.4) lib/rack/sendfile.rb:110:in `call'
    actionpack (7.0.4.3) lib/action_dispatch/middleware/host_authorization.rb:137:in `call'
    railties (7.0.4.3) lib/rails/engine.rb:530:in `call'
    puma (5.6.5) lib/puma/configuration.rb:252:in `call'
    puma (5.6.5) lib/puma/request.rb:77:in `block in handle_request'
    puma (5.6.5) lib/puma/thread_pool.rb:340:in `with_force_shutdown'
    puma (5.6.5) lib/puma/request.rb:76:in `handle_request'
    puma (5.6.5) lib/puma/server.rb:443:in `process_client'
    puma (5.6.5) lib/puma/thread_pool.rb:147:in `block in spawn_thread'
    Exception Causes
    Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'db' (115)
    Request
    Parameters:

    None
    Toggle session dump
    Toggle env dump
    Response
    Headers:

    None
  3. dockerコンテナ内からmysqlコマンドでログインできるのであれば、
    database.yml の記述が問題と思われます。

    さて

    production:
      adapter: mysql2
      encoding: utf8mb4
      ####database: mysql_db
      pool: 5
      username: root
      password: xxxx
      host: db?
    

    mysqlコマンドラインを教えてください。
    ping db で応答するのでしょうか?

  4. @kazchin

    Questioner

    database.yml です。

    MySQL. Versions 5.5.8 and up are supported.

    Install the MySQL driver

    gem install mysql2

    Ensure the MySQL gem is defined in your Gemfile

    gem "mysql2"

    And be sure to use new-style password hashing:

    https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html

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

    development:
    <<: *default
    database: app_development

    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:
    <<: *default
    database: app_test

    As with config/credentials.yml, you never want to store sensitive information,

    like your database password, in your source code. If your source code is

    ever seen by anyone, they now have access to your database.

    Instead, provide the password or a full connection URL as an environment

    variable when you boot the app. For example:

    DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"

    If the connection URL is provided in the special DATABASE_URL environment

    variable, Rails will automatically merge its configuration values on top of

    the values provided in this file. Alternatively, you can specify a connection

    URL environment variable explicitly:

    production:

    url: <%= ENV["MY_APP_DATABASE_URL"] %>

    Read https://guides.rubyonrails.org/configuring.html#configuring-a-database

    for a full overview on how database connection configuration can be specified.

    production:
    <<: *default
    database: app_production
    username: app
    password: <%= ENV["APP_DATABASE_PASSWORD"] %>

  5. dockerコンテナ内外からmysqlコマンドでログインできるか?確認しましょう!

    mysql -h db -u app -ppassword app_production
    でログインできましたか?

    mysql -u myuser -pmypass -h localhost somedatabase
    でログインできましたか?

    ping -c4 db で応答しましたか?

  6. docker-compose.ymlはどうなっていますか

Your answer might help someone💌