Docker Community Forums

Share and learn in the Docker community.

MariaDB host not allowed to connect

Hello. I’m new to Docker, trying to setup a PHP web server and MariaDB database. I can’t connect to the database from the other container. I used MYSQL_ROOT_HOST to create the root@db user automatically. But it only allows connections to root@localhost. To make it work I have to create a new user called root@%
I see the skip_name_resolve variable is turned on by default. Is that the problem?

# mysql -h db -uroot -ppassword
ERROR 1130 (HY000): Host '192.168.240.3' is not allowed to connect to this MariaDB server

It looks like the user is there:

MariaDB [(none)]> select Host,User,plugin from mysql.user; 
+-----------+-------------+-----------------------+
| Host      | User        | plugin                |
+-----------+-------------+-----------------------+
| localhost | mariadb.sys | mysql_native_password |
| localhost | root        | mysql_native_password |
| db        | root        | mysql_native_password |
+-----------+-------------+-----------------------+

My compose file is like this:

version: "3"
services:
  web:
    image: php:7.2-apache
    ports:
      - "80:80"
    volumes:
      - htdocs:/var/www/html/

  db:
    image: mariadb:10.4
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_ROOT_HOST: db
    volumes:
      - mysql-data:/var/lib/mysql
volumes:
  mysql-data:
  htdocs:

Ok I figured out this was caused by MYSQL_ROOT_HOST. After removing that line, it creates root@% automatically.