MySQL ERROR 1045 (28000): Accesso negato per l'utente 'bill'@'localhost' (usando la password: YES)

Prima di tutto lasciatemi dire che sono passato attraverso molte domande suggerite e non ho trovato alcuna risposta pertinente. Ecco cosa sto facendo.

Sono collegato alla mia istanza Amazon EC2. Posso accedere con MySQL root con questo comando:

mysql -u root -p

Poi ho creato un nuovo conto utente con host %

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

Ho concesso tutti i privilegi all'utente Bill:

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

Poi esco dall'utente root e provo ad accedere con bill:

mysql -u bill -p

inserito la password corretta e ottengo questo errore:

ERROR 1045 (28000): Accesso negato per l'utente 'bill'@'localhost' (usando la password: YES)

Quando hai corso

mysql -u bill -p

e hai ottenuto questo errore

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

mysqld si aspetta che tu ti connetta come bill@localhost.

Provare a creare bill@localhost.

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

Se vuoi connetterti da remoto, devi specificare il nome DNS, l'IP pubblico o 127.0.0.1 usando il TCP/IP:

mysql -u bill -p -hmydb@mydomain.com
mysql -u bill -p -h10.1.2.30
mysql -u bill -p -h127.0.0.1 --protocol=TCP

Una volta effettuato il login, esegui questo

SELECT USER(),CURRENT_USER();

USER() riporta come hai tentato di autenticarti in MySQL

CURRENT_USER() riporta come sei stato autorizzato ad autenticarti in MySQL dalla tabella mysql.user

Questo vi darà una visione migliore di come e perché vi è stato permesso di autenticarvi in mysql. Perché questa vista è importante da sapere? Ha a che fare con il protocollo di ordinazione dell'autenticazione utente.

Ecco un esempio: Creerò un utente anonimo sul mio 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 guardami mentre faccio il login come utente anonimo:

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>

L'ordine di autenticazione è molto rigoroso. Controlla dal più specifico al meno. Ho scritto su questo stile di autenticazione nel DBA StackExchange.

Non dimenticate di chiamare esplicitamente il TCP come protocollo per il client mysql quando necessario.

Commentari (7)

Quando digitate mysql -u root -p , vi state connettendo al server mysql su un socket unix locale.

Tuttavia il grant che hai dato, 'bill'@'%' corrisponde solo alle connessioni TCP/IP, curiosamente.

Se vuoi concedere l'accesso al socket unix locale, devi concedere i privilegi a 'bill'@'localhost' , che curiosamente non è lo stesso di 'bill'@'127.0.0.1'

Potresti anche connetterti usando TCP/IP con il client a riga di comando mysql, in modo che corrisponda ai privilegi che hai già concesso, ad esempio esegui mysql -u root -p -h 192.168.1.123 o qualsiasi indirizzo IP locale che la tua scatola ha.

Commentari (4)

Ok, non sono sicuro, ma probabilmente il colpevole è il file my.cnf nella directory di installazione di mysql. Commenta questa linea e il problema potrebbe essere risolto.

bind-address = 127.0.0.1
Commentari (2)