0

I have a really simple application in dotnet core 2.1 which talks to a MySQL database. The application uses Entity Framework like this:

var connectionString = Configuration.GetConnectionString("DatabaseConnection");
services.AddDbContext<ChtrDbContext>(options => options.UseMySql(connectionString));

Where the connectionString looks like this:

"DatabaseConnection": "Server=db;port=3306;Database=chtr;userid=dbuser;Password=dbuserpassword"

The application is dockerized and I use docker-compose up --build to start the environment.

When I navigate to localhost:8080/graphql, which is my GraphiQL endpoint and I try to do a simple query against the database, I'm unable to log in. This is the log file:

 Access denied for user 'dbuser'@'172.19.0.3' (using password: YES)
chtr.server_1  |    at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 360

It says that dbuser which is the configured user, doesnt have access rights to my database.

Running docker ps which lists all my containers I can see that the mysql container is running. Then again I run docker inspect chtrserver_db_1 which is the container name. Scrolling down to the Config section of this json file gives me the following:

"Env": [
                "MYSQL_RANDOM_ROOT_PASSWORD=1",
                "MYSQL_DATABASE=chtr",
                "MYSQL_USER=dbuser",
                "MYSQL_PASSWORD=dbuserpassword",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.7",
                "MYSQL_MAJOR=5.7",
                "MYSQL_VERSION=5.7.26-1debian9"
            ],

Alright, so I've confirmed that the docker container is running and the credentials specified in the connecting string is equal to what's configured within the container. How come that I'm unable to log in still?

The docker-compose.yml file looks like this:

version: '3.0'

services:
   db:
     image: mysql:5.7
     environment:
       MYSQL_RANDOM_ROOT_PASSWORD: 1
       MYSQL_DATABASE: chtr
       MYSQL_USER: dbuser
       MYSQL_PASSWORD: dbuserpassword
     volumes:
       - dbdata:/var/lib/mysql
       - ./Scripts:/docker-entrypoint-Dataseed.d
     restart: always

   chtr.server:
     depends_on:
       - db
     image: trebias/chtr.server
     build:
       context: .
     ports:
       - "8080:80"

volumes:
    dbdata:

Any suggestions on how to move forward?

4
  • 1
    you haven't exposed the ports to mysql Commented May 13, 2019 at 17:26
  • @DanielA.White Care to explain a bit more? I would guess its under the ports section in my yml file, but Im fairly new to docker. Thanks! Commented May 13, 2019 at 17:30
  • 1
    @DanielA.White, the mysql ports doesn't need to be exposed if the two containers are on the same network. Commented May 13, 2019 at 17:30
  • @codestation They are. Commented May 13, 2019 at 17:31

2 Answers 2

1

Are you 100% sure that the credentials matches the user on MySQL? Note that the MYSQL_USER and MYSQL_PASSWORD are only used once when the database is created for the first time so if you changed them while keeping the database then your assumption that the ENV variables for the credentials matches the one in the database is no longer true.

If this is the case then you can delete your database and start over. Since you are using a named volume you need to delete it by hand (docker rm will never delete a named volume when using a rm command, only anonymous ones).

To delete a named volume you can use the following command:

docker volume rm xxxx_dbdata

Where xxxx should be your project name (the directory where your compose file resides). You can also see the list of docker volumes by doing docker volume ls.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm not 100 % sure but I run the docker -rm -v command to remove the images everytime I tear down the environment. Is there any way to figure this out?
@TobiasMoeThorstensen -v only removes anonymous volumes. In your compose file you are using a named volume that you must remove it explicitly with docker volume rm xxxxx_dbdata (xxxxx would be your project name).
To clearify; what is the project name? is it defined in my yml file? Can any docker commands help me here?
@TobiasMoeThorstensen the project name is the directory name where your compose file resides. You can list all your created volumes (named and anonymous) with docker volume ls.
It seems like this was the case. Another volume was running side by side since I've configured different credentials earlier today.. Thanks so much. I suggest that you write an answer based on the chain of comments. I will accept that.
0

Make sure you have your ports specified in your docker-compose

services:
    db:
     image: mysql:5.7
     environment:
       MYSQL_RANDOM_ROOT_PASSWORD: 1
       MYSQL_DATABASE: chtr
       MYSQL_USER: dbuser
       MYSQL_PASSWORD: dbuserpassword
     volumes:
       - dbdata:/var/lib/mysql
       - ./Scripts:/docker-entrypoint-Dataseed.d
     restart: always
     ports:
       - "3306:3306"

Alos, are you creating a network in your docker-compose?

1 Comment

As written in a comment, ports doesnt need to be specified as long both containers are on the same network

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.