Mysql 如何将root密码设置为null

Mysql 如何将root密码设置为null,mysql,mysql-management,Mysql,Mysql Management,如何从MySQL命令行客户端将MySQL的root用户的密码更改为null——意味着没有密码或'——您可以通过以下五个简单步骤恢复MySQL数据库服务器密码 步骤1:停止MySQL服务器进程 步骤2:使用--skip grant tables选项启动MySQL(mysqld)服务器/守护进程,这样它就不会提示输入密码 步骤3:以root用户身份连接到mysql服务器 步骤4:设置新的mysql根帐户密码,即重置mysql密码 步骤5:退出并重新启动MySQL服务器 以下是您需要为每个步骤键入的命

如何从MySQL命令行客户端将MySQL的
root
用户的密码更改为
null
——意味着没有密码或
'
——您可以通过以下五个简单步骤恢复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
# /etc/init.d/mysql stop
输出:

Stopping MySQL database server: mysqld.
[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started
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>
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
步骤2启动MySQL服务器,无密码:

# mysqld_safe --skip-grant-tables &
mysql -u root
输出:

Stopping MySQL database server: mysqld.
[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started
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>
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
步骤3:使用mysql客户端连接到mysql服务器:

# mysql -u root
输出:

Stopping MySQL database server: mysqld.
[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started
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>
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
第4步:设置新的MySQL根用户密码

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
第5步:停止MySQL服务器:

输出:

Stopping MySQL database server: mysqld.
[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started
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>
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
来源:

  • 以root用户身份连接到mysql(使用以下两种方法之一)
    • 以root用户身份登录并使用mysql-p启动mysql,输入当前root密码
    • 以self身份登录并使用
      mysql-u root-p
      启动mysql,输入当前的root密码
  • mysql>set password=password(“”)

完成了!无根密码。

如果需要空密码,应将密码设置为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;

用户的答案是64141

use mysql;
update user set password=null where User='root';
flush privileges;
quit;
在MariaDB 10.1.5中对我不起作用(应该是MySQL的替代品)。虽然我没有在MySQL 5.6中测试它是否是上游更改,但我得到的错误是:

错误1048(23000):“密码”列不能为空

但是用空的单引号或双引号替换空引号效果很好

update user set password='' where User='root';


如果您知道您的根密码,只想重置它,请执行以下操作:

  • 从控制面板>管理工具>服务启动MySQL服务。(只有在您之前停止该操作时才可以!否则,请跳过此步骤)

  • 启动MySQL工作台

  • 键入此命令/SQL行

    更改用户“root”@“localhost”密码到期

要重置任何其他用户密码。。。只需键入其他用户名而不是root

为我和“5.7.11 MySQL社区服务器”工作:

我还必须更改“plugin”字段,因为它被设置为“auth_socket”


在那之后,我可以不用密码以
mysql-u root
的身份连接。

这对我在Ubuntu 16.04和v5.7.15 mysql上的工作很有效:

首先,确保安装了mysql客户端(
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登录(不使用Passbrow)。

直接编辑MySQL
数据库不是一个好主意

我更喜欢以下步骤:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY ''; 
mysql> flush privileges;

对于无密码连接到mysql:

# mysqld_safe --skip-grant-tables &
mysql -u root
mysql-p
设置密码=”


我正在使用nodejs和windows 10。两个答案的组合对我很有效

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY ''; 
mysql> flush privileges;
其次是:

restart;
希望这对其他仍有问题的人有所帮助。

这对我很有用


将用户“root”@“localhost”通过“password”标识为mysql\u native\u password

根据版本的不同,语法略有不同。从这里的文档:

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
对于MySQL 8.0,只需:


为'root'@'localhost'=''设置密码

我想把我自己的2个字母放在这里,因为上面的答案对我不适用。 在centos 7、mysql社区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

这都是因为您安装了高于5.6版本的mysql

解决方案

1.您可以降级mysql版本解决方案

2使用

配置选项

这来自MySQL 8.0.13:

use mysql;

update user set authentication_string=null  where user='root';

quit;

我注意到上面的一些解决方案现在已被弃用

要设置空密码,只需执行以下步骤:

mysql -u root -p

use mysql

SET PASSWORD FOR 'root'@'localhost' = '';

\q (to quit)

now run: mysql -u root

你现在应该可以不用密码启动mysql了。

在搜索了几个小时后,我找到了它。只需将密码更改为包含大写数字和特殊字符的密码。

在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\u safe
时出错,特别是:
/var/run/mysqld for UNIX套接字文件不存在
,可以尝试创建目录并再次运行
mysqld\u safe

$ sudo mkdir /var/run/mysqld
$ sudo chown mysql /var/run/mysqld
$ sudo chgrp mysql /var/run/mysqld

此链接实际上没有告诉您如何将其设置为null,这正是原始帖子的内容。@y0mbo:您不能
updateuser set password=password(“”)where user='root'?第1步和第2步不是必需的(问题并不是说以前的密码丢失或忘记),第4步是错误的,因为问题询问如何设置空白密码,应该是
update user set password=password(“”),其中user='root'
如果使用MySQL 5.5.46或更新版本,并且
update user set password=null,其中user='root'如果使用旧版本,我会再次访问