Mysql (MariaDB 10.0.29): 设置了root密码,但是不问密码还是可以登录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44298160/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:20:58  来源:igfitidea点击:

Mysql (MariaDB 10.0.29): Set root password, but still can login without asking password?

mysqlloginpasswordsmariadb

提问by thanhpt

I want to secure mysql by setting root password. I reset root password successfully:

我想通过设置 root 密码来保护 mysql。我成功重置了root密码:

MariaDB [(none)]> select Host, User, Password from mysql.user;
+-----------+------+-------------------------------------------+
| Host      | User | Password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *58319282EAB9E38D49CA25844B73DA62C80C2ABC |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)

But, after flush privileges, restart Mysql, I can still login mysql (on local host) without typing password.

但是,刷新权限后,重新启动Mysql,我仍然可以在不输入密码的情况下登录mysql(在本地主机上)。

root@myhost:/# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.0.29-MariaDB SLE 12 SP1 package

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

How can I force mysql asking password when connect ? Thanks !

连接时如何强制mysql询问密码?谢谢 !

回答by Sakura Kinomoto

On MySQL table you have a column called plugin:

在 MySQL 表上有一个名为 plugin 的列:

MariaDB [(none)]> SELECT host, user, password, plugin FROM mysql.user LIMIT 0,1;
+-----------+------+-------------------------------------------+--------+
| host      | user | password                                  | plugin |
+-----------+------+-------------------------------------------+--------+
| localhost | root | *                                         |        |
+-----------+------+-------------------------------------------+--------+
1 row in set (0.00 sec)

If I remember correctly the default plugin for mariaDB installations are 'console' or 'unix_socket', and this plugin allows you to enter without password, from console. But also disable authentication with password, and you cannot connect from another clients.

如果我没记错的话,mariaDB 安装的默认插件是“console”或“unix_socket”,这个插件允许你从控制台输入而无需密码。但也要禁用密码身份验证,并且您无法从其他客户端连接。

Simply update the plugin field with empty ('') value, and then, use FLUSH PRIVILEGES;

只需使用空 ('') 值更新插件字段,然后使用 FLUSH PRIVILEGES;

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
UPDATE mysql.user SET plugin = '' WHERE user = 'root' AND host = 'localhost';
FLUSH PRIVILEGES;

With this, you have the problem solved.

有了这个,你就解决了问题。

回答by motherwell

I found I needed to change this:

我发现我需要改变这个:

UPDATE mysql.user SET plugin = 'auth_socket' WHERE user = 'root' AND host = 'localhost';

To

 UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND host = 'localhost';

That is the plugin for a password AFAICT.

那是密码 AFAICT 的插件。

回答by NCode

I know this question is old, but I had the same issue and the steps below solved my problem:

我知道这个问题很老,但我遇到了同样的问题,以下步骤解决了我的问题:

on the Linux terminal, run the command below to set a new password for root user, skip this step, if root password is already set.

在Linux终端,运行以下命令为root用户设置新密码,如果已经设置了root密码,则跳过这一步。

mysqladmin --user=root password "newpassword"

Login to mysql

登录mysql

mysql -uroot -p

Enter 'root' user password

输入“root”用户密码

Run following query on MariaDB console

在 MariaDB 控制台上运行以下查询

MariaDB [(none)]> select host,user,password from mysql.user;

You may notice some records with 'user' column values are ''(empty), delete those records using following query

您可能会注意到一些带有 'user' 列值为 ''(empty) 的记录,请使用以下查询删除这些记录

MariaDB [(none)]> delete from mysql.user where user='';
MariaDB [(none)]> flush privileges;
Query OK, 2 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
#$ mysql
ERROR 1045 (28000): Access denied for user ''@'localhost' (using password: NO)

You wont be able to login to mysql without password after following above steps.

按照上述步骤操作后,您将无法在没有密码的情况下登录mysql。