MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
Asked Answered
R

45

540

First let me mention that I've gone through many suggested questions and found no relevent answer. Here is what I'm doing.

I'm connected to my Amazon EC2 instance. I can login with MySQL root with this command:

mysql -u root -p

Then I created a new user bill with host %

CREATE USER 'bill'@'%' IDENTIFIED BY 'passpass';

Granted all the privileges to user bill:

grant all privileges on *.* to 'bill'@'%' with grant option;

Then I exit from root user and try to login with bill:

mysql -u bill -p

entered the correct password and got this error:

ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

Ronda answered 24/4, 2012 at 13:42 Comment(13)
I think that will log you on as 'bill'@'localhost' which is probably not what you want.Adhern
Did you FLUSH PRIVILEGES?Axiom
Okay, I tried this without any success. Any other suggestion please.Ronda
What version of the server are you running? I've seen 5.1 behave oddly about this.Plumy
This happened to me while installing Magento and I made a much sillier mistake. Putting 'mysql -u magento -p magento' was prompting me for a password and instead of the default password I was putting the root password in.Humiliation
make sure to add with grant option to the end of the grant line. Several of the answers below have this buried in their lengthy answers. This is the only thing wrong with what you were doing. Just wasted some time on this myself.Kailyard
@Ronda Why not accept one as an answer?Rawinsonde
@Rawinsonde unfortunately, none of the suggested solutions worked for me at the time this question was posted. Please see my own answer that helped me getting outa this situation. That is the reason I did not mark any of them as the answer. Probably I can mark the highest ranked response as answer.Ronda
Very dumb of me but I should still mention in case someone else also makes this mistake since the error code is the same. I did not create a user by the name "bill" (or whatever name) before running the command mysql -u bill -pBlade
Creating a user other than root to access the database is more secure; and going further to limit their access to only one table is even one better. Thanks for the example.Strain
Anyone else was using wrong combination of username and password ? :)Vlada
Just noted: I was failed to create user and password. Accoding to the error message, I searched and found this thread. Please check the username and password if you are the same as me.Puissance
wrong password also cause 1045 !!!Ozzy
L
495

You probably have an anonymous user ''@'localhost' or ''@'127.0.0.1'.

As per the manual:

When multiple matches are possible, the server must determine which of them to use. It resolves this issue as follows: (...)

  • When a client attempts to connect, the server looks through the rows [of table mysql.user] in sorted order.
  • The server uses the first row that matches the client host name and user name.

(...) The server uses sorting rules that order rows with the most-specific Host values first. Literal host names [such as 'localhost'] and IP addresses are the most specific.

Therefore such an anonymous user would "mask" any other user like '[any_username]'@'%' when connecting from localhost.

'bill'@'localhost' does match 'bill'@'%', but would match (e.g.) ''@'localhost' beforehands.

The recommended solution is to drop this anonymous user (this is usually a good thing to do anyways).


Below edits are mostly irrelevant to the main question. These are only meant to answer some questions raised in other comments within this thread.

Edit 1

Authenticating as 'bill'@'%' through a socket.


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass --socket=/tmp/mysql-5.5.sock
    Welcome to the MySQL monitor (...)
    
    mysql> SELECT user, host FROM mysql.user;
    +------+-----------+
    | user | host      |
    +------+-----------+
    | bill | %         |
    | root | 127.0.0.1 |
    | root | ::1       |
    | root | localhost |
    +------+-----------+
    4 rows in set (0.00 sec)
    
    mysql> SELECT USER(), CURRENT_USER();
    +----------------+----------------+
    | USER()         | CURRENT_USER() |
    +----------------+----------------+
    | bill@localhost | bill@%         |
    +----------------+----------------+
    1 row in set (0.02 sec)
    
    mysql> SHOW VARIABLES LIKE 'skip_networking';
    +-----------------+-------+
    | Variable_name   | Value |
    +-----------------+-------+
    | skip_networking | ON    |
    +-----------------+-------+
    1 row in set (0.00 sec)
    

Edit 2

Exact same setup, except I re-activated networking, and I now create an anonymous user ''@'localhost'.


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql
    Welcome to the MySQL monitor (...)
    
    mysql> CREATE USER ''@'localhost' IDENTIFIED BY 'anotherpass';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> Bye

    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        --socket=/tmp/mysql-5.5.sock
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        -h127.0.0.1 --protocol=TCP
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        -hlocalhost --protocol=TCP
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

Edit 3

Same situation as in edit 2, now providing the anonymous user's password.


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -panotherpass -hlocalhost
    Welcome to the MySQL monitor (...)

    mysql> SELECT USER(), CURRENT_USER();
    +----------------+----------------+
    | USER()         | CURRENT_USER() |
    +----------------+----------------+
    | bill@localhost | @localhost     |
    +----------------+----------------+
    1 row in set (0.01 sec)

Conclusion 1, from edit 1: One can authenticate as 'bill'@'%'through a socket.

Conclusion 2, from edit 2: Whether one connects through TCP or through a socket has no impact on the authentication process (except one cannot connect as anyone else but 'something'@'localhost' through a socket, obviously).

