Ugh- nothing worked for me! I have a CentOS 7.4 machine running mariadb 5.5.64.
What I had to do was this, right after installation of mariadb from yum;
# systemctl restart mariadb
# mysql_secure_installation
The mysql_secure_installation
will take you through a number of steps, including "Set root password? [Y/n]". Just say "y" and give it a password. Answer the other questions as you wish.
Then you can get in with your password, using
# mysql -u root -p
It will survive
# systemctl restart mariadb
The Key
Then, I checked the /bin/mysql_secure_installation
source code to find out how it was magically able to change the root password and none of the other answers here could. The import bit is:
do_query "UPDATE mysql.user SET Password=PASSWORD('$esc_pass') WHERE User='root';"
...It says SET Password=...
and not SET authentication_string = PASSWORD...
. So, the proper procedure for this version (5.5.64) is:
login using mysql -u root -p , using the password you already set.
Or, stop the database and start it with:
mysql_safe --skip-grant-tables --skip-networking &
From the mysql> prompt:
use mysql;
select host,user,password from user where user = 'root';
(observe your existing passwords for root).
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root' AND host = 'localhost';
select host,user,password from user where user = 'root';
flush privileges;
quit;
kill the running mysqld_safe. restart mariadb. Login as root: mysql -u -p
. Use your new password.
If you want, you can set all the root passwords at once. I think this is wise:
mysql -u root -p
(login)
use mysql;
select host,user,password from user where user = 'root';
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root';
select host,user,password from user where user = 'root';
flush privileges;
quit;
This will perform updates on all the root passwords: ie, for "localhost", "127.0.0.1", and "::1"
In the future, when I go to RHEL8 or what have you, I will try to remember to check the /bin/mysql_secure_installation and see how the guys did it, who were the ones that configured mariadb for this OS.