Menu

MySQL ERROR 1045 "Access denied" with the right password: localhost vs 127.0.0.1
📰

MySQL ERROR 1045 "Access denied" with the right password: localhost vs 127.0.0.1

DEV Community: database·Mer Go·3 days ago
#gCj73Gis
#devtodatabase#devto#dev#user#app_user
Long article (567 words) — no AI summary yet.
Reading 0:00
15s threshold

The password is definitely correct, yet you get ERROR 1045 (28000): Access denied for user ... . A very common reason: the user was created as 'app_user'@'localhost' , but you're connecting over TCP with -h 127.0.0.1 .

I reproduced it on MySQL 8.4.13 in a container and ran the check and the fix. Every output below is exactly what the mysql client printed.

Reproduce

As root, create a user that only exists for localhost :

CREATE DATABASE shop ; CREATE USER 'app_user' @ 'localhost' IDENTIFIED BY 'App_Pw_2026' ; GRANT ALL ON shop . * TO 'app_user' @ 'localhost' ;

Enter fullscreen mode

Exit fullscreen mode

Connect with the correct password and an explicit host:

$ mysql -h 127.0.0.1 -u app_user -p 'App_Pw_2026' shop

Enter fullscreen mode

Exit fullscreen mode

ERROR 1045 (28000): Access denied for user 'app_user'@'127.0.0.1' (using password: YES)

Enter fullscreen mode

Exit fullscreen mode

Look at 'app_user'@'127.0.0.1' . The server sees the client as 127.0.0.1 , not localhost .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.