Conclusion 3, from edit 3: Although I specified -ubill, I have been granted access as an anonymous user. This is because of the "sorting rules" advised above. Notice that in most default installations, a no-password, anonymous user exists (and should be secured/removed).

Lashelllasher answered 26/6, 2012 at 22:36 Comment(17)
For anyone curious as to why 'bill'@'localhost' matches ''@'localhost' like I was, an empty string effectively acts a wild card in MySQL's authentication algorithm.Godiva
A short query RENAME USER ''@'localhost' TO ''@'%'; made my day! Thank you!Johppah
@Sanja Be extra careful with this workaround. You may be allowing anonymous access to your database from any location. In case of doubt, I would rather delete the user.Lashelllasher
@Lashelllasher Thank you for this comment! Probably I should just delete anonymous user. P.S. I found dev.mysql.com/doc/refman/5.1/en/default-privileges.html that says that these users surely can be deleted: DROP USER ''@'localhost';. They are not needed for some special purpose.Johppah
I dropped the anonymous user, but things still did not work. Then I found that I needed to issue also "FLUSH PRIVILEGES". It would be helpful to mention that also.Pocked
@NeemePraks This is probably because you dropped this user by directly manipulating the mysql.user table. FLUSH PRIVILEGES shouldn't be required if you rather DROP USER ''@localhostLashelllasher
Yep, that is true, I just deleted the row from "user" table.Pocked
@Ronda Why not choose this as best answer?Drowse
I'm so glad to have found this. My day was almost ruined! I'd put hours of time into looking for the error and I've come to find that it was all due to some stupid ''@'localhost'. We live and we learn!Aldrich
excellent, wonder why it took me days to find this, why it's stuck on page 20 or so of google results most of which are all sites leeching other comments from SO...Slunk
Spent a day on this, it was finally looking through multiple users in mysql.user that led me to it, from this answer.Tangential
I used the sudo mysql_secure_installation command to remove the anonymous usersTonsillotomy
This is the answer! Why does this not come in bold capital letters in every mysql manual. DELETE THE ANONYMOUS USER OR IT WILL KILL ALL YOUR ATTEMPTS TO LOGIN FROM LOCALHOST!Deutoplasm
I am working with MariaDB. I dropped the anonymous user, but still had issues. I could connect via Workbench, but not through my web service using the same account and port information. I ended up creating a new user account which works. This answer did help me find the right approach. There are a few other user accounts in MariaDB that may have prevented my original account from working.Poree
I am using the CakePHP 3.4.0 and was getting the same error. I have XAMPP on OS X Sierra. So after fixing the path on CLI and dropping the anonymous users (ANY), it started working. Thanks.Flyspeck
I don't see such a user in my table but have the same error as the OP...maybe there's a bug in new ubuntu dist of mysql?Matronage
Sweet! This was it!Druggist
T
165

Try:

~$ mysql -u root -p
Enter Password:

mysql> grant all privileges on *.* to bill@localhost identified by 'pass' with grant option;
Trapani answered 25/10, 2012 at 20:17 Comment(8)
This is rather dangerous, if someone hacks your bill@localhost mysql account, he will have infinite access to all the databases of your mysql server.Airscrew
Whehey. I had to put quote around my user 'myusername'@'myhost.static.myip.com' then it worked.Jodi
That works for me but I'm afraid I gave too much privileges for the userTimeout
@CsabaToth you did, reduce the privileges again until your user has what he needs and no more.Slunk
... and what do you do if this gives you "Access denied for user 'root'@'localhost' (using password: YES)" then ?Cymoid
Important that you be careful using WITH GRANT OPTION. From the MySQL manual The GRANT OPTION privilege enables you to give to other users or remove from other users those privileges that you yourself possess.. Don't use it for any user that pulls data for a public source (i.e. a website)Laughable
In place of *.*, we can also insert the name of the database to which this user is authorized.Pankey
Thank you. After 2 days of hard time, I found a solution with your answer.Beetroot
T
108

When you ran

mysql -u bill -p

and got this error

ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

mysqld is expecting you to connect as bill@localhost

Try creating bill@localhost

CREATE USER bill@localhost IDENTIFIED BY 'passpass';
grant all privileges on *.* to bill@localhost with grant option;

If you want to connect remotely, you must specify either the DNS name, the public IP, or 127.0.0.1 using TCP/IP:

mysql -u bill -p [email protected]
mysql -u bill -p -h10.1.2.30
mysql -u bill -p -h127.0.0.1 --protocol=TCP

Once you login, please run this

SELECT USER(),CURRENT_USER();

USER() reports how you attempted to authenticate in MySQL

CURRENT_USER() reports how you were allowed to authenticate in MySQL from the mysql.user table

This will give you a better view of how and why you were allowed to login to mysql. Why is this view important to know? It has to do with the user authentication ordering protocol.

Here is an example: I will create an anonymous user on my desktop MySQL

mysql> select user,host from mysql.user;
+---------+-----------+
| user    | host      |
+---------+-----------+
| lwdba   | %         |
| mywife  | %         |
| lwdba   | 127.0.0.1 |
| root    | 127.0.0.1 |
| lwdba   | localhost |
| root    | localhost |
| vanilla | localhost |
+---------+-----------+
7 rows in set (0.00 sec)

