1

I have yii2 advanced application on docker, mysql db does not work.

My docker-compose:

db:
  image: 'mysql:latest'
  volumes:
      - ./database:/var/lib/mysql
  ports:
      - '3306:3306'
  restart: always
  environment:
     MYSQL_ROOT_PASSWORD: pass
     MYSQL_DATABASE: database

db in yii2:

return => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=127.0.0.1;port=3306;dbname=database',
        'username' => 'root',
        'password' => 'pass',
        'charset' => 'utf8',
    ],

When i go on my local folder with yii2 and go php yii migrate i see Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client'

How fix my pronblem?

All code https://github.com/k0v4back/docker-yii2

k0v4
  • 15
  • 1
  • 8
  • Is the application also running in a Docker container? – David Maze Dec 19 '18 at 00:21
  • If your mysql and php are two different containers, then you should write the host in dsn as the mysql ip address or host name. – Luna Dec 19 '18 at 03:18
  • In my docker-compose I have php-fpm and php-cli, Should I connect with them? It is my code https://github.com/k0v4back/docker-yii2 – k0v4 Dec 19 '18 at 05:24

1 Answers1

3

I created this answer based on your docker-compose.yml file

Create new bridge network in your docker-compose.yml file:

networks:
  my-new-network:
    driver: bridge

It's important to connect all your containers to one network(bridge) to allow communication between them.

Add each your container to the new network:

db:
  image: 'mysql:latest'
  volumes:
      - ./database:/var/lib/mysql
  ports:
      - '3306:3306'
  restart: always
  environment:
     MYSQL_ROOT_PASSWORD: pass
     MYSQL_DATABASE: database
  networks:
     - my-new-network

After these changes check if the containers can communicate(send a ping from one to the other - use container names):

From nginx container:

ping db

if you don't have ping command, install iputils:

apt-get update
apt-get install iputils-ping

If everything works well - change your Yii DB connection configuration:

return => [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=db;port=3306;dbname=database', // change IP here to database container name
    'username' => 'root',
    'password' => 'pass',
    'charset' => 'utf8',
],

I think this should help to solve your problem. Take care of communication between containers :)


Ensure also that the database can accept external connections. Default mysql configuration block external connections, so you should check your mysql configuration and add new user on which you can log in "from outside"(from external hosts/networks):

CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';

mysql root user has disabled remote connections by default. More you can read here

Logs will tell you much more :) https://docs.docker.com/engine/reference/commandline/logs/


I forked your repository: https://github.com/raciniewski/docker-yii2

  1. Build and run all containers docker-compose up --build

enter image description here

enter image description here

  1. Send ping to db container from other container in your new my-new-network network:

enter image description here enter image description here

  1. Done - containers can communicate
Krzysztof Raciniewski
  • 4,735
  • 3
  • 21
  • 42