Reputation: 8981
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?
Upvotes: 0
Views: 395
Reputation: 3498
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
.
Upvotes: 1
Reputation: 152
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?
Upvotes: 0