mysql> grant all on *.* to x@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> select user,host from mysql.user;
+---------+-----------+
| user    | host      |
+---------+-----------+
| lwdba   | %         |
| mywife  | %         |
| x       | %         |
| lwdba   | 127.0.0.1 |
| root    | 127.0.0.1 |
| lwdba   | localhost |
| root    | localhost |
| vanilla | localhost |
+---------+-----------+
8 rows in set (0.00 sec)

mysql> update mysql.user set user='' where user='x';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host from mysql.user;
+---------+-----------+
| user    | host      |
+---------+-----------+
|         | %         |
| lwdba   | %         |
| mywife  | %         |
| lwdba   | 127.0.0.1 |
| root    | 127.0.0.1 |
| lwdba   | localhost |
| root    | localhost |
| vanilla | localhost |
+---------+-----------+
8 rows in set (0.00 sec)

mysql>

OK watch me login as anonymous user:

C:\MySQL_5.5.12>mysql -urol -Dtest -h127.0.0.1 --protocol=TCP
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.5.12-log MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user(),current_user();
+---------------+----------------+
| user()        | current_user() |
+---------------+----------------+
| rol@localhost | @%             |
+---------------+----------------+
1 row in set (0.00 sec)

mysql>

Authentication ordering is very strict. It checks from the most specific to the least. I wrote about this authentiation style in the DBA StackExchange.

Don't forget to explicitly call for TCP as the protocol for mysql client when necessary.

Toe answered 26/6, 2012 at 17:40 Comment(6)
'bill'@'localhost' should match 'bill@%', shouldn't it?Lashelllasher
@Yak the sort order is not based solely on mysql.user's user column. MySQL does not do any character matching per se. I wrote about the user authentication ordering protocol in the DBA StackExchange : dba.stackexchange.com/a/10897/877Toe
@YaK This is why I specifically mentioned SELECT USER(),CURRENT_USER();. You almost never see anonymous users appear from these two functions except in really poor setups.Toe
I never assumed the sort order is based solely on mysql.user. In fact, if you read my answer again, you will see that I said (actually, the manual says) the sort order is based on the host column first. You wrote a lot about how to check your current credentials, but I see little information as to why 'bill'@'localhost' cannot login as 'bill'@'%', which is today's question AFAIK. The OP probably has a poor setup, this is why he gets these errors.Lashelllasher
Note that in default "binary" installations, an anonymous user ''@'localhost' exists before you run the "Post-installation" procedure. dev.mysql.com/doc/refman/5.5/en/default-privileges.htmlLashelllasher
Error 1045 is explained here: webyog.com/faq/content/23/18/en/…Wedding
P
44

Super late to this

I tried all of these other answers and ran many different versions of mysql -u root -p but never just ran


mysql -u root -p

And just pressing [ENTER] for the password.


Once I did that it worked. Hope this helps someone.

Phocine answered 3/10, 2016 at 23:46 Comment(0)
L
26

I had a somewhat similar problem - on my first attempt to enter MySQL, as root, it told me access denied. Turns out I forgot to use the sudo...

So, if you fail on root first attempt, try:

sudo mysql -u root -p

and then enter your password, this should work.

Lapboard answered 4/8, 2018 at 11:0 Comment(1)
this worked for meHeartthrob
S
23

When you type mysql -u root -p , you're connecting to the mysql server over a local unix socket.

However the grant you gave, 'bill'@'%' only matches TCP/IP connections curiously enough.

If you want to grant access to the local unix socket, you need to grant privileges to 'bill'@'localhost' , which curiously enough is not the same as 'bill'@'127.0.0.1'

You could also connect using TCP/IP with the mysql command line client, as to match the privileges you already granted, e.g. run mysql -u root -p -h 192.168.1.123 or whichever local IP address your box have.

Sacken answered 26/6, 2012 at 17:39 Comment(4)
"'bill'@'%' only matches TCP/IP connections" This is wrong. Try it on a clean (virgin, out-of-the-box) instance with skip-networkingLashelllasher
@YaK I cannot get 'user'@'%' to function in such a setup, what do you mean was supposed to happen ?Sacken
I could connect as 'bill'@'%' on a v5.0 without networking (hence, through a socket). What version are you using? I will try it on a v5.5.Lashelllasher
Please see my updated answer. I was able to login as 'bill'@'%' through a socket on a v5.5.Lashelllasher
E
23

A related problem in my case was trying to connect using :

mysql -u mike -p mypass

Whitespace IS apparently allowed between the -u #uname# but NOT between the -p and #password#

Therefore needed:

mysql -u mike -pmypass

Otherwise with white-space between -p mypass mysql takes 'mypass' as the db name

Electromagnet answered 20/8, 2014 at 18:19 Comment(3)
or: mysql -u usrname -p -- this stops anyone from seeing the password as it will drop a new line and ask for the password without displaying itKila
great answer @ElectromagnetSpier
this solution is comfortable, nevertheless it's insecure to type password obviously, but no need type grant privileges and define other stuff. So if you mac book is secured – this approach very comfortable, even more I used it for importing data to aws from docker mysql driver.Cleopatracleopatre
V
22

