skip to Main Content

Codeigniter 3.0 can not connect to local mysql, so please let me know the reason.

Error message is SQLSTATE [HY000] [2002] Connection refused.
My Codeigniter is working on Docker.

Specifically on Docker in mac

  • php-fpm 7.1
  • MySQL 5.7
  • Nginx 2.4

Containers are working.

And that MySQL container can be connected from sequel pro or Mac terminal, only from Codeigniter can connect.

Successful connection code from my Mac terminal

mysql -uroot -h 127.0.0.1-P 4306 -ppassword

db_test.php

<?php
  try {

    $pdo = new PDO(
    'mysql:dbname=******;host=127.0.0.1;charset=utf8',
    'root',
    'password',
    [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]
   );

} catch (PDOException $e) {

   header('Content-Type: text/plain; charset=UTF-8', true, 500);
   exit($e->getMessage()); 

}

result — SQLSTATE [HY000] [2002] Connection refused.

/config/database.php

}elseif (strpos($_SERVER['SERVER_NAME'],'myname') !== false && ENVIRONMENT != "test"){
// MyLocal Setting
$db['default'] = array(
        'dsn'   => '',
        'hostname' => '127.0.0.1',
  'port' => '4306',
        'username' => 'root',
        'password' => 'password',
        'database' => '******',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
);

docker-compose.yml

 version: '2'
   services:
     memcached:
     image: memcached:1.4

   data:
    image: busybox
    volumes:
      - ./misc/data:/var/lib/mysql

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: mysql_database
      MYSQL_USER: mysql_user
      MYSQL_PASSWORD: mysql_pw
      socket: /var/run/mysqld/mysqld.sock
    ports:
      - "4306:3306"
    volumes_from:
      - data
    volumes:
      - ../application/initial.sql:/docker-entrypoint-initdb.d/initial.sql
    container_name: mysql
nginx:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    links:
      - web
    container_name: nginx
    environment:
     - CI_ENV=localhost

  web:
    build: ./php-fpm71
#    build: ./php-fpm56    <- if you want to use PHP5.6
    volumes:
      - /Users/alberorana/codeigniter/suke10:/var/www/html/suke10
      - ./php-fpm71/php.ini:/usr/local/etc/php/conf.d/php.ini
    links:
      - mysql
    environment:
      DATABASE_HOST: '0.0.0.0'
      DATABASE_NAME: 'suke10'
      DATABASE_USER: 'root'
      DATABASE_PASSWORD: 'password'
    container_name: web

etc/mysql/mysql.conf.d/mysqld.cnf in MySQL container

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

3

Answers


  1. Chosen as BEST ANSWER

    @ lvthillo We were able to connect by setting the host setting to the mysql container!

    'mysql:dbname=******;host=mysql;charset=utf8'
    

  2. Your database connection inside the php container is:

     mysql:3306
    

    Since you in your compioser file, “link” the phpcontainer to the databaser container, and a link is essentliation addition of a line in your hosts-file, so mysql will point to the ip in the internal docker-network for you.

    Login or Signup to reply.
  3. docker-compose.yml

    version: "3"
    
    services:
      webserver:
        build: 
          context: ./bin/webserver
        container_name: '7.3.x-webserver'
        restart: 'always'
        ports:
          - "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
          - "${HOST_MACHINE_SECURE_HOST_PORT}:443"
        links: 
          - mysql
        volumes: 
          - ${DOCUMENT_ROOT-./www}:/var/www/html
          - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
          - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
          - ${LOG_DIR-./logs/apache2}:/var/log/apache2
      mysql:
        build:
          context: "./bin/${DATABASE}"
        container_name: '5.7-mysql'
        restart: 'always'
        ports:
          - "${HOST_MACHINE_MYSQL_PORT}:3306"
        volumes: 
          - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
          - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
        environment:
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
          MYSQL_DATABASE: ${MYSQL_DATABASE}
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      phpmyadmin:
        image: phpmyadmin/phpmyadmin
        container_name: 'sc-phpmyadmin'
        links:
          - mysql
        environment:
          PMA_HOST: mysql
          PMA_PORT: 3306
          PMA_USER: ${MYSQL_USER}
          PMA_PASSWORD: ${MYSQL_PASSWORD}
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}
        ports:
          - '8080:80'
        volumes: 
          - /sessions
      redis:
        container_name: 'sc-redis'
        image: redis:latest
        ports:
          - "${HOST_MACHINE_REDIS_PORT}:6379"
    

    /project/application/config/database.php

    $active_group = 'default';
    $query_builder = TRUE;
    
    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'mysql',
        'username' => 'root',
        'password' => 'tiger',
        'database' => 'docker',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );
    

    mysql is the name of service(mysql container) && using the same as my hostname

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search