Can't log in to MySQL - Access denied for user 'foobar'@'localhost' (using password: NO)

March 17, 2019 1.8k views
MySQL Ubuntu

Hello,

From shell, I am able to log in to mysql using both root and my custom mysql user (let's call it 'foobar'). However, I can't log in via any external or local-based method such as phpMyAdmin, adminer, HeidiSQL, etc.

I see no errors in access.log nor error.log, but the error in the above programs seems to be of the 1045 type (Access denied for user 'foobar'@'localhost' (using password: NO)). If it makes any difference, I am still using the IP via http, not domain name with https as my server is not yet production ready.

Could someone kindly point me to information on where I could try to figure out why this might be happening please?

Thank you

1 Answer

Greetings!

This is interesting. I could see a clear reason for HeidiSQL, and maybe that leads to the reason for the others. I assume phpMyAdmin and Adminer are set up on the server while HeidiSQL is run remotely. If they're all three remote, then my idea makes sense across all of them.

When you create a user in MySQL, you define a host. If you do not define a host, it defaults to localhost. This means that all users are "user@ip" and by default "user@localhost." So when you try to log in externally, you're not just logging in as "user" but as "user@yourexternalip" instead.

Creating a remote user works like this from the shell:

GRANT ALL PRIVILEGES ON database.* TO 'username'@'ipaddress' IDENTIFIED BY 'password';

To connect remotely, ipaddress should be your external IP as found here:
https://ifconfig.me/

Jarland

Have another answer? Share your knowledge.