If you forget your password or you want to modify your password.You can follow these steps :

1 :stop your mysql

[root@maomao ~]# service mysqld stop
Stopping MySQL: [ OK ]

2 :use “--skip-grant-tables” to restart mysql

[root@mcy400 ~]# mysqld_safe --skip-grant-tables
[root@cy400 ~]# Starting mysqld daemon with databases from /var/lib/mysql

3 : open a new window and input mysql -u root

[root@cy400 ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.

4 : change the user database

mysql> use mysql
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed

5 : modify your password your new password should be input in "()"

mysql> update user set password=password('root123') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0

6 : flush

mysql> flush privileges;

7: quit

mysql> quit
Bye

8: restart mysql

[root@cy400 ~]# service mysqld restart;
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]

Bingo! You can connect your database with your username and new password:

[root@cy400 ~]# mysql -u root -p <br>
Enter password: admin123 <br>
Welcome to the MySQL monitor.  Commands end with ; or \g. <br>
Your MySQL connection id is 2 <br>
Server version: 5.0.77 Source distribution <br>
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. <br>
mysql> quit <br>
Bye
Vedette answered 26/5, 2016 at 7:48 Comment(3)
This helped me, but on mac if you install with homebrew it's mysql.server stop. In my case, I couldn't update the user column because there isn't one; also couldn't create one because it's in safe mode. For what I'm doing right now I don't care, but I really appreciate this format answer with exact inputs & outputs shown. thanks!Twerp
I'am sorry ,I have never use mac before.So I can not help you.Vedette
Thank you, It was very very usefull to me, you saved my day and night :)Jumble
B
19

Save yourself of a MAJOR headache... Your problem might be that you are missing the quotes around the password. At least that was my case that detoured me for 3 hours.

[client]
user = myusername
password = "mypassword"   # <----------------------- VERY IMPORTANT (quotes)
host = localhost

http://dev.mysql.com/doc/refman/5.7/en/option-files.html

Search for "Here is a typical user option file:" and see the example they state in there. Good luck, and I hope to save someone else some time.

Bridgeport answered 25/7, 2014 at 17:14 Comment(4)
THANK YOU for this. I've wasted half of a day on this and the stupid quotes around password did the trick!Karate
Haha. I wasted pretty much the same when it happened to me. Glad to be of help.Bridgeport
Yep. This happened to me as well in the laravel .env file. No quotes and wasn't working. Thanks. :)Loggins
thanks, bro for this tip, I have also wasted my day on this. working fine with quotes in laravel env.Windrow
P
13

The solution is to delete the anonymous (Any) user!

I also faced the same issue on a server setup by someone else. I normally don't choose to create an anonymous user upon installing MySQL, so hadn't noticed this. Initially I logged in as "root" user and created a couple of "normal" users (aka users with privileges only on dbs with their username as prefix), then logged out, then went on to verify the first normal user. I couldn't log in. Neither via phpMyAdmin, nor via shell. Turns out, the culprit is this "Any" user.

Preconize answered 24/9, 2012 at 15:58 Comment(0)
M
9

The best solution i found for myself is.

my user is sonar and whenever i am trying to connect to my database from external or other machine i am getting error as

ERROR 1045 (28000): Access denied for user 'sonar'@'localhost' (using password: YES)

Also as i am trying this from another machine and through Jenkins job my URL for accessing is

alm-lt-test.xyz.com

if you want to connect remotely you can specify it with different ways as follows:

mysql -u sonar -p -halm-lt-test.xyz.com
mysql -u sonar -p -h101.33.65.94
mysql -u sonar -p -h127.0.0.1 --protocol=TCP
mysql -u sonar -p -h172.27.59.54 --protocol=TCP

To access this with URL you just have to execute the following query.

GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'alm-lt-test.xyz.com' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'127.0.0.1' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'172.27.59.54' IDENTIFIED BY 'sonar';
Mandamandaean answered 18/9, 2014 at 14:33 Comment(0)
H
9

It's a difference between:

CREATE USER 'bill'@'%' IDENTIFIED BY 'passpass';

and

CREATE USER 'bill'@'localhost' IDENTIFIED BY 'passpass';

Check it:

mysql> select user,host from mysql.user;
+---------------+----------------------------+
| user          | host                       |
+---------------+----------------------------+
| bill          | %                          | <=== created by first
| root          | 127.0.0.1                  |
| root          | ::1                        |
| root          | localhost                  |
| bill          | localhost                  | <=== created by second
+---------------+----------------------------+

The command

mysql -u bill -p

access implicit to 'bill'@'localhost' and NOT to 'bill'@'%'.

There are no permissions for 'bill'@'localhost'

you get the error:

ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

solving the problem:

CREATE USER 'bill'@'localhost' IDENTIFIED BY 'passpass';

grant all privileges on . to 'bill'@'localhost' with grant option;
Hogg answered 1/12, 2015 at 17:26 Comment(2)
The grant statement returned an error. I needed to specify a database like so: grant all privileges on newdb.* to ....Fugger
grant all privileges on *.* to 'bill'@'localhost' with grant option; Correction to queryDorothi
R
5

