DOCKER INSTALLATION: Can't Connect to MYSQL Server

Your software
My Mautic version is: 5
My PHP version is: 8
My Database type and version is: MYSQL

Your problem
My problem is: Docker installation can’t connect to MYSQL

These errors are showing in the log:
mautic_web-1 | mysqladmin: connect to server at ‘db.farm-mail-docker’ failed
mautic_web-1 | error: ‘Host ‘172.19.0.3’ is not allowed to connect to this MySQL server’
mautic_web-1 | mysqladmin: connect to server at ‘db.farm-mail-docker’ failed
mautic_web-1 | error: ‘Host ‘172.19.0.3’ is not allowed to connect to this MySQL server’
mautic_web-1 | mysqladmin: connect to server at ‘db.farm-mail-docker’ failed
mautic_web-1 | error: ‘Host ‘172.19.0.3’ is not allowed to connect to this MySQL server’
mautic_web-1 | mysqladmin: connect to server at ‘db.farm-mail-docker’ failed
mautic_web-1 | error: ‘Host ‘172.19.0.3’ is not allowed to connect to this MySQL server’
mautic_web-1 | mysqladmin: connect to server at ‘db.farm-mail-docker’ failed
mautic_web-1 | error: ‘Host ‘172.19.0.3’ is not allowed to connect to this MySQL server’

Steps I have tried to fix the problem:
-reviewed config and made sure all the env variables were set
-reviewed old posts for solutions
-reviewed github issues for solutions

My config:

**docker-compose.yml**
x-mautic-volumes:
  &mautic-volumes
  - ./config:/var/www/html/config:z
  - ./logs:/var/www/html/var/logs:z
  - ./media/files:/var/www/html/docroot/media/files:z
  - ./media/images:/var/www/html/docroot/media/images:z
  - ./cron:/opt/mautic/cron:z
  - mautic-docroot:/var/www/html/docroot:z
 
services:
  db:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - mysql-data:/var/lib/mysql
    healthcheck:
      test: mysqladmin --user=$$MYSQL_USER --password=$$MYSQL_PASSWORD ping
      start_period: 5s
      interval: 5s
      timeout: 5s
      retries: 10
    networks:
      - default
 
  nginx:
    image: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      - mautic-docroot:/var/www/html/docroot:z
    depends_on:
      - mautic_web
    ports:
      - 8002:80
    networks:
      - default
 
  mautic_web:
    image: mautic/mautic:5-fpm
    links:
      - db:mysql
    volumes: *mautic-volumes
 
    environment:
      - DOCKER_MAUTIC_LOAD_TEST_DATA=${DOCKER_MAUTIC_LOAD_TEST_DATA}
      - DOCKER_MAUTIC_RUN_MIGRATIONS=${DOCKER_MAUTIC_RUN_MIGRATIONS}
    env_file:
      - .mautic_env
    healthcheck:
      test: cgi-fcgi -bind -connect 192.168.1.125:9000
      start_period: 5s
      interval: 5s
      timeout: 5s
      retries: 100
    depends_on:
      db:
        condition: service_healthy
    networks:
      - default
 
  mautic_cron:
    image: mautic/mautic:5-fpm
    links:
      - db:mysql
    volumes: *mautic-volumes
    environment:
      - DOCKER_MAUTIC_ROLE=mautic_cron
    env_file:
      - .mautic_env
    depends_on:
      mautic_web:
        condition: service_healthy
    networks:
      - default
 
  mautic_worker:
    image: mautic/mautic:5-fpm
    links:
      - db:mysql
    volumes: *mautic-volumes
    environment:
      - DOCKER_MAUTIC_ROLE=mautic_worker
    env_file:
      - .mautic_env
    depends_on:
      mautic_web:
        condition: service_healthy
    networks:
      - default
 
volumes:
  mysql-data:
  mautic-docroot:
 
networks:
  default:
    name: ${COMPOSE_PROJECT_NAME}-docker
 
**.env**
COMPOSE_PROJECT_NAME=farm-mail
COMPOSE_NETWORK=${COMPOSE_PROJECT_NAME}-docker
MYSQL_HOST=db.${COMPOSE_NETWORK}
MYSQL_PORT=3306
MYSQL_DATABASE=mautic_db
MYSQL_USER=evon
MYSQL_PASSWORD=fmq2108b
MYSQL_ROOT_PASSWORD=fmq2108b
DOCKER_MAUTIC_RUN_MIGRATIONS=false
DOCKER_MAUTIC_LOAD_TEST_DATA=false
 
