Linux 如何更改mysql root密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8789522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the mysql root password
提问by yossi
I have installed MySQL server 5 on redhat linux. I can't login as root so I can't change the root password.
我已经在 redhat linux 上安装了 MySQL 服务器 5。我无法以 root 身份登录,因此无法更改 root 密码。
mysql -u root -p
Enter password: <blank>
ERROR 1045 (28000): Access denied for user 'root'@'localhost'
(using password: NO)
When I try to set one like this:
当我尝试设置一个这样的:
mysqladmin -u root password 'newpass'
I get an error:
我收到一个错误:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost'
(using password: NO)'
As if there is a root password set.
好像设置了root密码。
I have also tried resetting the password using (described here)
我也尝试使用(描述here)重置密码
/sbin/service mysqld start --skip-grant-tables
And then making:
然后制作:
mysql> UPDATE mysql.user SET Password=PASSWORD('newpass')
-> WHERE User='root';
ERROR 1142 (42000): UPDATE command denied to user ''@'localhost' for table 'user'
I even uninstalled mysql-server (using yum) and then reinstalled it but that did not help.
我什至卸载了 mysql-server(使用 yum),然后重新安装了它,但这没有帮助。
How do I force reset the root password?
如何强制重置root密码?
采纳答案by yossi
I removed the MySQL installation and deleted the data files, and then reinstalled it.
我删除了 MySQL 安装并删除了数据文件,然后重新安装了它。
Then I was able to set the root password. Once you set the root password to something. mysqladmin won't let you reset it if you don't know it.
然后我就可以设置root密码了。一旦你设置了 root 密码。如果您不知道,mysqladmin 不会让您重置它。
To reset it, you've got to have ownership over how mysqld is executed, and feed it an init file to change the root password: https://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html
要重置它,您必须对 mysqld 的执行方式拥有所有权,并为其提供一个 init 文件以更改 root 密码:https: //dev.mysql.com/doc/refman/5.0/en/resetting-permissions .html
回答by danijar
The root user password is an empty string by default.
默认情况下,root 用户密码为空字符串。
And (using password: NO)
says that there is no password.
并(using password: NO)
说没有密码。
Do you try to login from another system? I imagine you can only login as root user locally.
您是否尝试从其他系统登录?我想您只能在本地以 root 用户身份登录。
回答by Michael Mior
One option is to save UPDATE mysql.user SET Password=PASSWORD('newpass') WHERE User='root';
into a file and then manually start mysqld
with --init-file=FILENAME
. Once the server starts, it should reset your password, and then you should be able to log in. After this, you should shut down the server and start it normally.
一种选择是保存UPDATE mysql.user SET Password=PASSWORD('newpass') WHERE User='root';
到文件中,然后手动mysqld
以--init-file=FILENAME
. 一旦服务器启动,它应该重置您的密码,然后您应该可以登录。之后,您应该关闭服务器并正常启动它。
回答by mangaldev
Remove the -p from your command. -p forces the prompt for password.
从您的命令中删除 -p。-p 强制提示输入密码。
Use :
mysql -u root
用 :
mysql -u root
This will resolve your problem.
这将解决您的问题。
回答by Alex W
This helped me on Windows with MySQL Server 5.6. Make sure you change the mysqld
path to point to where you have installed MySql Server, for me it was "C:\Program Files\mysql\MySQL Server 5.6\bin\mysqld.exe"
:
这对我在带有 MySQL Server 5.6 的 Windows 上有所帮助。确保将mysqld
路径更改为指向安装 MySql Server 的位置,对我来说是"C:\Program Files\mysql\MySQL Server 5.6\bin\mysqld.exe"
:
Log on to your system as Administrator.
Stop the MySQL server if it is running. For a server that is running as a Windows service, go to the Services manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list and stop it.
If your server is not running as a service, you may need to use the Task Manager to force it to stop.
Create a text file containing the following statements. Replace the password with the password that you want to use.
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root'; FLUSH PRIVILEGES;
Write the UPDATE and FLUSH statements each on a single line. The UPDATE statement resets the password for all root accounts, and the FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.
Save the file. For this example, the file will be named
C:\mysql-init.txt
.Open a console window to get to the command prompt: From the Start menu, select Run, then enter cmd as the command to be run.
Start the MySQL server with the special --init-file option (notice that the backslash in the option value is doubled):
C:\> C:\mysql\bin\mysqld --init-file=C:\mysql-init.txt
If you installed MySQL to a location other than C:\mysql, adjust the command accordingly.
The server executes the contents of the file named by the --init-file option at startup, changing each root account password.
You can also add the --console option to the command if you want server output to appear in the console window rather than in a log file.
If you installed MySQL using the MySQL Installation Wizard, you may need to specify a --defaults-file option:
C:\> "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld.exe" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.5\my.ini" --init-file=C:\mysql-init.txt
The appropriate --defaults-file setting can be found using the Services Manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list, right-click it, and choose the Properties option. The Path to executable field contains the --defaults-file setting.
After the server has started successfully, delete C:\mysql-init.txt.
以管理员身份登录您的系统。
如果 MySQL 服务器正在运行,请停止它。对于作为 Windows 服务运行的服务器,请转至服务管理器:从开始菜单中,依次选择控制面板、管理工具和服务。在列表中找到 MySQL 服务并停止它。
如果您的服务器没有作为服务运行,您可能需要使用任务管理器来强制它停止。
创建一个包含以下语句的文本文件。将密码替换为您要使用的密码。
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root'; FLUSH PRIVILEGES;
将 UPDATE 和 FLUSH 语句分别写在一行上。UPDATE 语句重置所有 root 帐户的密码,FLUSH 语句告诉服务器将授权表重新加载到内存中,以便它注意到密码更改。
保存文件。对于此示例,文件将命名为
C:\mysql-init.txt
.打开控制台窗口以进入命令提示符:从开始菜单中,选择运行,然后输入 cmd 作为要运行的命令。
使用特殊的 --init-file 选项启动 MySQL 服务器(注意选项值中的反斜杠是双倍的):
C:\> C:\mysql\bin\mysqld --init-file=C:\mysql-init.txt
如果您将 MySQL 安装到 C:\mysql 以外的位置,请相应地调整命令。
服务器在启动时执行由 --init-file 选项命名的文件的内容,更改每个 root 帐户密码。
如果您希望服务器输出显示在控制台窗口而不是日志文件中,您还可以将 --console 选项添加到命令中。
如果您使用 MySQL 安装向导安装 MySQL,则可能需要指定 --defaults-file 选项:
C:\> "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld.exe" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.5\my.ini" --init-file=C:\mysql-init.txt
可以使用服务管理器找到适当的 --defaults-file 设置:从开始菜单中,选择控制面板,然后选择管理工具,然后选择服务。在列表中找到 MySQL 服务,右键单击它,然后选择 Properties 选项。可执行文件路径包含 --defaults-file 设置。
服务器启动成功后,删除C:\mysql-init.txt。
http://docs.oracle.com/cd/E17952_01/refman-5.5-en/resetting-permissions.html
http://docs.oracle.com/cd/E17952_01/refman-5.5-en/resetting-permissions.html
回答by Marcelo
Try do this:
尝试这样做:
mysql -u root
and then:
进而:
mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')
-> WHERE User='root';
mysql> FLUSH PRIVILEGES;
Worked fine for me!
对我来说很好用!
回答by A.Dhond
Probably a bit late here , but here is what I did :
这里可能有点晚了,但这是我所做的:
create file resetpass.sh which has :
创建文件 resetpass.sh,其中包含:
UPDATE mysql.user SET Password=PASSWORD('newpassword') WHERE User='root';
# mysqld_safe --init-file=resetpass.sh
# service mysqld start --skip-grant-tables
# mysql -u root -p
Enter pass
输入通行证
mysql > change root pass ; flush privs;
quit
退出
# restart mysql service
The MySQL version I was using was 5.1.73 under centos6
我用的mysql版本是centos6下的5.1.73
回答by Wildcard
According to the docs, in MySQL 5.7 the temp password gets put in /var/log/mysqld
, but this is evidently not the case in 5.6.
根据文档,在 MySQL 5.7 中,临时密码被放入/var/log/mysqld
,但在 5.6 中显然不是这种情况。
With MySQL version 5.6 installed on RHEL 6.7, I finally found the temporary root password set during install was placed in the file /root/.mysql_secret
.
在 RHEL 6.7 上安装 MySQL 5.6 版后,我终于发现安装时设置的临时 root 密码放在了/root/.mysql_secret
.
回答by Pyroglyph
A little late to the game, but I had the same issue on a raspberry pi install and found out that it needs elevation. Adding a sudo to the front of the password change allowed it to work.
游戏有点晚了,但我在 raspberry pi 安装上遇到了同样的问题,发现它需要提升。在密码更改的前面添加一个 sudo 允许它工作。
sudo mysqladmin -u root password 'newpass'
followed by an elevated sql access
其次是提升的 sql 访问权限
sudo mysql -u root -p
If either are not run as sudo, it will fail.
如果没有以 sudo 身份运行,它将失败。