Okay, I'm not sure but probably this is my.cnf file inside mysql installation directory is the culprit. Comment out this line and the problem might be resolved.

bind-address = 127.0.0.1
Ronda answered 25/4, 2012 at 9:37 Comment(2)
For more details, you can view this as well wiki.bitnami.org/Components/MySQLRonda
I am quite sure that this is not the issue. If it would be, MySQL refuses connections from any host other than 127.0.0.1, and you will not get a SQL 'Access Denied' error.Vevina
W
5

Not sure if anyone else will find this helpful, but I encountered the same error and searched all over for any anonymous users...and there weren't any. The problem ended up being that the user account was set to "Require SSL" - which I found in PHPMyAdmin by going to User Accounts and clicking on Edit Privileges for the user. As soon as I unchecked this option, everything worked as expected!

Workbook answered 10/3, 2016 at 2:53 Comment(0)
I
5

This also happens when your password contains some special characters like @,$,etc. To avoid this situation you can wrap password in single quotes:

$ mysql -usomeuser -p's0mep@$$w0Rd'

Or instead don't use password while entering. Leave it blank and then type it when terminal asks. This is the recommended way.

$ mysql -usomeuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 191
Server version: 5.5.46-0ubuntu0.14.04.2 (Ubuntu)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
Isochor answered 8/7, 2016 at 6:44 Comment(3)
This can indeed be an issue (as it was for me). All my passwords for any of my accounts are generated using pwgen. Today I generated a new one for a MySQL database and its user. Unfortunately the password contained a backslash "\" which I didn't identify as the errors origin (I didn't even think of that). So, I searched for hours a solution. After setting the password to "123" in despair the login finally worked. … Users should be aware of that some special chars may cause problems since MySQL doesn't show any warnings on using passwords such as "daiy4ha4in7chooshuiphie\Th*aew", for example.Flor
Was about to post this as an answer if it hadn't shown up yet, it was simply too far down the list for me to notice (so I'm bumping it up +1)Matronage
Thanks for this. I've encountered it before and just couldn't remember! Got tied up with the difference between '127.0.0.1' and 'localhost'.Jayme
S
5

Update: On v8.0.15 (maybe this version) the PASSWORD() function does not work.

You have to:

  1. Make sure you have Stopped MySQL first.
  2. Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
  3. Login: mysql -u root
  4. mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';
  5. mysql> FLUSH PRIVILEGES;
  6. mysql> exit;
  7. Login again: mysql -u root
  8. mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
Singles answered 12/9, 2019 at 13:59 Comment(0)
Z
5

For me root had a default password

i changed the password using ALTER USER 'root'@'localhost' IDENTIFIED BY 'new Password'; and it worked

Zoomorphism answered 10/12, 2020 at 21:28 Comment(4)
I got an error ERROR 1396 (HY000): Operation ALTER USER failed for 'user'@'localhost'Coronado
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)Ashcraft
I updated the user I created privately with the same password. problem fixed for me too. mysql 8.0.34Calesta
i set root and user password same. after changing new user password issue is solved.Platte
V
4

I hope you have not done more damage by also deleting the debian-sys-maint user in mysql

Have your mysql daemon running the normal way. Start your mysql client as shown below

mysql -u debian-sys-maint -p

In another terminal, cat the file /etc/mysql/debian.cnf. That file contains a password; paste that password when prompted for it.

http://ubuntuforums.org/showthread.php?t=1836919

Vesperal answered 25/9, 2013 at 12:27 Comment(0)
S
4

Just wanted to let you know a unusual circumstance I received the same error. Perhaps this helps someone in the future.

I had developed a few base views, created at the development site and transferred them to the production-site. Later that week I changed a PHP script and all of a sudden errors came up that Access was denied for user 'local-web-user'@'localhost'. The datasource object had not changed, so I concentrated on the database user in MySQL, worrying in the meantime someone hacked my website. Luckily the rest of the site seemed unharmed.

It later turned out that the views were the culprit(s). Our object transfers are done using another (and remote: admin@ip-address) user than the local website user. So the views were created with 'admin'@'ip-address' as the definer. The view creation SECURITY default is

SQL SECURITY DEFINER

When local-web-user tries to use the view it stumbles on the lacking privileges of the definer to use the tables. Once security was changed to:

SQL SECURITY INVOKER

the issue was resolved. The actual problem was completely different than anticipated based on the error message.

Spoil answered 6/10, 2015 at 13:7 Comment(1)
This ended up being the source of my problem as well - a view with a missing "definer". One quick way to find out if this is what's happening to you is to try querying the same table or view as root - if you do, the error message changes to the much more descriptive "ERROR 1449 (HY000): The user specified as a definer does not exist".Menstruation
B
4

For me, this problem was caused by a new feature of MySQL 5.7.2: user entries are ignored if their plugin field is empty.

Set it to e.g. mysql_native_password to reenable them:

UPDATE user SET plugin='mysql_native_password' WHERE user='foo';
FLUSH PRIVILEGES;

