mysql So beheben Sie den Zugriff verweigert für Benutzer 'root' @ 'localhost'

Bevor ich etwas vermassle, wenn ich mich mit $ mysql -u root -p anmelde und Datenbanken zeige:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| game_data          |
| test               |
+--------------------+

Dann habe ich versucht, einen neuen Benutzer zu erstellen und festgestellt, dass etwas mit den BERECHTIGUNGEN nicht stimmt.

Also habe ich die neuen Benutzer gelöscht, und ich denke, ich habe versehentlich die 'root' und 'Admin' entfernt.

Dann versuche ich erneut, 'root' zu erstellen, erhalte jedoch den Fehler Access denied, wenn alle Berechtigungen erteilt werden.

mysql> CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';
mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'password' with grant option;
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Wenn ich mich erneut mit $ mysql -u root -p bei MySQL anmelde und zeige Datenbanken,

+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+

Alle anderen Datenbanken sind weg.

Wie behebe ich MySQL jetzt?

Ich kann die Datenbank 'mysql' nicht finden, kann keine Datenbank erstellen, Benutzer erstellen, alles, was ich versuche, wird eine Fehlermeldung erhalten.

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

Sollte ich MySQL mit MacPorts neu installieren? Wenn ich neu installiere, verliere ich die Datenbank game_data, oder?

Author: ReinstateMonica3167040, 2013-06-03

7 answers

Folgen Sie den folgenden Schritten.

  1. Starten Sie die MySQL Server Instanz oder den Daemon mit der Option --skip-grant-tables (Sicherheitseinstellung).

    $ mysqld --skip-grant-tables
    
  2. Führen Sie diese Anweisungen aus.

    $ mysql -u root mysql
    $mysql> UPDATE user SET Password=PASSWORD('my_password') where USER='root';
    $mysql> FLUSH PRIVILEGES;
    

Wenn Sie mit dem obigen unbekannten Feldpasswortfehler konfrontiert sind, verwenden Sie:

update user set authentication_string=password('my_password') where user='root';
  1. Starten Sie schließlich die Instanz/den Daemon ohne die Option --skip-grant-tables neu.

    $ /etc/init.d/mysql restart
    

Du solltest dich jetzt mit deinem neuen Passwort verbinden können.

$ mysql -u root -p

Eingeben passwort: my_password

Update für MySQL "Nicht in der Lage zu sperren ibdata1" error

sudo mv /usr/local/mysql/data/ibdata1 /usr/local/mysql/data/ibdata1.bak
sudo mv /usr/local/mysql/data/ib_logfile0 /usr/local/mysql/data/ib_logfile0.bak
sudo mv /usr/local/mysql/data/ib_logfile1 /usr/local/mysql/data/ib_logfile1.bak
sudo cp -a /usr/local/mysql/data/ibdata1.bak /usr/local/mysql/data/ibdata1
sudo cp -a /usr/local/mysql/data/ib_logfile0.bak /usr/local/mysql/data/ib_logfile0
sudo cp -a /usr/local/mysql/data/ib_logfile1.bak /usr/local/mysql/data/ib_logfile1
sudo /etc/init.d/mysql restart
 146
Author: Yogus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2016-01-14 08:16:58

Nichts davon war hilfreich für mich. Ich fand, dass ich die Plugin-Methode löschen musste. In 5.6 könnte ich tun:

sudo mysql -u root
use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;

In 5.7 fand ich, dass ich Folgendes brauchte:

sudo mysql -u root
use mysql;
[mysql] update user set plugin='mysql_native_password' where User='root';
[mysql] flush privileges;

Laut den Dokumenten sollte das Plugin, wenn es auf eine leere Zeichenfolge festgelegt ist, standardmäßig mysql_native_password, kann jedoch durch einen leeren Passwort-Hash verwirrt werden. Für weitere Nuancen können Sie die Dokumentation hier lesen: https://dev.mysql.com/doc/refman/5.7/en/native-authentication-plugin.html

 89
Author: Mario Olivio Flores,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2018-04-19 10:10:16

Stellen Sie außerdem sicher, dass der Datensatz in der Tabelle user ein leeres Feld plugin (es kann beispielsweise "unix_socket").

Seit Version 5.5.7 unterstützt MySQL verschiedene Auth-Plugins https://dev.mysql.com/doc/refman/5.6/en/authentication-plugins.html

Wenn Sie also ein nicht leeres plugin Feld haben, wird das Passwort ignoriert und es wird eine Warnung im MySQL-Fehlerprotokoll angezeigt (für mich ist es /var/log/mysql/error.log):

[Warning] 'user' entry 'root@localhost' has both a password and an authentication plugin specified. The password will be ignored.

 9
Author: d9k,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2016-04-28 21:27:58
grep 'temporary password' /var/log/mysqld.log
Sort date (newest date)

Sie können so etwas sehen;

[root@SERVER ~]# grep 'temporary password' /var/log/mysqld.log
2016-01-16T18:07:29.688164Z 1 [Note] A temporary password is generated for root@localhost: O,k5.marHfFu
2016-01-22T13:14:17.974391Z 1 [Note] A temporary password is generated for root@localhost: b5nvIu!jh6ql
2016-01-22T15:35:48.496812Z 1 [Note] A temporary password is generated for root@localhost: (B*=T!uWJ7ws
2016-01-22T15:52:21.088610Z 1 [Note] A temporary password is generated for root@localhost: %tJXK7sytMJV
2016-01-22T16:24:41.384205Z 1 [Note] A temporary password is generated for root@localhost: lslQDvgwr3/S
2016-01-22T22:11:24.772275Z 1 [Note] A temporary password is generated for root@localhost: S4u+J,Rce_0t
[root@SERVER ~]# mysql_secure_installation

Sicherung der MySQL-Serverbereitstellung.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password:

Wenn Sie es sehen, sagt

... Failed! Error: Your password does not satisfy the current policy requirements
That means your password needs to have a character such as ! . # - etc...
mix characters well, upper case, lower case, ! . , # etc...

New password: 

Re-enter new password: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done! 
[root@SERVER ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.10 MySQL Community Server (GPL)

Sehen Sie sich die letzten 10 Minuten von dieses Video an, es lehrt Sie, wie Sie es tun.

 7
Author: Tadeh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-02-22 13:01:30

Versuche es:

mysql --no-defaults --force --user=root --host=localhost --database=mysql 
UPDATE user SET Password=PASSWORD('NEWPASSWORD') where USER='root';
FLUSH PRIVILEGES;
 3
Author: Gustavo Duarte,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2014-02-21 17:16:49

In meinem Fall hatte ich eine Datenbankbeschädigung, nach dem Neustart von MySQL unter Debian war die Root-Anmeldung ohne Passwort. Die Lösung war dies:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';

Einige andere Antworten haben auch das native_password-Plugin erwähnt, aber so können Sie es ohne kompliziertes Herumfummeln tun. So soll es geändert werden.

 3
Author: John,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-11-10 20:13:46

Die offizielle Dokumentation schlug etwas anderes vor:

Erstellen Sie zunächst eine SQL-Datei, in der das neue Kennwort wie folgt festgelegt wird:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Dann starte mysqld mit dem Parameter --init-file, zum Beispiel:

mysqld --init-file=/home/me/mysql-init &

Vergessen Sie nicht, den neu gestarteten mysqld-Server zu beenden, nachdem Sie überprüft haben, ob das neue Kennwort festgelegt wurde.

 1
Author: sebix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2020-04-17 12:46:11