如何将root
MySQL 用户的密码更改为null
-- 表示没有密码或''
-- 从 MySQL 命令行客户端?
21 回答
为我和“5.7.11 MySQL 社区服务器”工作:
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
我还必须更改“插件”字段,因为它被设置为“auth_socket”。
之后,我可以像mysql -u root
没有密码一样连接。
如果您想要一个空密码,您应该将密码设置为 null 并且不使用密码哈希函数,如下所示:
在命令行上:
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-networking &
mysql -uroot
在 MySQL 中:
use mysql;
update user set password=null where User='root';
flush privileges;
quit;
- 以 root 用户身份连接到 mysql(使用以下两种方法之一)
- 以root身份登录并使用启动mysql
mysql -p
,输入当前root密码 - 以自我身份登录并使用启动mysql
mysql -u root -p
,输入当前root密码
- 以root身份登录并使用启动mysql
mysql> set password = password('');
完毕!没有root密码。
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('');
这在带有 v5.7.15 MySQL 的 Ubuntu 16.04 上对我有用:
首先,确保您已安装 mysql-client ( sudo apt-get install mysql-client
)。
打开终端并登录:
mysql -uroot -p
(然后输入您的密码)
之后:
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
(tnx @Stanislav Karakhanov)
最后重要的是重置 mysql 服务:
sudo service mysql restart
您现在应该也可以使用 MySQL Workbench 登录(无需密码)。
您可以通过以下五个简单步骤恢复 MySQL 数据库服务器密码。
步骤#1:停止 MySQL 服务器进程。
第 2 步:使用 --skip-grant-tables 选项启动 MySQL (mysqld) 服务器/守护进程,这样它就不会提示输入密码。
第 3 步:以 root 用户身份连接到 mysql 服务器。
第 4 步:设置新的 mysql 根帐户密码,即重置 mysql 密码。
步骤#5:退出并重新启动 MySQL 服务器。
以下是您需要为每个步骤键入的命令(以 root 用户身份登录):
步骤#1:停止mysql服务
# /etc/init.d/mysql stop
输出:
Stopping MySQL database server: mysqld.
第 2 步:无密码启动 MySQL 服务器:
# mysqld_safe --skip-grant-tables &
输出:
[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started
第 3 步:使用 mysql 客户端连接到 mysql 服务器:
# mysql -u root
输出:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
步骤#4:设置新的 MySQL root 用户密码
mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
步骤#5:停止 MySQL 服务器:
# /etc/init.d/mysql stop
输出:
Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended
[1]+ Done mysqld_safe --skip-grant-tables
第 6 步:启动 MySQL 服务器并对其进行测试
# /etc/init.d/mysql start
# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# mysql -u root -p
来源:http ://www.cyberciti.biz/tips/recover-mysql-root-password.html
对于 MySQL 8.0,只需:
SET PASSWORD FOR 'root'@'localhost' = '';
mysql
直接编辑数据库不是一个好主意。
我更喜欢以下步骤:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '';
mysql> flush privileges;
这个对我有用。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
这是来自 MySQL 8.0.13:
use mysql;
update user set authentication_string=null where user='root';
quit;
user64141的回答
use mysql;
update user set password=null where User='root';
flush privileges;
quit;
在 MariaDB 10.1.5 中对我不起作用(应该是 MySQL 的替代品)。虽然没有在 MySQL 5.6 中对其进行测试以查看是否是上游更改,但我得到的错误是:
错误 1048 (23000):“密码”列不能为空
但是用空的单引号或双引号替换 null 效果很好。
update user set password='' where User='root';
或者
update user set password="" where User='root';
我注意到上面的一些解决方案现在已被弃用。
要设置空密码,只需按照以下步骤操作:
mysql -u root -p
use mysql
SET PASSWORD FOR 'root'@'localhost' = '';
\q (to quit)
now run: mysql -u root
您现在应该可以在没有密码的情况下启动 mysql 了。
如果您知道您的 Root 密码并且只想重置它,请执行以下操作:
从控制面板 > 管理工具 > 服务启动 MySQL 服务。(仅当它之前被您阻止!否则,请跳过此步骤)
启动 MySQL 工作台
键入此命令/SQL 行
ALTER USER 'root'@'localhost' 密码过期;
要重置任何其他用户密码...只需键入其他用户名而不是 root。
我正在使用 nodejs 和 windows 10。两个答案的组合对我有用。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '';
mysql> flush privileges;
其次是:
restart;
希望这对仍然有此问题的其他人有所帮助。
语法因版本而略有不同。从这里的文档: https ://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
MySQL 5.7.6 及更高版本:
ALTER USER 'root'@'localhost' IDENTIFIED BY '';
MySQL 5.7.5 及更早版本:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('');
我的 MySQL 5.7 变体:
停止服务mysql:
$ sudo service mysql stop
在安全模式下运行:
$ sudo mysqld_safe --skip-grant-tables --skip-networking
(上面是整个命令)
打开一个新的终端窗口:
$ mysql -u root
$ mysql use mysql;
$ mysql update user set authentication_string=password('password') where user='root';
$ mysql update user set plugin="mysql_native_password" where User='root';
$ mysql flush privileges;
$ mysql quit;
运行mysql服务:
$ sudo service mysql start
想把我自己的 2cents 放在这里,因为上面的答案对我不起作用。在 centos 7、mysql community v8 上,shell 是 bash。
正确的命令如下:
# start mysql without password checking
systemctl stop mysqld 2>/dev/null
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables" &&
systemctl start mysqld
# set default password to nothing
mysql -u root mysql <<- 'EOF'
FLUSH PRIVILEGES;
UNINSTALL COMPONENT 'file://component_validate_password';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
FLUSH PRIVILEGES;
INSTALL COMPONENT 'file://component_validate_password';
EOF
# restart mysql normally
systemctl restart mysqld
然后你可以不用密码登录:
mysql -u root
这都是因为您安装了高于 5.6 版本的 mysql
解决方案
1.可以降级mysql版本的解决方法
2 使用配置选项将身份验证重新配置为本机类型或传统类型身份验证
在 ubuntu 19.10、mysql 8 上,这对我有用:
$ sudo mysqld --skip-grant-tables &
$ mysql
> use mysql
> alter user set authentication_string='', plugin='mysql_native_password' where user = 'root';
> quit
$ sudo mysqladmin shutdown
$ sudo systemctl start mysql
如果您在尝试运行时遇到错误mysqld_safe
,特别是:/var/run/mysqld for UNIX socket file don't exists
,您可以尝试创建目录并mysqld_safe
再次运行。
$ sudo mkdir /var/run/mysqld
$ sudo chown mysql /var/run/mysqld
$ sudo chgrp mysql /var/run/mysqld
经过几个小时的搜索,我找到了它。只需将密码更改为其中包含大写数字和特殊字符的内容。