See the release notes for MySQL 5.7.2, under «Authentication Notes».

For some reason (maybe because my pre-4.1 password hashes were removed), the mysql_upgrade script didn't set a default plugin value.

I found out by noticing the following warning message in /var/log/mysql/error.log:

[Warning] User entry 'foo'@'%' has an empty plugin value. The user will be ignored and no one can login with this user anymore.

I post this answer here to maybe save someone from using the same ridiculous amount of time on this as I did.

Bailable answered 18/9, 2016 at 23:43 Comment(0)
E
4

Debugging Summary

  • Check for typo error: username or password.
  • Check the host name and compare it with mysql.user table host name.
  • Check user exists or not.
  • Check whether host contains IP address or host name.

There is a great chance that, you might have encountered this issue multiple times in your work. This issue occurred to me most of times due to the incorrectly entering user name or password. Though this is one of the reasons, there are other many chances you might get this issue. Sometimes, it looks very similar, but when you dig deeper, you will realize multiple factors contributing to this error. This post will explain in detail, most of the common reasons and work around to fix this issue.

Possible reasons:

  • Case 1: Typo error: username or password.

This is the most common reason for this error. If you entered the username or password wrongly, surely you will get this error.

Solution:

Solution for this type of error is very simple. Just enter the correct username and password. This error will be resolved. In case if you forget the password you can reset the username/password. If you forget the password for admin / root account, there are many ways to reset / recapture the root password. I will publish another post on how to reset the root password in-case if you forget root password.

  • Case 2: Accessing from wrong host.

MySQL provides host based restriction for user access as a security features. In our production environment, we used to restrict the access request only to the Application servers. This feature is really helpful in many production scenarios.

Solution:

When you face this type of issue, first check whether your host is allowed or not by checking the mysql.user table. If it is not defined, you can update or insert new record to mysql.user table. Generally, accessing as a root user from remote machine is disabled and it is not a best practice, because of security concerns. If you have requirements to access your server from multiple machines, give access only to those machines. It is better not to use wildcards (%) and gives universal accesses. Let me update the mysql.user table, now the demouser can access MySQL server from any host.

  • Case 3: User does not exists on the server.

This type of error occurs when the user, which you are trying to access not exist on the MySQL server.

Solutions:

When you face this type of issue, just check whether the user is exists in mysql.user table or not. If the record not exists, user cannot access. If there is a requirement for that user to access, create a new user with that username.

  • Case 4: Mix of numeric and name based hosts.

Important points

  • It is not advisable to use wildcards while defining user host, try to use the exact host name.

  • Disable root login from remote machine.

  • Use proxy user concept.

There are few other concepts related with this topic and getting into details of those topics is very different scope of this article. We will look into the following related topics in the upcoming articles.

  • What to do, if you forgot root password in of MySQL server.
  • MySQL Access privilege issues and user related tables.
  • MySQL security features with best practices.

I hope this post will help for you to fix the MySQL Error Code 1045 Access denied for user in MySQL.

Eurythmics answered 14/11, 2017 at 7:26 Comment(1)
This was it :) I just had a wrong pw. In my case, I thought I would be able to copy a pw out of the db management system from the disguised black dots, very much wondering about this, and it turned out that I had another pw still in the clipboard instead. Well... Good that one cannot just copy a pw from a db manager :).Borries
B
4

I discovered yet another case that appears on the surface to be an edge case; I can export to the file system, via SELECT INTO .. OUTFILE as root, but not as regular user. While this may be a matter of permissions, I've looked at that, and see nothing especially obvious. All I can say is that executing the query as a regular user who has all permissions on the data base in question returns the access denied error that led me to this topic. When I found the transcript of a successful use of SELECT INTO … OUTFILE in an old project, I noticed that I was logged in as root. Sure enough, when I logged in as root, the query ran as expected.

Birgit answered 27/12, 2018 at 7:12 Comment(0)
P
2

This may apply to very few people, but here goes. Don't use an exclamation ! in your password.

I did and got the above error using MariaDB. When I simplified it to just numbers and letters it worked. Other characters such as @ and $ work fine - I used those characters in a different user on the same instance.

The fifth response at this address led me to my fix.

Pilatus answered 12/2, 2019 at 10:23 Comment(1)
Yes, this is true, my password was a mix of numbers, letters, and these chars -::)(&/&\)/ and guess what, the backslash was causing me the issue!Randalrandall
I
2

I had similar problems because my password contains ";" char breaking my password when I creates it at first moment. Caution with this if can help you.

Immunogenetics answered 16/9, 2019 at 10:58 Comment(1)
Same as @Chiwda issue! sometimes it's just the password itselfRandalrandall
L
2

Nowadays! Solution for:

MySQL ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES);

Wampserver 3.2.0 new installation or upgrading

Probably xamp using mariaDB as default is well.

Wamp server comes with mariaDB and mysql, and installing mariaDB as default on 3306 port and mysql on 3307, port sometimes 3308.

Connect to mysql!

On installation, it asks to use mariaDB or MySql. But mariaDB is checked as default and you can't change it; check mysql option and install.

When installation is done, both will be running mariaDB on default port 3306 and mysql on another port 3307 or 3308.

