簡體   English   中英

Mysql2::Error::ConnectionError: 無法在“mysql”上連接到 MySQL 服務器(111“連接被拒絕”)

[英]Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'mysql' (111 "Connection refused")

我正在為我的示例應用程序運行 docker-compose,該文件由 mysql 數據庫和應用程序映像組成。 我收到以下錯誤。

    $ docker logs -f sample-application
    Can't connect to MySQL server on 'mysql' (111 "Connection refused")
    Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "reconnect"=>true, "database"=>"datagen", "username"=>"root", "password"=>"password", "host"=>"mysql", "port"=>3306}
    rake aborted!
    Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'mysql' (111 "Connection refused")
    /usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `connect'
    /usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:809:in `new_connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `checkout_new_connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:832:in `try_to_checkout_new_connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:793:in `acquire_connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:521:in `checkout'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:380:in `connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1008:in `retrieve_connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_handling.rb:90:in `connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:119:in `create'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:313:in `each'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:138:in `create_current'
    /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>'
    /usr/local/bundle/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
    Tasks: TOP => db:create
    (See full trace by running task with --trace)

這是 mysql docker-compose yaml 文件的數據庫組件


version: '3.1'

services:
sample-service:
    image: service:latest
    hostname: sample-service
    container_name: sample-service
    depends_on:
      - sample-application
    ports:
      - "8080:8080"
    networks:
      - $NETWORK_NAME

mysql:
    image: mysql:5.7
    hostname: mysql
    container_name: mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=foo
      - MYSQL_USER=root
      - MYSQL_PASSWORD=password
    networks:
      - $NETWORK_NAME

sample-application:
    image: sample:latest 
    hostname: mysql
    container_name: sample-application
    depends_on:
      - mysql
    ports:
      - "3000:3000"
    environment:
      - RAILS_ENV=production
      - DATABASE_HOST=mysql
      - DATABASE_USER=root
      - DATABASE_NAME=foo
      - DATABASE_PORT=3306
    networks:
      - $NETWORK_NAME

networks:
  datagen:
    external: true

我能夠從應用程序 docker 容器映像 ping mysql。 連接成功,同時我也可以從其他機器訪問 mysql 映像。

因此,mysql 映像中沒有問題,但是在使用 docker compose 運行時無法連接到 mysql 主機。 我傳遞的環境變量也是正確的。

我查看了 stackoverflow 中的各種問題並嘗試更改綁定地址。 但是我在我的 mysql 容器映像中沒有看到它。


    bash-4.2# find / -name "*.cnf"
    /var/lib/mysql/auto.cnf
    /usr/share/mysql/my-default.cnf
    /etc/pki/tls/openssl.cnf
    /etc/my.cnf
    /healthcheck.cnf
    bash-4.2# cat /etc/my.cnf
    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

    [mysqld]
    #
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    #
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    #
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M
    skip-host-cache
    skip-name-resolve
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    secure-file-priv=/var/lib/mysql-files
    user=mysql

    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0

    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid

你真的需要外網嗎? 唯一可能的原因是您想將其他容器連接到它。 否則不要使用它 - 您將能夠僅通過端口公開機制從主機訪問容器。

如果您不打算從docker-compose外部訪問數據庫,請考慮對文件進行以下更改:

  • 刪除無用的東西
  • 刪除的端口(在您不需要公開的用戶定義網絡內)

docker-compose.yml

version: '3.1'

services:
  mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=foo
      - MYSQL_USER=root
      - MYSQL_PASSWORD=password

  sample-application:
    image: sample:latest 
    depends_on:
      - mysql
    environment:
      - RAILS_ENV=production
      - DATABASE_HOST=mysql
      - DATABASE_USER=root
      - DATABASE_NAME=foo
      - DATABASE_PORT=3306

  sample-service:
    image: service:latest
    depends_on:
      - sample-application

通過這樣的配置,您可以通過名稱訪問網絡內部的服務,例如mysqlsample-servicesample-application

順便說一句,當服務依賴於應用程序而不是相反時的奇怪情況

如果您使用 rails 連接到 Mysql,請確保database.yml的 db 主機與docker-compose中的服務名稱(在本例中為mysql )相同,這就是它知道連接到 mysql 容器的方式。 刪除所有容器和圖像並從頭開始構建,以確保沒有緩存任何配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM