Hvis du har det samme problem i MySql 5.7.+ :
Access denied for user 'root'@'localhost'
det er fordi MySql 5.7 som standard tillader at forbinde med socket, hvilket betyder at du bare forbinder med sudo mysql
. Hvis du kører sql :
SELECT user,authentication_string,plugin,host FROM mysql.user;
så vil du se det :
+------------------+-------------------------------------------+-----------------------+-----------+
| 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)
For at tillade forbindelse med root og adgangskode skal du opdatere værdierne i tabellen med kommandoen :
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password';
FLUSH PRIVILEGES;
Kør derefter select-kommandoen igen, og du vil se, at den er ændret :
+------------------+-------------------------------------------+-----------------------+-----------+
| 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)
Og det er det. Du kan køre denne proces efter at have kørt og gennemført sudo mysql_secure_installation
kommando.
For mariadb, brug
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('manager');
for at indstille adgangskode.Mere på https://mariadb.com/kb/en/set- adgangskode/