Right click on the wampserver icon where it's running. It should be on the right bottom corner; go to tools and see your correct mysql running port.

And include it in your database connection as follows:

$host = 'localhost';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$port = '3308';//Port

$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset"; //Add in connection
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

Note : I am using pdo.

See here for more : https://sourceforge.net/projects/wampserver/

Laywoman answered 30/1, 2020 at 14:16 Comment(2)
Please do not use backticks for emphasis. They should only be used for code. Words like MySQL or Wampserver are not code.Artistry
Right Click wampmanager->Tools->Invert Default DBMS will allow you to make either MySQL or mariaDB the default (the one using port 3306) This way you dont have to add stuff to your code that wont work when you move it live.Amain
R
2
sudo -i 
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

access mysql service without login ( can see beacue in shell mysql> )

Radiance answered 19/5, 2022 at 18:10 Comment(0)
L
1

On Windows, here's how to resolve:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

  1. Uninstall mysql from the control panel
  2. Delete the MySql folder from C:\Program Files,C:\Program Files (x86) and C:\ProgramData
  3. Install mysql
Longeron answered 4/3, 2018 at 8:55 Comment(1)
Tried all other answers, finally resolved to try this and it worked!Hagood
M
1

For me, It was not specifying the -p parameter when entering mysql.

mysql -p

I had no problem, but it was wrong to invoke mysql without a password.

Millican answered 9/1, 2021 at 17:21 Comment(0)
G
1

So for me the issue was related to the ports i'm mapping.

3306 => 3306 did not work(because I had a local mysql running)

3307 => 3306 works!

This is in the context of establishing as ssh tunnel:

ssh -N -L 3307:rdsDns:3306 ec2User@ec2Dns -i key.pem -v

3307 is the local port, and 3306 is the remote port.

Global answered 10/4, 2021 at 12:51 Comment(0)
C
0

When you run mysql -u bill -p, localhost is resolved to your ip, since it is 127.0.0.1 and in your /etc/hosts file, as default 127.0.0.1 localhost exists. So, mysql interprets you as bill@localhost which is not granted with bill@'%' . This is why there are 2 different records for root user in result of select host, user from mysql.user; query.

There are two ways to handle this issue.

One is specifying an ip which is not reversely resolved by /etc/hosts file when you try to login. For example, the ip of server is 10.0.0.2. When you run the command mysql -u bill -p -h 10.0.0.2, you will be able to login. If you type select user();, you will get [email protected]. Of course, any domain name should not be resolved to this ip in your /etc/hosts file.

Secondly, you need grant access for this specific domain name. For bill@localhost, you should call command grant all privileges on *.* to bill@localhost identified by 'billpass'; . In this case, you will be able to login with command mysql -u bill -p. Once logined, select user(); command returns bill@localhost.

But this is only for that you try to login a mysql server in the same host. From remote hosts, mysql behaves expectedly, '%' will grant you to login.

Concise answered 17/1, 2014 at 9:9 Comment(0)
J
0

I resolved this by deleting the old buggy user 'bill' entries (this is the important part: both from mysql.user and mysql.db), then created the same user as sad before:

FLUSH PRIVILEGES;
CREATE USER bill@localhost IDENTIFIED BY 'passpass';
grant all privileges on *.* to bill@localhost with grant option;
FLUSH PRIVILEGES;

Worked, user is connecting. Now I'll remove some previlegies from it :)

Jacklyn answered 3/5, 2014 at 4:6 Comment(0)
W
0

OS: windows

My error message is:

MySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

My reason is that I didn't open cmd with administrator permissions.

So my solution was to open cmd as administrator, then it worked.

Wrath answered 25/10, 2018 at 2:44 Comment(1)
This is only relevant to windows.Flapper
B
0

This may also happen if MySQL is running on a case-insensitive OS, such as Windows.

e.g. I found that attempting to connect to a database using these credentials failed:

mysql> grant select on databaseV105.* to 'specialuser' identified by 's3curepa5wrd';

$ mysql -specialuser' -p's3curepa5wrd' -h10.61.130.89 databaseV105

ERROR 1045 (28000): Access denied for user 'specialuser'@'10.0.1.113' (using password: YES)

But, this succeeded:

mysql> grant select on databasev105.* to 'specialuser' identified by 's3curepa5wrd';

$ mysql -specialuser' -h10.300.300.400 databaseV105 -p

Enter password:

Bespread answered 4/7, 2019 at 14:14 Comment(0)
C
0

If your dbname, username, password, etc strings lengths exceed values outlined at https://dev.mysql.com/doc/refman/5.7/en/grant-tables.html#grant-tables-scope-column-properties , you might also fail to log in, as this was in my case.

Carmacarmack answered 20/1, 2020 at 10:11 Comment(0)
A
0

in my case the server hosting my code was sending '@blablabla.amazonaws.com' at the ed of the username when trying to log into MySQL. So I had to add %.amazonws.com into my hosting's user config (dreamhost) and it went through

Arcadian answered 22/10, 2022 at 13:51 Comment(0)
C
0

