Saturday, April 7, 2018

Reset MySQL Root Password

-----
Basic step to reset mysqld password just follow these instructions :
  • Stop the mysql demon process
    •    sudo /etc/init.d/mysql stop
  • Start the mysqld demon process using the --skip-grant-tables option
    •    sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
      or
         sudo mysqld_safe --skip-grant-tables &
       
  • start the mysql client process using this command
    •    mysql -u root
  • from the mysql prompt execute this command to be able to change any password
    •    FLUSH PRIVILEGES;
  • Then reset/update your password
    •    SET PASSWORD FOR root@'localhost' = PASSWORD('password');
  • If you have a mysql root account that can connect from everywhere, you should also do:
    •    UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
  • Alternate Method:
    •    USE mysql
         UPDATE user SET Password = PASSWORD('newpwd')
         WHERE Host = 'localhost' AND User = 'root';
  • And if you have a root account that can access from everywhere:
    •    USE mysql
         UPDATE user SET Password = PASSWORD('newpwd')
         WHERE Host = '%' AND User = 'root';
For either method, once have received a message indicating a successful query (one or more rows affected), flush privileges:
   FLUSH PRIVILEGES;

Then stop the mysqld process and relaunch it
   sudo /etc/init.d/mysql stop
   sudo /etc/init.d/mysql start
 

-- Alternative method for different source
 
1. Run bash commands
 

# 1. first, run these bash commands
sudo /etc/init.d/mysql stop # stop mysql service
sudo mysqld_safe --skip-grant-tables & # start mysql without password
# enter -> go
mysql -uroot # connect to mysql

2. Run mysql commands

use mysql; # use mysql table
update user set authentication_string=PASSWORD("") where User='root'; # update password to nothing
update user set plugin="mysql_native_password" where User='root'; # set password resolving to default mechanism for root user

flush privileges;
quit;

3. Run more bash commands

sudo /etc/init.d/mysql stop 
sudo /etc/init.d/mysql start # reset mysql 
mysql -u root -p root # try login to database
 
 

-- If you find following error

2017-02-10T17:05:44.870970Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-02-10T17:05:44.872874Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-02-10T17:05:44.874547Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists. 
 
Do this 
$ mkdir -p /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld

To reset 
Mysqld_safe 
Solving Error 
 

No comments:

Post a Comment