Home » Web development » mysql » How to reset MySQL root password on your Linux server

How to reset MySQL root password on your Linux server

We may sometimes need to reset the MySQL root password on your Linux server. Here are the steps to do that

We need the root user privilege to do this

Step1 Login as root user and Stop the MySQL server

/etc/init.d/mysql stop
or
service mysql stop

Step 2 Start MySQL in safe mode and skip the use of the “grant tables”

/usr/bin/mysqld_safe --user=mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --datadir=/var/lib/mysql --skip-grant-tables --skip-networking &
or
/usr/bin/mysqld_safe --skip-grant-tables &

Step 3 Reset the MySQL root password

mysql -u root
use mysql;
update user set authentication_string=PASSWORD("newpass") where User='root';
flush privileges;

or

mysqladmin -u root flush-privileges password newpass

or

mysql -u root
use mysql;
SET PASSWORD FOR root@'localhost' = PASSWORD('newpass');
flush privileges;

Step 4 Stop the MySQL service running in safe mode

kill `cat /var/run/mysqld/mysqld.pid`

or

/etc/init.d/mysql stop

or

/etc/init.d/mysqld stop

Step 5 Start the Mysql service

/etc/init.d/mysql start 
or 
service mysql start
or
/etc/init.d/mysqld start

Step 6 login with the new password

mysql -u root -p

Related Articles

MySQL queries
MySQL COALESCE and NULLIF Function
How to start and stop mysql on Linux

See also  How to use if else condition in php

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top