97

I installed mysql through yum just now and the OS fedora installed mariadb for me. I know mariadb is a new branch of mysql, but I can't understand why it does not ask me for setting the password. I have tried for 123456 and so on, but I failed. My fedora is new, and this is the first time to install mysql/mariadb. What should I do for it?

Tony
  • 1,209
  • 1
  • 8
  • 11

13 Answers13

160

I had the same problem. It's true the password is empty, but even so the error message is shown. The solution is just using "sudo" so

$ sudo mysql

will open the mysql tool

For securing the database, you should use sudo again.

$ sudo mysql_secure_installation
Alex Angelico
  • 3,710
  • 8
  • 31
  • 49
74

From https://mariadb.com/kb/en/mysql_secure_installation/ :

In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

the password will be blank

I think that's your answer.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • 6
    Thank your for your answer, I have tried it, but it seems not work. – Tony Nov 28 '13 at 16:34
  • 6
    It shows: [lucups@localhost nginx]$ mysql -uroot -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) – Tony Nov 28 '13 at 16:35
  • I found that, the folder '/var/lib/mysql' does not exsit. – Tony Nov 28 '13 at 16:58
  • 1
    Wow. Yes, you have to leave the field blank, then press reset. THEN you are being presented with this mask to change user and pass. This is so screwed up by Synology. Thanks Floris for that answer! – atripes Jul 24 '17 at 22:43
  • 2
    Running mysql_secure_installation without sudo gave me an "incorrect password" error message when I just hit enter. Running under sudo did the trick for me. – tplive Jun 01 '21 at 06:35
52

mariadb uses by defaults UNIX_SOCKET plugin to authenticate user root. https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin/

"Because he has identified himself to the operating system, he does not need to do it again for the database"

so you need to login as the root user on unix to login as root in mysql/mariadb:

sudo mysql

if you want to login with root from your normal unix user, you can disable the authentication plugin for root.

Beforehand you can set the root password with mysql_secure_installation (default password is blank), then to let every user authenticate as root login with:

shell$ sudo mysql -u root
[mysql] use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
[mysql] \q
sebisnow
  • 1,671
  • 17
  • 26
