16

I have this yml file for configuration of MySQL in docker:

# Use root/example as user/password credentials
version: '3.1'

services:

  db:
    image: mysql
    restart: always
    environment:
       MYSQL_ROOT_PASSWORD: 'pass'
       MYSQL_DATABASE: 'db'
       MYSQL_USER: 'user'
       MYSQL_PASSWORD: 'pass'

  adminer:
    image: adminer
    restart: always
    ports:
      - 8888:8080

And I start the container using following command from the same directory where yml is located:

docker-compose -f stack.yml up

Then I got this error:

then i get following error while logging in

hatef
  • 5,491
  • 30
  • 43
  • 46
Navin Gelot
  • 1,264
  • 3
  • 13
  • 32

4 Answers4

24

If you encounter this error but still wish to use MySQL v.8. You can do this by telling MySQL Server to use the legacy authentication plugin.

So, your compose file will look like this:

# Use root/example as user/password credentials

version: '3.1'

services:

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
       MYSQL_ROOT_PASSWORD: 'pass'
       MYSQL_DATABASE: 'db'
       MYSQL_USER: 'user'
       MYSQL_PASSWORD: 'pass'

  adminer:
    image: adminer
    restart: always
    ports:
      - 8888:8080
hatef
  • 5,491
  • 30
  • 43
  • 46
8

This worked for me:

image: mysql:5.7
trozen
  • 1,117
  • 13
  • 13
  • 6
    5:37 hours. Literally 5:37 hours to find this solution. – Rolly Mar 18 '19 at 22:38
  • 16
    Why using older MySQL 5.7 instead of stable MySQL 8.0 is suggested as the solution when author explicitly used the MySQL 8.0 tag in the question? – Tom Raganowicz Apr 08 '19 at 07:41
  • @NeverEndingQueue cause the realm of software currently revolves around copying and pasting whatever workaround works on stack overflow instead of reverse engineering the problem and actually solving it – TheLebDev Oct 03 '22 at 08:45
7

Since I just spent the last 3 hours looking for a solution for this same issue, I thought I'd extend on Hatef's answer. Adding the command --default-authentication-plugin=mysql_native_password to the container works but you have to first run the following set of commands:

docker rm $(docker ps -a -q)
docker volume prune -f

This is because without cleaning up the containers (yes, the ones marked exited), not all of the volumes will be pruned. And if you don't prune all of the volumes, the db will persist from the prior instance. This was confusing because containers are thought of as isolated instances, but not if the volumes are persisting between them.

Note that this resolves the same message in phpmyadmin and any other php-based mysql maintenance tool.


To make things easier, I wrote a powershell script that handles cleaning up the process/volumes between installations.

Write-Host 'Begin configuring MySQL Server'
$server = @{
    name = 'testdb';
    mysql_database = 'test';
    mysql_user = 'test';
    mysql_pass = '';
    mysql_root_pass = '';
    mysql_port = '3306';
    mysql_version = 'latest';
    adminer_port = '8080';
    adminer_version = 'latest';
};
$stackFilePath = '.\stack.yml';
if (Test-Path $stackFilePath)
{
    Write-Host 'Cleaning up old configuration file'
    Remove-Item $stackFilePath
}
$stack = @"
version: '3.7'
services:
  mysql:
    image: mysql:$($server.mysql_version)
    container_name: $($server.name)-mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ROOT_PASSWORD=$($server.mysql_root_pass)
      - MYSQL_DATABASE=$($server.mysql_database)
      - MYSQL_USER=$($server.mysql_user)
      - MYSQL_PASSWORD=$($server.mysql_pass)
      - MYSQL_PORT=$($server.mysql_port)
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
    restart: always
    ports:
      - $($server.mysql_port):$($server.mysql_port)
    expose:
      - $($server.mysql_port)
  adminer:
    image: adminer:$($server.adminer_version)
    container_name: $($server.name)-adminer
    depends_on:
      - mysql
    restart: always
    ports:
      - $($server.adminer_port):8080
"@;
Write-Host 'Writing new configuration file'
$stack | Out-File -FilePath $stackFilePath

Write-Host 'Clean up old processes'
docker rm $(docker ps -a -q)

Write-Host 'Clean up old volumes'
docker volume prune -f

Write-Host 'Create service from configurations'
docker-compose -f $stackFilePath up
Ethan DeLong
  • 163
  • 2
  • 12
2

You can just add this line after the image and it will start working again.

command: --default-authentication-plugin=mysql_native_password
jerryurenaa
  • 3,863
  • 1
  • 27
  • 17