**.mautic_env**
COMPOSE_PROJECT_NAME=farm-mail
COMPOSE_NETWORK=${COMPOSE_PROJECT_NAME}-docker
MYSQL_HOST=db.${COMPOSE_NETWORK}
MYSQL_PORT=3306
MYSQL_DATABASE=mautic_db
MYSQL_USER=evon
MYSQL_PASSWORD=fmq2108b
MYSQL_ROOT_PASSWORD=fmq2108b
DOCKER_MAUTIC_RUN_MIGRATIONS=false
DOCKER_MAUTIC_LOAD_TEST_DATA=false
root@mail-list-server:~/mautic# cat .mautic_env 
# use this file for environment variables that can be used.
# see https://docs.mautic.org/en/5.x/
 
MAUTIC_DB_HOST="${MYSQL_HOST}"
MAUTIC_DB_PORT="${MYSQL_PORT}"
MAUTIC_DB_DATABASE="${MYSQL_DATABASE}"
MAUTIC_DB_USER="${MYSQL_USER}"
MAUTIC_DB_PASSWORD="${MYSQL_PASSWORD}"
 
MAUTIC_MESSENGER_DSN_EMAIL="doctrine://default"
MAUTIC_MESSENGER_DSN_HIT="doctrine://default"

Looks like the MySQL account you use in the mautic_web instance has not the required rights to use the database. In MySQL there is an IP ACL additionally to the user/password.

You don’t mention if the database already exists or you like to create a new one? If the later is true then this is wrong
DOCKER_MAUTIC_LOAD_TEST_DATA=false
and needs to be set to
DOCKER_MAUTIC_LOAD_TEST_DATA=true
to create a new database.

Thanks for taking the time to reply. I don’t know what you mean by IP ACL. I changed this value “DOCKER_MAUTIC_LOAD_TEST_DATA=true” according to your instructions but the same issue persists.

Try to open a shell in your web container:

docker exec -it mautic_web bash

Then try to connect to your database:

mysql -hdb.farm-mail-docker -uevon -pfmq2108b

You should get a connection prompt of MySQL, like that:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 52563
Server version: 8.0.40 MySQL Community Server - GPL

If you got “access denied” instead, try to use the root user, to connect:

mysql -hdb.farm-mail-docker -uroot -pfmq2108b

If that get’s the same, there is a problem with the database. Try to delete the locally mapped DB files inside /var/lib/mysql/ on your host.

The setting
DOCKER_MAUTIC_LOAD_TEST_DATA=true
will cause this inside entrypoint_mautic_web.sh to be executed:

# prepare mautic with test data
if [ "$DOCKER_MAUTIC_LOAD_TEST_DATA" = "true" ]; then
	su -s /bin/bash www-data -c 'php /var/www/html/bin/console doctrine:migrations:sync-metadata-storage'
	# mautic installation with dummy password and email, as the next step (doctrine:fixtures:load) will overwrite those
	su -s /bin/bash www-data -c 'php /var/www/html/bin/console mautic:install --force --admin_email willchange@mautic.org --admin_password willchange http://localhost'
	su -s /bin/bash www-data -c 'php /var/www/html/bin/console doctrine:fixtures:load -n'
fi

which will install a basic database structure if none exists. But as you can see, the database is persistet on the host running the docker containers and so you need to “docker compose down”, delete the local DB files and “docker compose up -d” again, to have it recreated.

Getting the same errors. /var/lib/mysql does’t exist.

Please check the database logs

docker logs db

to ensure a proper startup. If it starts correctly i assume something is wrong with the setup of the database, because of the access denied.

According to your docker-compose.yml file the MySQL database files are stored in an “named volume” called “mysql-data”. This volume will persist, and so it’s data, even if you rebuild the containers. You should remove that volume before rebuilding. Try to start from scratch (delete ALL data):

docker-compose rm -fsv
docker system prune -a

Then start over again

docker-compose up -d

After running those commands. This is the error I get

/mysqld/mysqld.sock' exists!
farm-mail_mautic_web_1  | mysqladmin: connect to server at '' failed
farm-mail_mautic_web_1  | error: 'Can't connect to server on 'localhost' (111)'
farm-mail_mautic_web_1  | Check that mariadbd is running and that the socket: '/run/mysqld/mysqld.sock' exists!