redochka
  • 12,345
  • 14
  • 66
  • 79
  • 1
    This is my favorite way to let every user authenticate as root in mariadb – sebisnow Jan 03 '18 at 09:16
  • 1
    Note that this may break scripts used by your OS distribution, see [Resetting Authentication to Old Style (Password is Required)](https://mariadb.com/kb/en/library/authentication-plugin-unix-socket/#resetting-authentication-to-old-style-password-is-required) – return42 Sep 24 '18 at 16:30
  • Finally a solution that works. The mysql_secure_installation was impossible to run, as it was hanging on the "enter the current password" stage. Hitting just ENTER was not working at all. – Kouber Saparev Sep 02 '19 at 08:45
24

The default password is empty. More accurately, you don't even NEED a password to login as root on the localhost. You just need to BE root. But if you need to set the password the first time (if you allow remote access to root), you need to use:

sudo mysql_secure_installation

Enter empty password, then follow the instructions.

The problem you are having is that you need to BE root when you try to login as root from the local machine.

On Linux: mariadb will accept a connection as root on the socket (localhost) ONLY IF THE USER ASKING IT IS ROOT. Which means that even if you try

mysql -u root -p

And have the correct password you will be refused access. Same goes for

mysql_secure_installation

Mariadb will always refuse the password because the current user is not root. So you need to call them with sudo (or as the root user on your machine) So locally you just want to use:

sudo mysql

and

sudo mysql_secure_installation

When moving from mysql to mariadb it took a while for me to figure this out.

Stéphan Champagne
  • 1,259
  • 12
  • 12
16

Lucups, Floris is right, but you comment that this didn't solve your problem. I ran into the same symptoms, where mysql (mariadb) will not accept the blank password it should accept, and '/var/lib/mysql' does not exist.

I found that this Moonpoint.com page was on-point. Perhaps, like me, you tried to start the mysqld service instead of the mariadb service. Try:

systemctl start mariadb.service
systemctl status mysqld service

Followed by the usual:

mysql_secure_installation
Michael Greisman
  • 410
  • 4
  • 10
  • 2
    It's a long period of time from I run into this problem : ) I have given up the fedora Linux distribution and I am now using Ubuntu Server. I installed mysql by apt-get, and it let me set password during the installation process. Anyway, thanks very much for your answer! I think it would be helpful to others. – Tony Mar 16 '15 at 17:36
11

Had the same issue after installing mysql mariadb 10.3. The password was not NULL so simply pressing ENTER didn't worked for me. So finally had to change the password. I followed instruction from here. In nutshell; stop the server

sudo systemctl stop mariadb.service

gain access to the server through a backdoor by starting the database server and skipping networking and permission tables.

sudo mysqld_safe --skip-grant-tables --skip-networking &

login as root

sudo mysql -u root

then change server password

use mysql;
update user set password=PASSWORD("new_password_here") where User='root';

Note that after MySQL 5.7, the password field in mysql.user table field was removed, now the field name is 'authentication_string'. So use appropriate table name based on mysql version. finally save changes & restart the server

flush privileges;
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54
  • 2
    after doing this, if you want to be able to login from other places than *only* unix sockets, you'll also have to do ```UPDATE user SET plugin='' WHERE user = 'root';``` - the default "plugin" value is "unix_socket" which makes root only able to login via unix sockets. – hanshenrik Apr 08 '20 at 22:24
  • Just a comment about the comment above this...you should NEVER be logging in as the root user from anywhere except the unix socket: i.e. on the box itself. DBA 101: no remote root access! – Paul Allsopp May 02 '20 at 23:11
  • This answer is not working on newer installations of Mariadb, see https://stackoverflow.com/a/64841540/985366 – user985366 Dec 28 '22 at 00:16
8

If your DB is installed properly and typed the wrong password, the error thrown will be:

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

The following error indicates you DB hasn't been started/installed completely. Your command is not able to locate and talk with your DB instance.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

Good practice would be to change your password after a fresh install

$ sudo service mysql stop
$ mysqld_safe --skip-grant-tables &
$ sudo service mysql start
$ sudo mysql -u root

MariaDB [(none)]> use mysql;
MariaDB [mysql]> update user set password=PASSWORD("snafu8") where User='root';
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> exit;

$ sudo service mysql restart

OR

mysqladmin -u root password 'enter password here'
Dhruvan Ganesh
  • 1,502
  • 1
  • 18
  • 30
7

The default password for Mariadb is blank.

$ mysql -u root -p
Enter Password:    <--- press enter
slm
  • 15,396
  • 12
  • 109
  • 124
rahul kumar
  • 99
  • 1
  • 2
4

I hit the same issue and none of the solutions worked. I then bumped an answer in stackexchange dba which lead to this link. So here is what I did:

  1. ran sudo mysqld_safe --skip-grant-tables --skip-networking &
  2. ran sudo mysql -uroot and got into mysql console
  3. run ALTER USER root@localhost identified via unix_socket; and flush privileges; consecutively to allow for password-less login

If you want to set the password then you need to do one more step, that is running ALTER USER root@localhost IDENTIFIED VIA mysql_native_password; and SET PASSWORD = PASSWORD('YourPasswordHere'); consecutively.

UPDATE

Faced this issue recently and here is how I resolved it with recent version, but before that some background. Mariadb does not require a password when is run as root. So first run it as a root. Then once in the Mariadb console, change password there. If you are content with running it as admin, you can just keep doing it but I find that cumbersome especially because I cannot use that with DB Admin Tools. TL;DR here is how I did it on Mac (should be similar for *nix systems)

sudo mariadb-secure-installation

then follow instructions on the screen!

Hope this will help someone and serve me a reference for future problems

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
3

I believe I found the right answers from here.

GitHub Wnmp

So in general it says this:

user: root
password: password

That is for version 10.0.15-MariaDB, installed through Wnmp ver. 2.1.5.0 on Windows 7 x86

Matija
  • 17,604
  • 2
  • 48
  • 43
3

just switch your current logged-in user to the root and login without password mysql -uroot

Arash Mohammadi
  • 256
  • 3
  • 3
1

By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.

neildt
  • 5,101
  • 10
  • 56
  • 107
  • 3
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' – Tony Nov 28 '13 at 16:46
1

For me, password = admin, worked. I installed it using pacman, Arch (Manjaro KDE).

NB: MariaDB was already installed, as a dependency of Amarok.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133