简体   繁体   中英

MariaDB installed without password prompt

I've installed mariadb from Ubuntu 15.04 repositories using the Ubuntu software center or at the command prompt (apt-get install maraidb-server), but no password is asked for root user. Now I'm able to connect to mysql on command line without password, but connecting using Mysql-Workbench or python mysqldb library failed with the "Access denied for user 'root'@'localhost'" message

Starting with MariaDB 10.4 root@localhost account is created with the ability to use two authentication plugins:

  • 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 .

That is why you don't need a password to login on a fresh install.

But then 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).

So setting plugin = '' will force it to use password based authentication. Make sure you set a password before that.

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

This needs to be followed by following command

# mysql_secure_installation

it is common for root to have password-less access if accessing from localhost, I recommend this setting to be left alone.

I also recommend that you create a user with less permissions and allow that user to login remotely.

create user my_admin identified by '12345';
create database my_database;
grant all on my_database.* to my_admin;

This way you have a little more security.

If you do need to connect as root from a tool like workbench, you can configure those tools to create an ssh tunnel and connect to the database as localhost.

As @Pedru noticed, the "Access denied for user 'root'@'localhost'" message is due to the fact that Debian and Ubuntu enable the UNIX_SOCKET Authentication Plugin plugin by default , allowing passwordless login (See also Authentication Plugin - Unix Socket ). This is not an installation problem.

It means that if you type mysql -u root -p in the Linux Shell, root is actually the Linux root (or linked to it, I don't know how this actually works). So that if you logged on Linux with another account, you will get an error: ERROR 1698 (28000): Access denied for user 'root'@'localhost' . Better type sudo mysql -u root -p or sudo mysql -u root if the password is not yet defined.

If you want to switch to the mysql_native_password authentication plugin, then you could use

ALTER USER root@localhost IDENTIFIED VIA mysql_native_password;
SET PASSWORD = PASSWORD('new_password');

For further information see https://mariadb.com/kb/en/authentication-plugin-unix-socket/

For a MariaDB version greater than or equals to 10.2.0, the response of user3054067 is correct.

For a MariaDB version less than 10.2.0, you can do this :

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)

In another terminal,

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)]> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM