title: Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'mysql' (111 "Connection refused")
body: <p>i am running the docker-compose for my sample applications, and this file consist of mysql database and the application image. i am getting the following error.</p>
  <pre class="lang-sh prettyprint-override"><code>    $ docker logs -f sample-application
      Can't connect to MySQL server on 'mysql' (111 &quot;Connection refused&quot;)
      Couldn't create database for {&quot;adapter&quot;=&gt;&quot;mysql2&quot;, &quot;encoding&quot;=&gt;&quot;utf8&quot;, &quot;pool&quot;=&gt;5, &quot;reconnect&quot;=&gt;true, &quot;database&quot;=&gt;&quot;datagen&quot;, &quot;username&quot;=&gt;&quot;root&quot;, &quot;password&quot;=&gt;&quot;password&quot;, &quot;host&quot;=&gt;&quot;mysql&quot;, &quot;port&quot;=&gt;3306}
      rake aborted!
      Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'mysql' (111 &quot;Connection refused&quot;)
      /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 &lt;top (required)&gt;'
      /usr/local/bundle/gems/rake-12.3.1/exe/rake:27:in `&lt;top (required)&gt;'
      Tasks: TOP =&gt; db:create
      (See full trace by running task with --trace)
  
  </code></pre>
  <p>Here is database components for mysql docker-compose yaml file</p>
  <pre class="lang-yaml prettyprint-override"><code>
  version: '3.1'
  
  services:
  sample-service:
      image: service:latest
      hostname: sample-service
      container_name: sample-service
      depends_on:
        - sample-application
      ports:
        - &quot;8080:8080&quot;
      networks:
        - $NETWORK_NAME
  
  mysql:
      image: mysql:5.7
      hostname: mysql
      container_name: mysql
      ports:
        - &quot;3306:3306&quot;
      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:
        - &quot;3000:3000&quot;
      environment:
        - RAILS_ENV=production
        - DATABASE_HOST=mysql
        - DATABASE_USER=root
        - DATABASE_NAME=foo
        - DATABASE_PORT=3306
      networks:
        - $NETWORK_NAME
  
  networks:
    datagen:
      external: true
  
  </code></pre>
  <p>I am able to ping mysql from application docker container image. The connection is successfull and at the same time i am able to access the mysql image from some other machine also.</p>
  <p>So there is no issue in the mysql image but i am unable to connect to mysql host when running using docker compose. The environmental variable which i passes is also correct.</p>
  <p>I looked into various issues in stackoverflow and tried to change the bind address. But i dont see it in my mysql container image.</p>
  <pre class="lang-sh prettyprint-override"><code>
      bash-4.2# find / -name &quot;*.cnf&quot;
      /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
  
  </code></pre>