安装 MariaDB 没有密码提示
MariaDB installed without password prompt
我已经使用 Ubuntu 软件中心或在命令提示符下(apt-get install maraidb-server)从 Ubuntu 15.04 存储库安装了 mariadb,但没有要求 root 用户输入密码.
现在我可以在命令行上连接到 mysql 而无需密码,但是使用 Mysql-Workbench 或 python mysqldb 库连接失败并显示 "Access denied for user 'root'@'localhost'" 留言
Starting with MariaDB 10.4 root@localhost
创建的帐户可以使用两个身份验证插件:
- First, it is configured to try to use the
unix_socket
authentication plugin. This allows the root@localhost
user to login without a password via the local Unix socket file defined by the socket system variable, as long as the login is attempted from a process owned by the operating system root
user account.
- Second, if authentication fails with the
unix_socket
authentication plugin, then it is configured to try to use the mysql_native_password
authentication plugin. However, an invalid password is initially set, so in order to authenticate this way, a password must be set with SET PASSWORD
.
这就是为什么您无需密码即可在全新安装时登录的原因。
但是 another quote:
When the plugin column is empty, MariaDB defaults to authenticating accounts with either the mysql_native_password
or the mysql_old_password
plugins. It decides which based on the hash used in the value for the Password column. When there's no password set or when the 4.1 password hash is used, (which is 41 characters long), MariaDB uses the mysql_native_password
plugin. The mysql_old_password
plugin is used with pre-4.1 password hashes, (which are 16 characters long).
因此设置 plugin = ''
将强制它使用基于密码的身份验证。请务必在此之前设置密码。
sudo mysql -u root
[mysql] use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
[mysql] \q
sudo mysql -u root
[mysql] use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
[mysql] \q
这需要跟随以下命令
# mysql_secure_installation
如果从 localhost 访问,root 具有无密码访问是很常见的,我建议保留此设置。
我还建议您创建一个权限较低的用户,并允许该用户远程登录。
create user my_admin identified by '12345';
create database my_database;
grant all on my_database.* to my_admin;
这样你就多了一点安全感。
如果您确实需要从 workbench 等工具以 root 用户身份连接,您可以配置这些工具以创建 ssh 隧道并以本地主机身份连接到数据库。
正如@Pedru 所注意到的,"Access denied for user 'root'@'localhost'" 消息是由于 Debian and Ubuntu enable the UNIX_SOCKET Authentication Plugin plugin by default, allowing passwordless login (See also Authentication Plugin - Unix Socket)。这不是安装问题。
这意味着如果你在Linux Shell中输入mysql -u root -p
,root
实际上是Linux根(或链接到它,我不知道这实际上是如何工作的)。因此,如果您使用其他帐户登录 Linux,您将收到错误消息:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
。如果尚未定义密码,最好输入 sudo mysql -u root -p
或 sudo mysql -u root
。
如果你想切换到mysql_native_password认证插件,那么你可以使用
ALTER USER root@localhost IDENTIFIED VIA mysql_native_password;
SET PASSWORD = PASSWORD('new_password');
更多信息见
https://mariadb.com/kb/en/authentication-plugin-unix-socket/
对于10.2.0以上的MariaDB版本,的回答是正确的。
对于低于 10.2.0 的 MariaDB 版本,您可以这样做:
me$ sudo su -
root$ mysql -u root
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在另一个终端,
me$ mysql -u root -pnew_password
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 83
Server version: 10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
我已经使用 Ubuntu 软件中心或在命令提示符下(apt-get install maraidb-server)从 Ubuntu 15.04 存储库安装了 mariadb,但没有要求 root 用户输入密码. 现在我可以在命令行上连接到 mysql 而无需密码,但是使用 Mysql-Workbench 或 python mysqldb 库连接失败并显示 "Access denied for user 'root'@'localhost'" 留言
Starting with MariaDB 10.4 root@localhost
创建的帐户可以使用两个身份验证插件:
- First, it is configured to try to use the
unix_socket
authentication plugin. This allows theroot@localhost
user to login without a password via the local Unix socket file defined by the socket system variable, as long as the login is attempted from a process owned by the operating systemroot
user account.- Second, if authentication fails with the
unix_socket
authentication plugin, then it is configured to try to use themysql_native_password
authentication plugin. However, an invalid password is initially set, so in order to authenticate this way, a password must be set withSET PASSWORD
.
这就是为什么您无需密码即可在全新安装时登录的原因。
但是 another quote:
When the plugin column is empty, MariaDB defaults to authenticating accounts with either the
mysql_native_password
or themysql_old_password
plugins. It decides which based on the hash used in the value for the Password column. When there's no password set or when the 4.1 password hash is used, (which is 41 characters long), MariaDB uses themysql_native_password
plugin. Themysql_old_password
plugin is used with pre-4.1 password hashes, (which are 16 characters long).
因此设置 plugin = ''
将强制它使用基于密码的身份验证。请务必在此之前设置密码。
sudo mysql -u root
[mysql] use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
[mysql] \q
sudo mysql -u root
[mysql] use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
[mysql] \q
这需要跟随以下命令
# mysql_secure_installation
如果从 localhost 访问,root 具有无密码访问是很常见的,我建议保留此设置。
我还建议您创建一个权限较低的用户,并允许该用户远程登录。
create user my_admin identified by '12345';
create database my_database;
grant all on my_database.* to my_admin;
这样你就多了一点安全感。
如果您确实需要从 workbench 等工具以 root 用户身份连接,您可以配置这些工具以创建 ssh 隧道并以本地主机身份连接到数据库。
正如@Pedru 所注意到的,"Access denied for user 'root'@'localhost'" 消息是由于 Debian and Ubuntu enable the UNIX_SOCKET Authentication Plugin plugin by default, allowing passwordless login (See also Authentication Plugin - Unix Socket)。这不是安装问题。
这意味着如果你在Linux Shell中输入mysql -u root -p
,root
实际上是Linux根(或链接到它,我不知道这实际上是如何工作的)。因此,如果您使用其他帐户登录 Linux,您将收到错误消息:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
。如果尚未定义密码,最好输入 sudo mysql -u root -p
或 sudo mysql -u root
。
如果你想切换到mysql_native_password认证插件,那么你可以使用
ALTER USER root@localhost IDENTIFIED VIA mysql_native_password;
SET PASSWORD = PASSWORD('new_password');
更多信息见 https://mariadb.com/kb/en/authentication-plugin-unix-socket/
对于10.2.0以上的MariaDB版本,
对于低于 10.2.0 的 MariaDB 版本,您可以这样做:
me$ sudo su -
root$ mysql -u root
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在另一个终端,
me$ mysql -u root -pnew_password
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 83
Server version: 10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>