如何重置mysql root密码?

发布于 2024-10-03 19:55:54 字数 397 浏览 8 评论 0原文

我的 phpmyadmin 有一个小问题,事实上我不小心删除了多个用户帐户。 因为不可能在没有错误的情况下连接:

# 1045 - Access denied for user 'root' @ 'localhost' (using password: NO)

我之前在网上搜索过一点,甚至技术:

UPDATE mysql.user SET Password = PASSWORD ('') WHERE User = 'root';
FLUSH PRIVILEGES;

不起作用,或者我不明白它是如何工作的。

我使用的是 FreeBSD 8.1,我的 PhpMyadmin 版本是 2.11。

预先感谢您的答复。

I have a little problem with my phpmyadmin, in fact I accidentally delete multiple user accounts.
Since it is impossible to connect without the error:

# 1045 - Access denied for user 'root' @ 'localhost' (using password: NO)

I have search a little on the net before, and even the technic:

UPDATE mysql.user SET Password = PASSWORD ('') WHERE User = 'root';
FLUSH PRIVILEGES;

does not work, or I didn't understood how it worked.

I'm on FreeBSD 8.1, my version of PhpMyadmin is 2.11.

Thank you in advance for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

蓝眸 2024-10-10 19:55:54

我在这里总结了我的解决方案: http://snippets.dzone.com/posts/show/13267

sudo stop mysql
sudo mysqld --skip-grant-tables --skip-networking

mysql
mysql> update mysql.user set password = password('your_new_password') where user = 'root';
mysql> flush privileges;
mysql> exit;
sudo mysqladmin shutdown
sudo start mysql

I summarised my solution here: http://snippets.dzone.com/posts/show/13267

sudo stop mysql
sudo mysqld --skip-grant-tables --skip-networking

mysql
mysql> update mysql.user set password = password('your_new_password') where user = 'root';
mysql> flush privileges;
mysql> exit;
sudo mysqladmin shutdown
sudo start mysql
孤千羽 2024-10-10 19:55:54

对于 mysql 5.7.16 或更高版本,我发现 this (来自 mysql.com)作为可行的解决方案。以下是与以前的旧版本相比的一些不同之处

1) 我使用以下命令在安全模式下运行 mysql,根据其他一些参考文献,这些参考资料实际上对我来说效果很好(mysql 5.7.16 ubuntu 16.04)。

mysqld_safe --skip-grant-tables --skip-networking

2) 下面的更新 stmt 不适用于更高版本(即 5.7 及更高版本),

-- NOT Working for 5.7 and later
UPDATE mysql.user SET Password = PASSWORD ('') WHERE User = 'root';
FLUSH PRIVILEGES;

请使用下面的 5.7.6 及更高版本

UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

或用户下面的 5.7.5 或更早版本。

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

For mysql 5.7.16 or later I found this (from mysql.com) to be the working solution. Below are some points which are different compared to previous older versions

1) I used the below command to run mysql in safe mode as per some other references which actually worked fine for me (mysql 5.7.16 ubuntu 16.04).

mysqld_safe --skip-grant-tables --skip-networking

2) The below update stmt is NOT working for later version (ie. 5.7 and above)

-- NOT Working for 5.7 and later
UPDATE mysql.user SET Password = PASSWORD ('') WHERE User = 'root';
FLUSH PRIVILEGES;

INSTEAD use below for 5.7.6 and later

UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

OR user below for 5.7.5 or earlier versions.

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
甜味超标? 2024-10-10 19:55:54

我正在使用 MySQL Server 8,这就是我解决这个问题的方法。

创建一个文件并相应命名。我将其命名为reset.txt。将以下内容放入文件中,但更改 MyNewPass。这将是您的新 SQL 密码

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

停止我的 SQL 服务器(转到服务并搜索 MYSQL 并右键单击它并停止)

现在在 My SQLserver 目录的 bin 文件夹中打开一个 cmd。就我而言是

C:\Program Files\MySQL\MySQL Server 8.0\bin

执行以下命令。

mysqld.exe --defaults-file="C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" --init-file="C:\reset.txt' 

注意:我的reset.txt文件位于c盘中。给出正确的路径。检查 my.ini 文件是否在目录中。就我而言,它是在

C:\ProgramData\MySQL\MySQL Server 8.0\my.ini

最后,重新启动 SQL Server。检查您的新密码。

