Getting initial root permissions
/!\ Note! I assume that all actions are conducted as a superuser or via sudo command.
- Find out your DB version:
#mysql --version - Stop DB server
#service mysql stop (or mariadb) - Start DB instance in safe mode (yeah, there goes security...)
#mysqld_safe --skip-grant-tables --skip-networking & - Login as root
IMPORTANT NOTE: In newer versions of mysql you can login as root into database if you are under superuser system account. Which means: you have to be root in system, to get mysql root shell.
#mysql -u root - Flush priviliges and alter permissions
mysql> FLUSH PRIVILIGES;
MySQL 5.7.6+ and MariaDB 10.1.20+:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
Prior MySQL and MariaDB versions:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password'); - Due to restricted usage of root in newer versions, I adwise to create admin user:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES; - Kill mysqld_safe process
Find PID in /var/run/mysql/mysql.pid or via ps aufx
Ensure no mysql survives :))) - Restart mysql (of course you can simply use "star", but I prefer that way)
#service mysql restart
Another way, that just might help.
- Open /etc/mysql/my.cnf (or any other effective config)
- under
[mysqld]
add
skip-grant-tables - #service restart mysql
- #mysql -u root
- mysql>UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass') WHERE User = 'root' AND Host = 'localhost';
- /!\ Remove skip-grant-tables from my.cnf
- #service restart mysql