Jeśli masz ten sam problem w MySql 5.7.+ :
Access denied for user 'root'@'localhost'
to dlatego, że MySql 5.7 domyślnie pozwala na łączenie się z socketem, co oznacza, że łączysz się z sudo mysql
. Jeśli uruchomisz sql :
SELECT user,authentication_string,plugin,host FROM mysql.user;
wtedy to zobaczysz :
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *497C3D7B50479A812B89CD12EC3EDA6C0CB686F0 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
Aby zezwolić na połączenie z rootem i hasłem, zaktualizuj wartości w tabeli poleceniem :
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password';
FLUSH PRIVILEGES;
Następnie uruchom ponownie polecenie select, a zobaczysz, że się zmieniło:
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | *2F2377C1BC54BE827DC8A4EE051CBD57490FB8C6 | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *497C3D7B50479A812B89CD12EC3EDA6C0CB686F0 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
I to wszystko. Możesz uruchomić ten proces po uruchomieniu i ukończeniu sudo mysql_secure_installation
polecenie.
W przypadku mariadb użyj
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('manager');
aby ustawić hasło.Więcej na https://mariadb.com/kb/en/set- hasło/