User Tools

Site Tools


reset-mysql-root-password

Reset MySQL root Password

You may need to reset the root password of the MySQL server.

First stop the MySQL server.

cap@CMAL-eabf:~$ sudo service mysql stop

Prepare a directory needed to manually start the MySQL server.

cap@CMAL-eabf:~$ sudo mkdir /var/run/mysqld
cap@CMAL-eabf:~$ sudo chown mysql /var/run/mysqld

Now lets start up the mysql daemon and skip the grant tables which store the passwords.

cap@CMAL-eabf:~$ sudo mysqld_safe --skip-grant-tables &

Now you should be able to connect to mysql without a password. Execute the following commands.

cap@CMAL-eabf:~$ mysql -u root -e "update user set Password=PASSWORD('new-password') where user='root';"
cap@CMAL-eabf:~$ mysql -u root -e "flush privileges;"

Kill the MySQL you started above. To do that you first need to know the relevant process IDs. Execute the following

cap@CMAL-eabf:~$ ps -ef | grep mysql
root      9708 7231  0 05:55 pts/0    00:00:00 sudo mysqld_safe --skip-grant-tables
root      9709  9708  0 05:55 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql    10160  9709  0 05:55 pts/0    00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/.data/RACHEL/mysql --plugin-d
ir=/usr/lib/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysql
d/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 --log-syslog=1 --log-syslog-facility=daemon --log-syslog-t
ag=
cap      10255  9681  0 05:59 pts/1    00:00:00 grep mysql

Above the process ID to kill are 9708, 9709 and 10160. You do this by executing the following.

cap@CMAL-eabf:~$ sudo kill -9 9708
cap@CMAL-eabf:~$ sudo kill -9 9709
cap@CMAL-eabf:~$ sudo kill -9 10160

If you execute the following again you should no longer see any MySQL server processes running like follows.

cap@CMAL-eabf:~$ ps -ef | grep mysql
cap      10389  9681  0 06:05 pts/1    00:00:00 grep mysql

Restart the MySQL server normally.

cap@CMAL-eabf:~$ sudo service mysql start

You should have reset the root password of the MySQL server.

reset-mysql-root-password.txt · Last modified: 2021/02/02 02:10 by 127.0.0.1