首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL 2::Error::ConnectionError:无法连接到“mysql”上的mysql服务器(111个“拒绝连接”)

问MySQL 2::Error::ConnectionError:无法连接到“mysql”上的mysql服务器(111个“拒绝连接”)
EN

Stack Overflow用户
提问于 2019-01-16 11:30:20
回答 2查看 5.4K关注 0票数 1

我正在为我的示例应用程序运行docker-组合,这个文件由mysql数据库和应用程序映像组成。我得到了以下错误。

代码语言:javascript
复制
    $ 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的数据库组件-复合yaml文件

代码语言:javascript
复制
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

我能够平mysql从应用程序码头容器图像。连接是成功的,同时我也可以从其他机器访问mysql映像。

因此,mysql映像中没有任何问题,但是在使用运行时,我无法连接到mysql主机。我传递的环境变量也是正确的。

我研究了堆栈溢出中的各种问题,并试图更改绑定地址。但我在mysql容器映像中没有看到它。

代码语言:javascript
复制
    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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-16 12:03:04

你真的需要外部网络吗?唯一可能的原因是您希望将其他容器连接到它。否则,不要使用它-您将能够访问容器从主机只是通过端口暴露机制。

如果您不打算从外部docker-compose访问数据库,请考虑对文件进行以下更改:

  • 移除无用的东西
  • 删除端口(在用户定义的网络中,您不需要公开)

docker-compose.yml

代码语言:javascript
复制
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

通过这样的配置,您可以使用网络内部的服务名称访问服务,例如mysql、sample-service和sample-application。

顺便说一句,当服务依赖于应用程序,而不是相反的情况时,情况会很奇怪。

票数 1
EN

Stack Overflow用户

发布于 2019-12-25 14:44:03

如果您使用rails连接到Mysql,请确保database.yml中的db主机与docker-compose中的服务名称(在本例中为mysql)相同,这就是它如何知道如何连接到mysql容器。删除所有容器和图像,并从一开始就进行构建,以确保没有缓存任何配置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54216150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档