I am Using MySQL Server 8 and this is how I solved this problem.

create a file and name it accordingly. I've named it as reset.txt. put below content in the file but change MyNewPass. This will be your new SQL password

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

Stop my SQL server (Go to services and search MYSQL and right-click on it and stop )

Now open a cmd in your bin folder of My SQLserver directory. in my case it's

C:\Program Files\MySQL\MySQL Server 8.0\bin

execute the following command.

mysqld.exe --defaults-file="C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" --init-file="C:\reset.txt' 

NOTE: my reset.txt file is in the c drive. Give the correct path. Check my.ini file is in the directory. In my case, it is in

C:\ProgramData\MySQL\MySQL Server 8.0\my.ini

Finally, restart the SQL Server. Check with your new password.

暖阳 2024-10-10 19:55:54

请按照以下链接中的说明重置 root 密码。您必须从 mysql 外部执行此操作。

重置 MySql 密码

忘记 MySQL 密码可能会让您非常头疼。您可以按照以下一些简单步骤在 Windows 下恢复 MySql 密码
停止MySql

在继续之前,您必须停止MySql 服务。在Windows 环境中,转到“任务管理器”并在“服务”选项卡下找到MySQL 并停止它。
编写 Sql 来更改密码

您所需要做的就是创建一个文本文件并将以下两行放入其中。 UPDATE mysql.user SET 密码=PASSWORD('MyNewPass') WHERE User='root';同花顺特权;将其保存在 C:\ 驱动器下并命名为“mysql-init.txt”
是时候自己重启 MySQL 了。

现在是时候重新启动之前停止过的 MySQL,但这次是从命令行启动。 C:> C:\mysql\bin\mysqld-nt --init-file=C:\mysql-init.txt
收尾啦!

现在您可以使用新密码登录。完成后,删除您在上一阶段创建的文件。

还有一个链接(http://kaziprogrammingblog.osinweb.com/article/showarticle /Resetting-MySql-Password),我在其中解释了同样的事情。

希望这会有所帮助..:)

Please follow the instruction from the below link to reset your root password.You have to do it from outside mysql.

Resetting MySql Password

Forgetting your MySQL password can be a real headache for you.Here are some easy steps that you can follow to recover MySql password under Windows
Stop MySql

You have to stop MySql service before you proceed.In Windows environment, go to 'Task Manager' and Under 'Service' tab find MySQL and stop it.
Write Sql to change your password

All you need to do is to create a text file and put the below two lines into that. UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root'; FLUSH PRIVILEGES; Save it under C:\ drive and give it a name 'mysql-init.txt'
Time to restart MySQL by your own.

Now it's time to restart your MySQl which you stopped in before but this time from command line. C:> C:\mysql\bin\mysqld-nt --init-file=C:\mysql-init.txt
Finishing up!

Now you can log in with your new password. When you finish remove the file that you created in the previous stage.

Also there is a link (http://kaziprogrammingblog.osinweb.com/article/showarticle/Resetting-MySql-Password) where I have explained the same thing.

Hope this will help..:)

思念满溢 2024-10-10 19:55:54

Windows 上 XAMPP 的答案:

  • 编辑 C:\xampp\mysql\bin\my.ini 并在 [mysqld] 下面插入skip-grant-tables
  • 从控制面板界面重新启动 MySQL
  • 在 phpMyAdmin 窗口中,从顶部面板中选择 SQL 选项卡。这将打开 SQL 选项卡,我们可以在其中运行 SQL 查询。现在在文本区域中键入以下查询,然后单击执行

更新 mysql.user SET 密码=PASSWORD('密码') WHERE User='root';同花顺特权;

  • 删除 my.ini 文件中的skip-grant-tables
  • 从控制面板界面重新启动MySQL

完成!

请注意,mysql root 用户密码不必与 phpmyadmin 的密码相同。

Answer for XAMPP on Windows:

  • Edit C:\xampp\mysql\bin\my.ini and insert skip-grant-tables below [mysqld]
  • Restart MySQL from Control Panel interface
  • In the phpMyAdmin window, select SQL tab from the top panel. This will open the SQL tab where we can run the SQL queries. Now type the following query in the text area and click Go

UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root'; FLUSH PRIVILEGES;

  • Remove the skip-grant-tables in the my.ini file
  • Restart MySQL from Control Panel interface

DONE!

Be awared that mysql root user password does not have to be the same as password for phpmyadmin.

