MariaDB installed without password prompt

35,052

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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/

Share:
35,052

Related videos on Youtube

mtoloo
Author by

mtoloo

Updated on November 26, 2020

Comments

  • mtoloo
    mtoloo over 3 years

    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

    • arkascha
      arkascha almost 9 years
      This is not a question of the installation. Those servers always create the root account without password. You have to internally change it as documented. About the workbench or similar failing: whyever that is, but the problem will be solved the moment you have set a password for the account which you have to do anyway.
    • mtoloo
      mtoloo almost 9 years
      @arkascha: According to this manual, mariadb will ask for root password at installation time: tecadmin.net/install-mariadb-10-on-ubuntu
    • arkascha
      arkascha almost 9 years
      I don't know much about Ubuntu, I prefer other distributions, so I cannot say anything specific. Certainly such thing is possible, I have never seen it though it probably makes sense. Anyway: that is not your issue, is it? Your issue is that your root account apparently has no password set. Did you check that inside the internal mysql table? If so, then just set a password and all is fine.
    • mtoloo
      mtoloo almost 9 years
      I've set the password with three different ways: using set password or update user commands then flush privileges inside mysql. Using mysqladmin command. Using mysql_secure_installation. But None of them fixed the problem. I am still able to connec to mysql from command line without password, but mysql-workbench can not connect.
  • ρss
    ρss over 8 years
    Please add some explanation to your answer!
  • marshall
    marshall almost 8 years
    Mysql tries to authenticate root using plugin, not password. You need just disable plugin usage for root.
  • user1283068
    user1283068 over 7 years
    This advice saved me - thanks a lot. I can't believe this isn't in the official documenation, or in any other guides that describe how to reset the root password.
  • nrvx
    nrvx about 7 years
    On Ubuntu 16.04, I had an "SQL Syntax Error" using this code given by Aashish: >update user set plugin='' where User='root'; In place, I used that to overwrite authentication method : > update user set plugin="mysql_native_password";
  • deviato
    deviato almost 7 years
    also on debian 9 stretch, you have to use "update user set plugin='mysql_native_password' ..."
  • Pedru
    Pedru over 6 years
    explanation can be found here
  • DeltaFlyer
    DeltaFlyer over 4 years
    If you're just dabbling around in a development environment, do you need to configure mysql_secure_installation?
  • jfgiraud
    jfgiraud over 3 years
    The ALTER USER statement was introduced in MariaDB 10.2.0.