简体   繁体   English

Mysql2::Error::ConnectionError: 无法在“mysql”上连接到 MySQL 服务器(111“连接被拒绝”)

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

i am running the docker-compose for my sample applications, and this file consist of mysql database and the application image.我正在为我的示例应用程序运行 docker-compose,该文件由 mysql 数据库和应用程序映像组成。 i am getting the following error.我收到以下错误。

    $ 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)

Here is database components for mysql docker-compose yaml file这是 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

I am able to ping mysql from application docker container image.我能够从应用程序 docker 容器映像 ping mysql。 The connection is successfull and at the same time i am able to access the mysql image from some other machine also.连接成功,同时我也可以从其他机器访问 mysql 映像。

So there is no issue in the mysql image but i am unable to connect to mysql host when running using docker compose.因此,mysql 映像中没有问题,但是在使用 docker compose 运行时无法连接到 mysql 主机。 The environmental variable which i passes is also correct.我传递的环境变量也是正确的。

I looked into various issues in stackoverflow and tried to change the bind address.我查看了 stackoverflow 中的各种问题并尝试更改绑定地址。 But i dont see it in my mysql container image.但是我在我的 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

You really need external network?你真的需要外网吗? The only possible reason is that you want to connect other containers to it.唯一可能的原因是您想将其他容器连接到它。 Otherwise don't use it - you will be able to access containers from host just through port exposal mechanism.否则不要使用它 - 您将能够仅通过端口公开机制从主机访问容器。

If you are not going to access database from outside docker-compose , consider following changes to file:如果您不打算从docker-compose外部访问数据库,请考虑对文件进行以下更改:

  • removed useless stuff删除无用的东西
  • removed ports (inside user-defined network you don't need exposing)删除的端口(在您不需要公开的用户定义网络内)

docker-compose.yml 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

With such configuration you can access services inside network by their names, eg mysql , sample-service and sample-application通过这样的配置,您可以通过名称访问网络内部的服务,例如mysqlsample-servicesample-application

Btw strange situation when service depends on application and not vice versa顺便说一句,当服务依赖于应用程序而不是相反时的奇怪情况

If you are using rails to connect to Mysql, make sure the db host in database.yml is same as the name of the service(in this case mysql ) in docker-compose , that is how it knows to connect to mysql container.如果您使用 rails 连接到 Mysql,请确保database.yml的 db 主机与docker-compose中的服务名称(在本例中为mysql )相同,这就是它知道连接到 mysql 容器的方式。 Remove all containers and images and build from beginning just to make sure no configuration is cached.删除所有容器和图像并从头开始构建,以确保没有缓存任何配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) - Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 错误2003(HY000):无法连接到[IP]上的MySQL服务器(111“连接被拒绝”) - ERROR 2003 (HY000): Can't connect to MySQL server on [IP] (111 “Connection refused”) Rails:Heroku CI:Mysql2 :: Error:无法连接到“ 127.0.0.1”(111)上的MySQL服务器以进行测试环境 - Rails : Heroku CI : Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' (111) for test env 无法连接到&#39;mysql.hostinger.in&#39;上的MySQL服务器(111“连接被拒绝”) - Can't connect to MySQL server on 'mysql.hostinger.in' (111 “Connection refused”) Docker MYSQL [2003] 无法连接到 MySQL 服务器(111 连接被拒绝) - Docker MYSQL [2003] Can't connect to MySQL server (111 Connection refused) mysql.connector.errors.InterfaceError:2003:无法连接到“127.0.0.1:3306”上的 MySQL 服务器(111 连接被拒绝) - mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused) 无法在位桶管道上连接到MySQL(111“连接被拒绝”) - Can't connect to MySQL on bitbucket pipelines (111 “Connection refused”) MySQL 错误 111 无法连接到服务器 - MySQL Error 111 Can't connect to server Mariadb docker容器无法使用Python连接到主机上的MySQL服务器(111连接被拒绝) - Mariadb docker container Can't connect to MySQL server on host (111 Connection refused) with Python 2003:无法连接到“192.168.0.14:3306”上的 MySQL 服务器(111 连接被拒绝) - 2003: Can't connect to MySQL server on '192.168.0.14:3306' (111 Connection refused)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM