在 OS X 上设置 MySQL root 用户密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6474775/
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
Setting the MySQL root user password on OS X
提问by madaura
I just installed MySQL on Mac OS X. The next step was setting the root user password, so I did this next:
我刚刚在 Mac OS X 上安装了 MySQL。下一步是设置 root 用户密码,所以我接下来做了:
- Launch the terminal app to access the Unix command line.
Under the Unix prompt I executed these commands:
$ cd /usr/local/mysql/bin $ ./mysqladmin -u root password 'password'
- 启动终端应用程序以访问 Unix 命令行。
在 Unix 提示符下,我执行了以下命令:
$ cd /usr/local/mysql/bin $ ./mysqladmin -u root password 'password'
But, when I execute the command
但是,当我执行命令时
$ ./mysql -u root
, this is the answer:
$ ./mysql -u root
,这是答案:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 224
Server version: 5.5.13 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
I can get into the mysql
command line without any password!
我可以在mysql
没有任何密码的情况下进入命令行!
Why is this?
为什么是这样?
回答by Scott
Try the command FLUSH PRIVILEGES
when you log into the MySQL terminal. If that doesn't work, try the following set of commands while in the MySQL terminal
FLUSH PRIVILEGES
当您登录 MySQL 终端时尝试该命令。如果这不起作用,请在 MySQL 终端中尝试以下命令集
$ mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET password=PASSWORD("NEWPASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
Change out NEWPASSWORD with whatever password you want. Should be all set!
使用您想要的任何密码更改 NEWPASSWORD。应该都准备好了!
Update: As of MySQL 5.7, the password
field has been renamed authentication_string
. When changing the password, use the following query to change the password. All other commands remain the same:
更新:从 MySQL 5.7 开始,该password
字段已重命名为authentication_string
。更改密码时,请使用以下查询更改密码。所有其他命令保持不变:
mysql> UPDATE user SET authentication_string=PASSWORD("NEWPASSWORD") WHERE User='root';
Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work, as mentioned in the comments below. You have to use:
更新:在 8.0.15(可能已经在该版本之前),PASSWORD() 函数不起作用,如下面的评论中所述。你必须使用:
UPDATE mysql.user SET authentication_string='password' WHERE User='root';
UPDATE mysql.user SET authentication_string='password' WHERE User='root';
回答by radtek
If you don't remember the password you set for root and need to reset it, follow these steps:
如果您不记得为 root 设置的密码并需要重置它,请按照下列步骤操作:
- Stop the mysqld server, this varies per install
- Run the server in safe mode with privilege bypass
- 停止 mysqld 服务器,这因安装而异
- 以绕过特权的安全模式运行服务器
sudo mysqld_safe --skip-grant-tables;
sudo mysqld_safe --skip-grant-tables;
- In a new window connect to the database, set a new password and flush the permissions & quit:
- 在新窗口中连接到数据库,设置新密码并刷新权限并退出:
mysql -u root
mysql -u root
For MySQL older than MySQL 5.7 use:
对于早于 MySQL 5.7 的 MySQL,请使用:
UPDATE mysql.user SET Password=PASSWORD('your-password') WHERE User='root';
UPDATE mysql.user SET Password=PASSWORD('your-password') WHERE User='root';
For MySQL 5.7+ use:
对于 MySQL 5.7+ 使用:
USE mysql;
USE mysql;
UPDATE mysql.user SET authentication_string=PASSWORD("your-password") WHERE User='root';
UPDATE mysql.user SET authentication_string=PASSWORD("your-password") WHERE User='root';
Refresh and quit:
刷新并退出:
FLUSH PRIVILEGES;
FLUSH PRIVILEGES;
\q
\q
- Stop the safe mode server and start your regular server back. The new password should work now. Worked like a charm for me :)
- 停止安全模式服务器并重新启动常规服务器。新密码现在应该可以使用了。对我来说就像一个魅力:)
回答by bob
Once you've installed MySQL, you'll need to establish the "root" password. If you don't establish a root password, then, well, there is no root password, and you don't need a password to log in.
安装 MySQL 后,您需要建立“root”密码。如果你没有建立root密码,那么,好吧,没有root密码,你也不需要密码来登录。
So, that being said, you need to establish a root password.
因此,话虽如此,您需要建立一个 root 密码。
Using terminal enter the following:
使用终端输入以下内容:
Installation: Set root user password:
安装:设置root用户密码:
/usr/local/mysql/bin/mysqladmin -u root password NEW_PASSWORD_HERE
If you've made a mistake, or need to change the root password use the following:
如果您犯了错误,或者需要更改 root 密码,请使用以下命令:
Change root password:
更改root密码:
cd /usr/local/mysql/bin/
./mysql -u root -p
> Enter password: [type old password invisibly]
use mysql;
update user set password=PASSWORD("NEW_PASSWORD_HERE") where User='root';
flush privileges;
quit
回答by Ankireddy Polu
The instructions provided in the mysql website is so clear, than the above mentioned
mysql网站上提供的说明很清楚,比上面说的
$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables
/usr/local/mysql/bin/mysql
- mysql> FLUSH PRIVILEGES;
- mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
- mysql>
exit
or Ctrl + z $ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start
/usr/local/mysql/support-files/mysql -u root -p
- Enter the new password i.e MyNewPass
$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables
/usr/local/mysql/bin/mysql
- mysql> 刷新特权;
- mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
- mysql>
exit
或 Ctrl + z $ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start
/usr/local/mysql/support-files/mysql -u root -p
- 输入新密码,即 MyNewPass
Reference: http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
参考:http: //dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
回答by imbond
Stop the mysqld server.
- Mac OSX:
System Preferences
>MySQL
>Stop MySQL Server
- Linux (From Terminal):
sudo systemctl stop mysqld.service
- Mac OSX:
Start the server in safe mode with privilege bypass
- From Terminal:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
- From Terminal:
In a newterminal window:
sudo /usr/local/mysql/bin/mysql -u root
This will open the mysql command line. From here enter:
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';
FLUSH PRIVILEGES;
quit
Stop the mysqld server again and restart it in normal mode.
- Mac OSX (From Terminal):
sudo /usr/local/mysql/support-files/mysql.server restart
- Linux Terminal:
sudo systemctl restart mysqld
- Mac OSX (From Terminal):
停止 mysqld 服务器。
- Mac OSX:
System Preferences
>MySQL
>Stop MySQL Server
- Linux(从终端):
sudo systemctl stop mysqld.service
- Mac OSX:
以绕过特权的安全模式启动服务器
- 从终端:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
- 从终端:
在新的终端窗口中:
sudo /usr/local/mysql/bin/mysql -u root
这将打开 mysql 命令行。从这里输入:
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';
FLUSH PRIVILEGES;
quit
再次停止 mysqld 服务器并以正常模式重新启动它。
- Mac OSX(从终端):
sudo /usr/local/mysql/support-files/mysql.server restart
- Linux终端:
sudo systemctl restart mysqld
- Mac OSX(从终端):
回答by Deoxyseia
For new Mysql 5.7 for some reason bin commands of Mysql not attached to the shell:
对于新的 Mysql 5.7 出于某种原因,Mysql 的 bin 命令没有附加到 shell:
Restart the Mac after install.
Start Mysql:
System Preferences > Mysql > Start button
Go to Mysql install folder in terminal:
$ cd /usr/local/mysql/bin/
Access to Mysql:
$ ./mysql -u root -p
安装后重启 Mac。
启动Mysql:
系统偏好设置 > Mysql > 开始按钮
转到终端中的 Mysql 安装文件夹:
$ cd /usr/local/mysql/bin/
访问Mysql:
$ ./mysql -u root -p
and enter the initial password given to the installation.
并输入为安装提供的初始密码。
In Mysql terminal change password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword';
在 Mysql 终端更改密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword';
回答by Nitish Pareek
In the terminal, write mysql -u root -p
and hit Return.
Enter the current mysql password that you must have noted down.
And set the password
SET PASSWORD = PASSWORD('new_password');
在终端中,写入mysql -u root -p
并按回车键。输入您必须记下的当前 mysql 密码。并设置密码
SET PASSWORD = PASSWORD('new_password');
Please refer to this documentation herefor more details.
有关更多详细信息,请参阅此处的文档。
回答by Homam Bahrani
If you have forgot the MySQL root password, can't remember or want to break in….. you can reset the mysql database password from the command line in either Linux or OS X as long as you know the root user password of the box you are on:
如果你忘记了 MySQL 的 root 密码,想不起来或者想闯入……你可以在 Linux 或 OS X 中从命令行重置 mysql 数据库密码,只要你知道 box 的 root 用户密码你是在:
(1) Stop MySQL
(1) 停止 MySQL
sudo /usr/local/mysql/support-files/mysql.server stop
(2) Start it in safe mode:
(2) 在安全模式下启动它:
sudo mysqld_safe --skip-grant-tables
(3) This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:
(3) 这将是一个持续的命令,直到进程完成,所以打开另一个 shell/终端窗口,在没有密码的情况下登录:
mysql -u root
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
In the UPDATE command above just replace the 'password' with your own new password, make sure to keep the quotation marks
在上面的 UPDATE 命令中,只需将“密码”替换为您自己的新密码,请确保保留引号
(4) Save and quite
(4)保存和相当
FLUSH PRIVILEGES;
\q
(5) Start MySQL
(5)启动MySQL
sudo /usr/local/mysql/support-files/mysql.server start
回答by Prem Timsina
When I installed OS X Yosemite,I got problem with Mysql. I tried lot of methods but none worked. I actually found a quite easy way. Try this out.
当我安装 OS X Yosemite 时,Mysql 出现问题。我尝试了很多方法,但都没有奏效。我其实找到了一个很简单的方法。试试这个。
- First log in terminal from su privileges.
- 首先以su权限登录终端。
sudo su
sudo su
- stop mysql
- 停止mysql
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server stop
- start in safe mode:
- 以安全模式启动:
sudo mysqld_safe --skip-grant-tables
sudo mysqld_safe --skip-grant-tables
- open another terminal, log in as su privileges than, log in mysql without password
- 再打开一个终端,以su权限登录,不用密码登录mysql
mysql -u root
mysql -u root
- change the password
- 更改密码
UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root';
UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root';
- flush privileges
- 刷新权限
FLUSH PRIVILEGES;
FLUSH PRIVILEGES;
- You are done now
- 你现在完成了
回答by Imtiaz Shakil Siddique
The methods mentioned in existing answers don't work for mysql 5.7.6 or later. According mysql documentation this is the recommended way.
现有答案中提到的方法不适用于 mysql 5.7.6 或更高版本。根据 mysql 文档,这是推荐的方式。
B.5.3.2.3 Resetting the Root Password: Generic Instructions
B.5.3.2.3 重置根密码:通用说明
MySQL 5.7.6 and later:
MySQL 5.7.6 及更高版本:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
Reference: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
参考:https: //dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html