Joe S
Joe S

Reputation: 111

Docker MySQL mysqli::real_connect():(HY000/2002): Connection refused

I'm new to docker/docker-compose. I'm trying to connect 2 containers which are the CodeIgniter application and MySQL. I can't seems to solve the 'mysqli::real_connect(): (HY000/2002): Connection refused' issue on my web app. I was able to ping from inside the container to the other and vice versa.

This is my docker-compose.yml file:

version: '3'

services:

  web:
    container_name: ciblog
    build: .
    links: 
      - db
    depends_on:
      - db
    ports:
      - '80:80'
    volumes: 
      - .:/var/www/html/

  db:
    container_name: mysql
    image: mysql:latest
    ports:
      - '3306:3306'
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
      MYSQL_USER: root
      MYSQL_DATABASE: ciblog
    volumes:
      - ./assets/sql/ciblog.sql:/docker-entrypoint-initdb.d/ciblog.sql

Dockerfile:

FROM php:7.0-apache
RUN apt-get update && \
    apt-get install -y libfreetype6-dev libjpeg62-turbo-dev && \
    docker-php-ext-install mysqli && \
    docker-php-ext-install mbstring && \
    docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/  &&  \
    docker-php-ext-install gd
RUN a2enmod rewrite
RUN service apache2 restart

Here's my database.php configuration:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'dbport' => '3306',
    'hostname' => 'db',
    'username' => 'root',
    'password' => '',
    'database' => 'ciblog',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Could anyone please help me identify the issue? Thank you in advance. Here's my GitHub repository in case you want to see the files: https://github.com/josephsim/docker-ciblog

Upvotes: 4

Views: 15852

Answers (4)

Uma Shankar Nayak
Uma Shankar Nayak

Reputation: 1

if you are using docker with version 2.0.0 and below. please use image: mysql:5.7 I fixed my issue.

Upvotes: -1

Joe S
Joe S

Reputation: 111

I've managed to solve it. Changing the MySQL version seem to work.

image: mysql:5.7

Source: https://stackoverflow.com/a/50169745/12555817

Upvotes: 2

Carlos Eduardo
Carlos Eduardo

Reputation: 1

In Docker Compose-based setups, you can use the names of services as hostnames. In your example, set

'hostname' => 'db'

Upvotes: 0

David Maze
David Maze

Reputation: 159040

In Docker Compose-based setups, you can use the names of services as hostnames. In your example, set

'hostname' => 'db'

Networking in Compose discusses this in more detail.

Docker containers don't have consistent IP addresses, and it's usually not useful to manually look them up. You don't need links: in modern Docker either and removing that line won't be harmful.

Upvotes: 5

Related Questions