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.
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.
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
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
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.
"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
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.
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
ran sudo mysqld_safe --skip-grant-tables --skip-networking &
ran sudo mysql -uroot and got into mysql console
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