How do I connect to a local MySQL database? What should I use as the HOST?

The Requirement

Use local MYSQL database instead of the usual docker-based MYSQL database.

Question

How should I override the config parameters, so that the tutor can connect successfully to the local database ? Maybe I’m missing something ?

I know that we can overwrite MYSQL_HOST, MYSQL_PORT, … values by setting the config (see).
But the the LMS and CMS can not connect to the local mysql database, see: Error Logs.
for easier reproduction I have listed all my steps under: Reproduce (all Steps)

(I personally think, that it can’t connect because by setting MYSQL_HOST to localhost, the LMS/CMS docker containers would target the container itself (cause localhost). Therefore I tried setting MYSQL_HOST to host.docker.internal but it didnt work either)

Reproduce (all Steps)

tutor-version: 19.0.2
ubuntu-server-version: 24.04.02

Install MYSQL Database locally

Run following commands

MYSQL_ROOT_PASSWORD='root'

sudo apt update && apt install mysql-server -y

mysql -u root <<EOF
-- Set root password (MySQL 8.0+ syntax)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$MYSQL_ROOT_PASSWORD';
-- Remove anonymous users
DELETE FROM mysql.user WHERE User='';
-- Disallow root login remotely  
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
-- Remove test database
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
-- Reload privilege tables
FLUSH PRIVILEGES;
EOF

Now the local mysql database can be accessed with

mysql -u root -p -h localhost -P 3306

Override Configs

Now the next step I think would be to override the Config parameters as mentioned in the docs

tutor config save \
  --set RUN_MYSQL=false \
  --set MYSQL_HOST=localhost \
  --set MYSQL_PORT=3306 \
  --set MYSQL_ROOT_USERNAME=root \
  --set MYSQL_ROOT_PASSWORD=root

now launching tutor

tutor local launch --non-interactive

… it does not connect to the previously installed local database.

Error Logs

 tutor local launch --non-interactive
Configuration saved to /home/humanityfirst-root/.local/share/tutor/config.yml
Environment generated in /home/humanityfirst-root/.local/share/tutor/env
======================================
        Building Docker images
======================================
No image to build
==============================================
        Stopping any existing platform
==============================================
docker compose -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.yml -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.prod.yml --project-name tutor_local stop
[+] Stopping 11/11
 ✔ Container tutor_local-mfe-1          Stopped                                                              0.0s 
 ✔ Container tutor_local-cms-worker-1   Stopped                                                              0.0s 
 ✔ Container tutor_local-lms-worker-1   Stopped                                                              0.0s 
 ✔ Container tutor_local-caddy-1        Stopped                                                              0.0s 
 ✔ Container tutor_local-cms-1          Stopped                                                              0.0s 
 ✔ Container tutor_local-lms-1          Stopped                                                              0.0s 
 ✔ Container tutor_local-redis-1        Stopped                                                              0.0s 
 ✔ Container tutor_local-mongodb-1      Stopped                                                              0.0s 
 ✔ Container tutor_local-smtp-1         Stopped                                                              0.0s 
 ✔ Container tutor_local-meilisearch-1  Stopped                                                              0.0s 
 ✔ Container tutor_local-permissions-1  Stopped                                                              0.0s 
======================================================
        Starting the platform in detached mode
======================================================
docker compose -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.yml -f /home/humanityfirst-root/.local/share/tutor/env/dev/docker-compose.yml --project-name tutor_dev stop
docker compose -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.yml -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.prod.yml --project-name tutor_local up --remove-orphans -d
[+] Running 12/12
 ✔ Container tutor_local-mysql-1        Removed                                                              0.0s 
 ✔ Container tutor_local-permissions-1  Started                                                              0.1s 
 ✔ Container tutor_local-caddy-1        Started                                                              0.1s 
 ✔ Container tutor_local-smtp-1         Started                                                              0.1s 
 ✔ Container tutor_local-mongodb-1      Started                                                              0.1s 
 ✔ Container tutor_local-meilisearch-1  Started                                                              0.1s 
 ✔ Container tutor_local-redis-1        Started                                                              0.1s 
 ✔ Container tutor_local-lms-1          Started                                                              0.0s 
 ✔ Container tutor_local-mfe-1          Started                                                              0.1s 
 ✔ Container tutor_local-cms-1          Started                                                              0.1s 
 ✔ Container tutor_local-lms-worker-1   Started                                                              0.1s 
 ✔ Container tutor_local-cms-worker-1   Started                                                              0.1s 
================================================
        Database creation and migrations
================================================
Initialising all services...
Running init task in lms
docker compose -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.yml -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.prod.yml --project-name tutor_local -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.jobs.yml run --rm lms-job sh -e -c '# The initialization job contains various re-install operations needed to be done
# on mounted directories (edx-platform, /mnt/*xblock, /mnt/<edx-ora, search, enterprise>)
# 1. /mnt/*
# Whenever xblocks or other installable packages are mounted, during the image build, they are copied over to container
# and installed. This results in egg_info generation for the mounted directories. However, the egg_info is not carried
# over to host. When the containers are launched, the host directories without egg_info are mounted on runtime
# and disappear from pip list.
#
# 2. edx-platform
# When a new local copy of edx-platform is bind-mounted, certain build
# artifacts from the openedx image'"'"'s edx-platform directory are lost.
# We regenerate them here.


