1

Using docker compose i have a mysql 5.7 database container, i set the root password and a user password but they don't work, the docker-compose:

version: '3.8'
services:
  viprs-proxy:
    platform: linux/amd64
    image: nginx:alpine
    container_name: viprs-proxy
    depends_on:
      - viprs-website
    volumes:
      - ./nginx/proxy.conf:/etc/nginx/nginx.conf
    ports:
      - 80:80
    networks:
      - viprs-net
  viprs-website:
    platform: linux/amd64
    image: nginx
    container_name: viprs-website
    depends_on:
      - php
      - viprs-website-database
    volumes:
      - ./website/nginx/site.conf:/etc/nginx/conf.d/default.conf
      - ./website:/usr/share/nginx/html
      - ./website/logs:/var/log/nginx
      - viprs-uploads:/usr/share/nginx/html/wp-content/uploads
    ports:
      - 80
    links:
      - php
    networks:
      - viprs-net
  php:
    platform: linux/amd64
    #image: php:7-fpm
    image: viprs-php
    container_name: php
    volumes:
      - ./website:/usr/share/nginx/html
    ports:
      - 9000
    networks:
      - viprs-net
  viprs-website-database:
    platform: linux/amd64
    image: mysql:5.7
    container_name: viprs-db
    command: --init-file /usr/share/nginx/website.sql
    volumes:
      - ./website.sql:/usr/share/nginx/website.sql
      - viprs-db:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: testpassword
      MYSLQ_DATABASE: viprs
      MYSQL_USER: viprs
      MYSQL_PASSWORD: testpassword
    networks:
      - viprs-net
networks:
  viprs-net:
volumes:
  viprs-uploads:
  viprs-db:

Now if i log into bash:

docker exec -it viprs-db bash

and try to log into mysql:

mysql -u root -p

It just says access is denied, it doesn't matter if i am using the root user or the viprs user.

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

If i output $MYSQL_ROOT_PASSWORD it gives:

echo $MYSQL_ROOT_PASSWORD
testpassword

I have seen loads of people with this issue but can't find any solution that works. The environment is set as per the image documentation on docker hub so i'm confused.

In fact, i can log in as root without a password, so something isn't right.

noname
  • 153
  • 2
  • 12
  • Can you check if answers of this answer helps https://stackoverflow.com/q/40149880/2704032 – Vishrant May 02 '22 at 15:53
  • If I try to reproduce it, it works as it should. I've had to remove the `command` and both volumes since I don't have those. So that leads me to think that your issue lies somewhere in those. – Hans Kilian May 02 '22 at 16:35

1 Answers1

3

The environment variables are only used if there is no database present when then container starts and MySQL has to create one.

If there already is a database, the users defined in that database are used and no new users are created. Since you have a volume mapping on /var/lib/mysql chances are that you already have a database.

To verify if that's the issue, you can try removing the /var/lib/mysql mapping. That will cause the container to create a new database when it starts using the environment variable values.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • there isn't an existing database, i completely deleted the volume before firing it up, i have also tried to remove the mapping part and it still doesn't work – noname May 02 '22 at 16:19
  • i can get in and create a database and user, but those env vars are just being ignored – noname May 02 '22 at 16:59
  • @noname You have misspelled `MYSLQ_DATABASE`, so that's probably why it doesn't create a 'viprs' database. – Hans Kilian May 02 '22 at 17:03
  • I noticed that but it wasn't the issue, it was the --init-file command, it seems to try to run this before creating the database and crashes it. So i just manually imported the database, only needs doing the first time anyway – noname May 02 '22 at 18:08