The solution for this problem can be very simple!

  1. In my case I had exported my database (mysqldump) into a dumpfile.

  2. Then I copied the file, to the destination server.

  3. On this destination server I created the target database

  4. Then I ran the command to import the copied dumpfile to the new target database on my destination server.

==> ERROR! At this point I just got the error that you got too!

Now, think about it! The user you use, what GRANT's does he have on the source database?

An what GRANT's does he have on the new target database? I think, you see allready the problem and also the solution!

==> SOLUTION!

  1. So I went into my source database and exported all the grants of the user I use.

  2. After that I went to my target database and imported the exported GRANT's.

A voila! After that the import of the source database on the target server into the new target database work without any problems!

It's a lot of text, sorry! but maybe it will help someone solve a problem :-)

Christean answered 13/1, 2023 at 10:35 Comment(0)
N
0

if you have freshly installed the mysql-server on you linux machine then here is the solution

try running this

sudo mysql

then create user for login to database , not alsways using above command

Nisus answered 21/6, 2023 at 10:14 Comment(0)
C
0

for me putting "password" on the passoword solved the problem

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=realxcqv_ppcLeads
DB_USERNAME=realxcqv_UserPPcLeads
DB_PASSWORD="PPC@199#@199#PPCLeads@199#@199#"
Compulsion answered 30/7, 2023 at 9:28 Comment(0)
S
-1

MySQL account names consist of a user name and a host name, The name 'localhost' in host name indicates the local host also You can use the wildcard characters “%” and “_” in host name or IP address values. These have the same meaning as for pattern-matching operations performed with the LIKE operator. For example, a host value of '%' matches any host name, whereas a value of '%.mysql.com' matches any host in the mysql.com domain. '192.168.1.%' matches any host in the 192.168.1 class C network.

Above was just introduction:

actually both users 'bill'@'localhost' and 'bill'@'%' are different MySQL accounts, hence both should use their own authentication details like password.

For more information refer http://dev.mysql.com/doc/refman//5.5/en/account-names.html

Seize answered 27/6, 2012 at 14:15 Comment(0)
N
-1

Be sure there are no other SQL instances running that are using the localhost post. In our case another instance was running on the localhost that conflicted with the login. Turning it off solved this problem.

Nannie answered 11/2, 2014 at 13:58 Comment(0)
E
-1

Also the problem can occur if you are using old version of the MySQL UI (like SQLYoug) that generates passwords with wrong hash.

Creating user with SQL script will fix the problem.

Effuse answered 5/5, 2014 at 15:34 Comment(0)
S
-1

I had the same problem, but in my case the solution was solved by the comment by eggyal. I had an anonymous user as well, but removing it didn't solve the problem. The 'FLUSH PRIVILEGES' command worked though.

The surprising thing to me about this was that I created the user with MySQL Workbench and I would have expected that to perform all of the necessary functions to complete the task.

Systematism answered 29/8, 2014 at 11:56 Comment(0)
D
-1

For Mac users if they still have the problem (as was my case), I found this solution worked for me: MySQL command line '-bash command not found'

To use command i.e. mysql on macbook terminal you need to export path using:

export PATH=$PATH:/usr/local/mysql/bin/

Considering default installation, use following command to get mysql prompt as root user:

mysql -u root

Otherwise you are using wrong root password.

Ref: Setting the MySQL root user password on OSX

Duchamp answered 28/2, 2016 at 16:35 Comment(0)
S
-1

The percent sign means all ip's so localhost is superfluous ... There is no need of the second record with the localhost .

Actually there is, 'localhost' is special in mysql, it means a connection over a unix socket (or named pipes on windows I believe) as opposed to a TCP/IP socket. using % as the host does not include 'localhost'

MySQL user accounts have two components: a user name and a host name. The user name identifies the user, and the host name specifies what hosts that user can connect from. The user name and host name are combined to create a user account:

'<user_name>'@'<host_name>' You can specify a specific IP address or address range for host name, or use the percent character ("%") to enable that user to log in from any host.

Note that user accounts are defined by both the user name and the host name. For example, 'root'@'%' is a different user account than 'root'@'localhost'.

Shaker answered 26/8, 2016 at 17:8 Comment(0)
S
-1

I encountered the same error. The setup that wasn't working is as follows:

define("HOSTNAME", "localhost");
define("HOSTUSER", "van");
define("HOSTPASS", "helsing");
define("DBNAME", "crossbow");
$connection = mysqli_connect(HOSTNAME, HOSTUSER, HOSTPASS);

The edited setup below is the one that got it working. Notice the difference?

define('HOSTNAME', 'localhost');
define('HOSTUSER', 'van');
define('HOSTPASS', 'helsing');
define('DBNAME', 'crossbow');
$connection = mysqli_connect(HOSTNAME, HOSTUSER, HOSTPASS);

The difference is the double quotes. They seem to be quite significant in PHP as opposed to Java and they have an impact when it comes to escaping characters, setting up URLs and now, passing parameters to a function. They're prettier (I know) but always use single quotes as much as possible then double quotes can be nested within those if necessary.

This error came up when I tested my application on a Linux box as opposed to a Windows environment.

Strain answered 11/8, 2018 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.