Recommended method:
1) Create file /var/lib/mysql/mysql.init with contents:
SET PASSWORD FOR 'root'@'localhost'=PASSWORD('mynewpassword');
2) Add this line to /etc/my.cnf in the [mysqld] section:
init_file=/var/lib/mysql/mysql.init
3) Restart the MySQL service
4) At your leisure, delete the mysql.init file and remove the init_file line
from /etc/my.cnf
With MARIADB
Source: https://stroobants.io/reset-a-root-password-in-mariadb-on-linux/
service mysql stop mysqld_safe --skip-grant-tables --skip-networking & # mysql use mysql; update user set password=PASSWORD("<PASSWORD>") where User='root' flush privileges; exit service mysql restart