Recommended method:
- Create file
/var/lib/mysql/mysql.initwith contents:
sh
SET PASSWORD FOR 'root'@'localhost'=PASSWORD('mynewpassword');
- Add this line to
/etc/my.cnfin the [mysqld] section:
sh
init_file=/var/lib/mysql/mysql.init
- Restart the MySQL service
- At your leisure, delete the
mysql.initfile and remove theinit_fileline from/etc/my.cnf
With MARIADB
Source: https://stroobants.io/reset-a-root-password-in-mariadb-on-linux/
sh
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