0

I am having 2 docker containers for php app and mysql. Both are working perfectly individually. I can access my php app at localhost:8000 and can connect mysql at localhost:3306 using MySQL Workbench.

But, my php app which is inside the container is not able to connect to the mysql db which is inside another container.

My docker-compose.yml file is as follows:

    version: '3'
    services:
      website:
        container_name: php-app
        image: php-app
        build:
          context: ./
        volumes:
          - php-app:/var/www/html/
        ports:
          - 8000:80
        depends_on:
          - mysql
      mysql:
        image: mysql:8.0
        container_name: mysql-server-80
        command: --default-authentication-plugin=mysql_native_password
        volumes:
          - .:/application
        restart: always
        environment:
          - MYSQL_ROOT_PASSWORD=123456
          - MYSQL_DATABASE=testdb
          - MYSQL_USER=root
          - MYSQL_PASSWORD=123456
        ports:
          - "3306:3306"
    volumes:
      jayde-ci:
      mysql:

My database configuration file inside the codeigniter based php app is as follows:

    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'mysql',
        'username' => 'root',
        'password' => '123456',
        'database' => 'test',
        '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
    );
3
  • Have you tried in your config using localhost as well - 'hostname' => 'localhost',? Commented Dec 27, 2019 at 16:43
  • @NigelRen I tried localhost as well Commented Dec 27, 2019 at 18:50
  • Your volumes: ['php-app:/var/www/html/'] declaration will prevent the container from seeing updates to the image's source code, assuming php-app is an ordinary named volume. If at one point you had incorrect information in this file, it's stuck there until you explicitly delete that volume. Commented Dec 27, 2019 at 19:07

1 Answer 1

0

You should make password filed blank on localhost. try this

$db['default'] = array(
        'dsn'   => '',
        'hostname' => 'mysql',
        'username' => 'root',
        'password' => '',
        'database' => 'test',
        '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
    );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.