At first you need to stop the service.
sudo systemctl stop mariadb
Then you need to restart the service without permission checking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
Note the returned process ID.
Then we can log in without a password.
mysql -u root
Load up the grant tables with:
FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
Then set the new password.
ALTER USER 'root'@'localhost' IDENTIFIED BY 'cool_password';
Query OK, 0 rows affected (0.01 sec)
Exit the MariaDB with:
exit;
Now kill the process you started in the background. Use the process ID you noted earlier instead of 1234.
sudo kill -9 1234
Then start up MariaDB and check if you can log in with your cool new password.
sudo systemctl start mariadb
mysql -u root -p