I decided to start from scratch with the original docker-compose file to make sure there weren’t any changes I made to it that screwed things up. However, now I’m back to the same error smh. I deleted and pruned everything before I re-installed.

mautic_web-1  | mysqladmin: connect to server at 'db.farm-mail-docker' failed
mautic_web-1  | error: 'Host '172.18.0.3' is not allowed to connect to this MySQL server'
root@mail-list-server:~/mautic# cat docker-compose.yml
x-mautic-volumes:
  &mautic-volumes
  - ./config:/var/www/html/config:z
  - ./logs:/var/www/html/var/logs:z
  - ./media/files:/var/www/html/docroot/media/files:z
  - ./media/images:/var/www/html/docroot/media/images:z
  - ./cron:/opt/mautic/cron:z
  - mautic-docroot:/var/www/html/docroot:z

services:
  db:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - mysql-data:/var/lib/mysql
    healthcheck:
      test: mysqladmin --user=$$MYSQL_USER --password=$$MYSQL_PASSWORD ping
      start_period: 5s
      interval: 5s
      timeout: 5s
      retries: 10
    networks:
      - default

  nginx:
    image: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      - mautic-docroot:/var/www/html/docroot:z
    depends_on:
      - mautic_web
    ports:
      - 8002:80
    networks:
      - default

  mautic_web:
    image: mautic/mautic:5-fpm
    links:
      - db:mysql
    volumes: *mautic-volumes

    environment:
      - DOCKER_MAUTIC_LOAD_TEST_DATA=${DOCKER_MAUTIC_LOAD_TEST_DATA}
      - DOCKER_MAUTIC_RUN_MIGRATIONS=${DOCKER_MAUTIC_RUN_MIGRATIONS}
    env_file:
      - .mautic_env
    healthcheck:
      test: cgi-fcgi -bind -connect 127.0.0.1:9000
      start_period: 5s
      interval: 5s
      timeout: 5s
      retries: 100
    depends_on:
      db:
        condition: service_healthy
    networks:
      - default

  mautic_cron:
    image: mautic/mautic:5-fpm
    links:
      - db:mysql
    volumes: *mautic-volumes
    environment:
      - DOCKER_MAUTIC_ROLE=mautic_cron
    env_file:
      - .mautic_env
    depends_on:
      mautic_web:
        condition: service_healthy
    networks:
      - default

  mautic_worker:
    image: mautic/mautic:5-fpm
    links:
      - db:mysql
    volumes: *mautic-volumes
    environment:
      - DOCKER_MAUTIC_ROLE=mautic_worker
    env_file:
      - .mautic_env
    depends_on:
      mautic_web:
        condition: service_healthy
    networks:
      - default

volumes:
  mysql-data:
  mautic-docroot:

networks:
  default:
    name: ${COMPOSE_PROJECT_NAME}-docker

But now it looks different than before. Post result of

docker ps

and

docker logs db
docker logs mautic_web

please.
Also exec into db Container and try mysql login there using the ‘root’ user and password.
If that works, post the output of

show grants;

docker ps

CONTAINER ID   IMAGE                 COMMAND                  CREATED       STATUS                   PORTS                                     NAMES
1337c894fede   nginx                 "/docker-entrypoint.…"   4 hours ago   Up 4 hours               0.0.0.0:8002->80/tcp, [::]:8002->80/tcp   farm-mail-nginx-1
ae32457b6411   mautic/mautic:5-fpm   "/entrypoint.sh php-…"   4 hours ago   Up 4 hours (unhealthy)   9000/tcp                                  farm-mail-mautic_web-1
9c74d3761eaa   mysql:8.0             "docker-entrypoint.s…"   4 hours ago   Up 4 hours (healthy)     3306/tcp, 33060/tcp                       farm-mail-db-1

docker logs mautic_web

mysqladmin: connect to server at 'db.farm-mail-docker' failed
error: 'Host '172.18.0.3' is not allowed to connect to this MySQL server'
mysqladmin: connect to server at 'db.farm-mail-docker' failed
error: 'Host '172.18.0.3' is not allowed to connect to this MySQL server'

Can’t login to mysql

oot@ae32457b6411:/var/www/html/docroot# mysql -hdb.farm-mail-docker -u root -p fmq2108b
Enter password:
ERROR 1130 (HY000): Host '172.18.0.3' is not allowed to connect to this MySQL server
root@ae32457b6411:/var/www/html/docroot# mysql
ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2)

I gave up on this and managed to get it installed with composer.