心如荒岛 2024-10-10 19:55:54

这是我所做的并且有效:
安装(rpm 安装)后,执行 vi /etc/my.cnf
这将为您提供设置 datadir 的路径。对我来说是datadir=/var/lib/mysql
在那里添加一行user=root,并删除datadir路径中的所有内容:rm -rf /var/lib/mysql/* .
现在输入命令:mysqld --initialize
在某个位置生成临时密码。为此:
grep“临时密码”/var/log/*
您将看到一行内容:
/var/log/mysqld.log:2018-05-01T15:13:47.937449Z 5 [Note] [MY-010454] [Server] 为 root@localhost 生成临时密码: &uosjoGfi9:K< /代码>。所以对我来说 &uosjoGfi9:K 是我的临时密码。
现在执行mysql -u root -p。粘贴您从上面获得的临时密码。
您将处于 mysql cli 模式。现在做:
mysql>使用mysql;
系统会要求您重置密码:
错误 1820 (HY000):在执行此语句之前,您必须使用 ALTER USER 语句重置密码。
运行您的 ALTER 命令:
mysql>更改由“MyNewPswd”标识的用户“root”@“localhost”;
你就完成了。

here is what I did and it worked:
After you install (rpm installation), do a vi /etc/my.cnf.
This will give you a path where your datadir is set. For me it was datadir=/var/lib/mysql.
Add a line there user=root, and remove all the content inside the datadir path: rm -rf /var/lib/mysql/*.
Now hit the command: mysqld --initialize.
A temporary password is generated at a location. For this:
grep "A temporary password" /var/log/*.
You will get a line that says:
/var/log/mysqld.log:2018-05-01T15:13:47.937449Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: &uosjoGfi9:K. So for me &uosjoGfi9:K was my temporary password.
Now do a mysql -u root -p. Paste your temporary password from what you got from above.
You will be in your mysql cli mode. Now do:
mysql> use mysql;
You will be asked to reset your password:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
Run your ALTER command:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPswd';
And you are done.

北音执念 2024-10-10 19:55:54

就我而言,它是一个令人沮丧的 Windows Server 安装(iis/php/mysql),所以这就是我所做的:

第 1 部分:删除旧的 MYSQL:

注意:如果您已经有数据,则应该备份它!

步骤1
从控制面板卸载 MySQL。 (你应该知道怎么做)

第2步
以管理员身份运行命令提示符并执行以下命令来停止和删除 MySQL 服务。

Net stop MySQL
Sc delete MySQL

步骤3
删除这些文件夹:

C:\Program Files\MySQL
C:\Program Files (x86)\MySQL
C:\ProgramData\MySQL

如果存在:

C:\Users\[User-Name]\AppData\Roaming\MySQL

第 2 部分:安装 MYSQL

https://dev 下载安装程序。 mysql.com/downloads/installer/ 并安装
-MySQL Server 8.0.23

以管理员身份打开cmd并转到
c:\Program Files\MySQL\MySQL Server 8.0\bin\
现在运行

mysqld --initialize --console
mysqld --install

启动服务(在运行面板中键入 services.msc)

现在回到控制台运行:

mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

就是这样

in my case its a frustrating Windows Server installation (iis/php/mysql) so this is what i did:

PART 1: remove the old MYSQL :

NOTE: if you already have data you should back it up!

Step 1
Uninstall MySQL from Control Panel. (you should know how to do this)

Step 2
Run Command Prompt as Admini and execute the following commands to stop and remove MySQL service.

Net stop MySQL
Sc delete MySQL

Step 3
Delete these folders:

C:\Program Files\MySQL
C:\Program Files (x86)\MySQL
C:\ProgramData\MySQL

And if it exists:

C:\Users\[User-Name]\AppData\Roaming\MySQL

PART 2: Install MYSQL

Download installer from https://dev.mysql.com/downloads/installer/ and install
-MySQL Server 8.0.23

open cmd as admin and go to
c:\Program Files\MySQL\MySQL Server 8.0\bin\
run

mysqld --initialize --console
mysqld --install

now start the service (type services.msc in run panel)

now back to console run:

mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

that's it

杀お生予夺 2024-10-10 19:55:54

mysql80:

update user SET authentication_string='your password after encode to sha256' where User='root';

mysql80:

update user SET authentication_string='your password after encode to sha256' where User='root';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文