for mounted_dir in /mnt/*; do
    if [ -f $mounted_dir/setup.py ] && ! ls $mounted_dir/*.egg-info >/dev/null 2>&1 ; then
        echo "Unable to locate egg-info in $mounted_dir"
        pip install -e $mounted_dir
    fi
done

if [ -f /openedx/edx-platform/bindmount-canary ] ; then
	# If this file exists, then edx-platform has not been bind-mounted,
	# so no build artifacts need to be regenerated.
	echo "Using edx-platform from image (not bind-mount)."
	echo "No extra setup is required."
	exit
fi

echo "Performing additional setup for bind-mounted edx-platform."
set -x # Echo out executed lines

# Regenerate Open_edX.egg-info
pip install -e .

# Regenerate node_modules
npm clean-install

# Regenerate static assets.
npm run build-dev

set -x
echo "Done setting up bind-mounted edx-platform."'
[+] Creating 2/2
 ✔ Container tutor_local-mongodb-1      Running                                                              0.0s 
 ✔ Container tutor_local-meilisearch-1  Running                                                              0.0s 
[+] Running 1/1
 ✔ Container tutor_local-permissions-1  Started                                                              0.1s 
Using edx-platform from image (not bind-mount).
No extra setup is required.
Running init task in mysql
docker compose -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.yml -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.prod.yml --project-name tutor_local -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.jobs.yml run --rm mysql-job sh -e -c 'echo "Initialising MySQL..."
mysql_connection_max_attempts=10
mysql_connection_attempt=0
until mysql -u root --password="root" --host "localhost" --port 3306 -e '"'"'exit'"'"'
do
    mysql_connection_attempt=$(expr $mysql_connection_attempt + 1)
    echo "    [$mysql_connection_attempt/$mysql_connection_max_attempts] Waiting for MySQL service (this may take a while)..."
    if [ $mysql_connection_attempt -eq $mysql_connection_max_attempts ]
    then
      echo "MySQL initialisation error" 1>&2
      exit 1
    fi
    sleep 10
done
echo "MySQL is up and running"

# edx-platform database
mysql -u root --password="root" --host "localhost" --port 3306 -e "CREATE DATABASE IF NOT EXISTS openedx;"
mysql -u root --password="root" --host "localhost" --port 3306 -e "CREATE USER IF NOT EXISTS '"'"'openedx'"'"';"
mysql -u root --password="root" --host "localhost" --port 3306 -e "ALTER USER '"'"'openedx'"'"'@'"'"'%'"'"' IDENTIFIED BY '"'"'HAzSlLsQ'"'"';"
mysql -u root --password="root" --host "localhost" --port 3306 -e "GRANT ALL ON openedx.* TO '"'"'openedx'"'"'@'"'"'%'"'"';"'
Initialising MySQL...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [1/10] Waiting for MySQL service (this may take a while)...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [2/10] Waiting for MySQL service (this may take a while)...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [3/10] Waiting for MySQL service (this may take a while)...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [4/10] Waiting for MySQL service (this may take a while)...
^[[Amysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [5/10] Waiting for MySQL service (this may take a while)...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [6/10] Waiting for MySQL service (this may take a while)...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [7/10] Waiting for MySQL service (this may take a while)...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [8/10] Waiting for MySQL service (this may take a while)...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [9/10] Waiting for MySQL service (this may take a while)...
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    [10/10] Waiting for MySQL service (this may take a while)...
MySQL initialisation error







Error: Command failed with status 1: docker compose -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.yml -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.prod.yml --project-name tutor_local -f /home/humanityfirst-root/.local/share/tutor/env/local/docker-compose.jobs.yml run --rm mysql-job sh -e -c echo "Initialising MySQL..."
mysql_connection_max_attempts=10
mysql_connection_attempt=0
until mysql -u root --password="root" --host "localhost" --port 3306 -e 'exit'
do
    mysql_connection_attempt=$(expr $mysql_connection_attempt + 1)
    echo "    [$mysql_connection_attempt/$mysql_connection_max_attempts] Waiting for MySQL service (this may take a while)..."
    if [ $mysql_connection_attempt -eq $mysql_connection_max_attempts ]
    then
      echo "MySQL initialisation error" 1>&2
      exit 1
    fi
    sleep 10
done
echo "MySQL is up and running"

# edx-platform database
mysql -u root --password="root" --host "localhost" --port 3306 -e "CREATE DATABASE IF NOT EXISTS openedx;"
mysql -u root --password="root" --host "localhost" --port 3306 -e "CREATE USER IF NOT EXISTS 'openedx';"
mysql -u root --password="root" --host "localhost" --port 3306 -e "ALTER USER 'openedx'@'%' IDENTIFIED BY 'HAzSlLsQ';"
mysql -u root --password="root" --host "localhost" --port 3306 -e "GRANT ALL ON openedx.* TO 'openedx'@'%';"

The Docker containers would resolve localhost to themselves. So, if your DB is running directly on the host where the Docker containers are running, you should use host.docker.internal as the HOST value. This might or might not work depending on the Docker version, platform and other docker network config.

So please Google for something like “connecting to docker host from a container” and you should be able to find the right config to get it working.