MySQL access denied

Hey everyone,

not sure if this is the right place for this issue. I am desperately trying to connect to a MySQL DB via Django and it doesn’t make sense to me at all. My setup is as follows

  1. Creating a docker container running a MySQL DB
# variables

# Start to run docker container
docker run --rm -d \
  --name=$CONTAINER_NAME \
  -p $PORT:$PORT \
  -t mysql:$MYSQL_VERSION \
echo "Created new container"

echo "Wait " $SLEEP_TIME "sec for the database to be started"
echo "Finished waiting"

# Create Dev user
docker exec $CONTAINER_NAME bash -c \
  "mysql -hlocalhost -P 3306 -u$ROOT_USER -p$ROOT_PASSWORD -e \"CREATE USER '$DEV_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DEV_PASSWORD'; GRANT ALL PRIVILEGES ON *.* TO '$DEV_USER'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;"\"
  1. Try to run the server / perform migrations
    I have configured the following for the database connection
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "myDatabase",
        "USER": "dev",
        "PASSWORD": "dev",
        "HOST": "localhost",
        "PORT": "3306",

But all i get is

django.db.utils.OperationalError: (1045, "Access denied for user 'dev'@'localhost' (using password: YES)")

I have no problems accessing the DB with the root or dev user from the shell, both users even have all the privileges. But Django tells me that the dev user is not allowed to access the db. Please help me!

Is your application running in the host, or in another container? If you’re running your application in another container, it’s not connecting to localhost, it’s connecting to the container name. Also, this means that you need to grant permissions to the account at the container in which it’s being run. (For testing purposes, I’d suggest granting permissions to the account to all hosts.)

Hey Ken, my application is running in host. I also tried to connect with DBeaver to the database, which also worked just fine (Parameters > host: localhost, port: 3306, user: dev, password: dev)

Granting access to all hosts is being done by replacing the ‘localhost’ in the last statement of the bash script by ‘%’ right? I tried that before and the same issue still occurred

I can’t address what you’re seeing with DBeaver - it’s possible that it’s using a different / custom library for connection.

But what I am seeing is that it’s not working with the mysql command-line client, unless:

  • I specify the docker container’s IP address for the connection
  • I connect to it from another docker container and reference the server by container name

I believe that it should also work if you specify --network host, but I haven’t been able to get that to work yet.

You’re right DBeaver could be totally different. Regarding your two comments:

  1. I tried to get the containers ip by
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db-local

a got something like Both Django and DBeaver timed out with this ip

  1. I didn’t try that one yet, but im sure that this will work

But what did work for me was to simply use the hostname, configured like so

    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "myDatabase",
        "USER": "dev",
        "PASSWORD": "dev",
        "HOST": os.uname().nodename,
        "PORT": "3306",