Tuesday, January 3, 2017

MySQL how to reset root password in Linux (Debian, Ubuntu, etc)

In case you've forgotten or lost your root password from MySQL server, there is a way to reset it. Below you will find detailed guide on how to do it in Linux. In particular, all instructions and commands were tested with Debian 8 and MySQL 5.5, but they should also work on most of other distributions. So let's get started.

1. Login as root
su -

2. Stop mysql service
systemctl stop mysql

3. Create sql file, which will be executed during next startup of MySQL service. Make sure that MySQL process has enough permissions to read that file. For example, create file /tmp/start.sql and put the following commands into it, by changing password in quotes:
use mysql;
update user set Password=PASSWORD("password") where User='root';
flush privileges;

4. Start mysql with your startup script, which will be executed:
mysqld_safe --init-file=/tmp/start.sql

Now you should be able to login as root using new password.

5. Remove startup file:
rm /tmp/start.sql

6. Stop mysql server:
kill `cat /var/run/mysqld/mysqld.pid`

Path to pid file may vary. And if you cant find pid file, you can also find pid of mysql by using this command, and then kill it:
ps aux|grep mysql
kill <pid_of_mysql>

7. Start mysql server in a regular way:
systemctl start mysql


No comments:

Post a Comment