I deployed mysql via docker compose and it’s available at 0.0.0.0 and at 127.0.0.1 via commands mysql --host=127.0.0.1 --user=root --password=team6 --port=53366
and mysql --host=0.0.0.0 --user=root --password=team6 --port=53366
but mysql --host=localhost --user=root --password=team6 --port=53366
fails due to "ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)", why?
My end goal is for this code to work
con = await mysql.createConnection({
host: 'localhost',
port: '53366', //We need to consider adding new users
user: 'root',
password: 'team6',
database: 'Yahtzee',
});
My compose file:
# Compose file for creating a database named Yahtzee in a mysql container
version: '3.8'
services:
team6db:
image: mysql/mysql-server:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: team6
MYSQL_DATABASE: Yahtzee
MYSQL_ROOT_HOST: '%'
volumes:
- team6db:/var/lib/mysql
- ./Initdb.sql:/docker-entrypoint-initdb.d/Initdb.sql
- ./InsertSampleGameRecords.sql:/docker-entrypoint-initdb.d/InsertSampleGameRecords.sql
ports:
- '53366:3306'
team6phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
restart: always
depends_on:
- team6db
environment:
PMA_HOST: team6db
PMA_USER: root
PMA_PASSWORD: team
ports:
- '50086:80'
volumes:
team6db:
2
Answers
Ok, so weird answer here (I am OP but can't accept answer yet).
Ended up having a typo in my connection where I defined my port as "user" within the library I was calling (should have just been passing the connection in but whatever that's a separate problem). Now the tests work fine with localhost as the port but
mysql --host=localhost --user=root --password=team6 --port=53366
still fails with the errorERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
whilemysql --host=127.0.0.1 --user=root --password=team6 --port=53366
andmysql --host=0.0.0.0 --user=root --password=team6 --port=53366
both succeed. I don't understand why and I would like to know but I've been working on this for far longer than I'd like to admit and given my tests work now I'm moving on.By default, the each container gets an IP address. So, in your case
localhost
host name would not work. You can get IP address for your docker container using either of below commands:OR
Use IP address of container instead of
localhost
while creating connection.