Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Reset/Set the MySQL Root Password

refer here
refer here
how-to-change-mysql-root-password-on-linux

Reset the MySQL Root Password

If you forget your root MySQL password, it can be reset.

Stop the current MySQL server instance, then restart it with an option to not ask for a password.

1.sudo systemctl stop mysqld
2.sudo mysqld_safe --skip-grant-tables &
Enter fullscreen mode Exit fullscreen mode

Reconnect to the MySQL server with the MySQL root account.
1.mysql -u root
Use the following commands to reset root’s password. Replace password with a strong password.

1.use mysql;
2.update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
3.flush privileges;
4.exit
Enter fullscreen mode Exit fullscreen mode

Then restart MySQL.

1.sudo systemctl start mysqld
Enter fullscreen mode Exit fullscreen mode

How to Reset MySQL or MariaDB Root Password

Follow these steps to reset your MySQL/MariaDB root password:

Stop the MySQL/MariaDB service

To change the root password first, you need to stop the MySQL server. To do so type the following command:

$ sudo systemctl stop mysql
Enter fullscreen mode Exit fullscreen mode

Start the MySQL/MariaDB server without loading the grant tables

Start the database server without loading the grant tables:

$ sudo mysqld_safe --skip-grant-tables &
Enter fullscreen mode Exit fullscreen mode

The ampersand & at the end of the command above will cause the program to run in the background , so you can continue to use the shell.

When the --skip-grant-tables option is used, anyone can to connect to the database server without a password and with all privileges granted.

Log in to the MySQL shell

Now you can connect to the database server as the root user:

$ mysql -u root
Enter fullscreen mode Exit fullscreen mode

Set a new root password

Run the following commands if you run MySQL 5.7.6 and later or MariaDB 10.1.20 and later:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MY_NEW_PASSWORD';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Step 1: First set MYSQL environment variable in Ubuntu
Step 2:login using username and password
In root vi .bashsrc

Image description

Image description

Image description

Image description

export PATH=$PATH:/opt/lampp/bin
Enter fullscreen mode Exit fullscreen mode

Image description
====================or===================

other than root user

Image description

ALTER USER 'root'@'localhost' IDENTIFIED BY 'pws?~,F(?Fkdfgbdfb';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

====================OR=======================

ALTER USER 'wizbrandos'@'localhost' IDENTIFIED BY 'myphpmyadminpassword';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Set db name,username and pwd

Image description

If ALTER USER statement doesn’t work for you, try to modify the user table directly:

UPDATE mysql.user SET authentication_string = PASSWORD('MY_NEW_PASSWORD')
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Set db name,username and pwd

Image description
Run the following commands if you have MySQL 5.7.5 and earlier or MariaDB 10.1.20 and earlier:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MY_NEW_PASSWORD');
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

In both cases if all goes well, you should see the following output:

5. Stop and Start the database server normally

Now that the root password is set, stop the database server and start it normally:

mysqladmin -u root -p shutdown
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter the new root password.

Start the database server normally:

For MySQL, type:

sudo systemctl start mysql
Enter fullscreen mode Exit fullscreen mode

For MariaDB, type:

sudo systemctl start mariadb
Enter fullscreen mode Exit fullscreen mode

6. Verify the password

To verify that the new root password has been applied correctly, type:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Step 1: First set MYSQL environment variable in Ubuntu
Step 2:login using username and password
In root vi .bashsrc

Image description

Image description

Image description

Image description

export PATH=$PATH:/opt/lampp/bin
Enter fullscreen mode Exit fullscreen mode

Image description
====================or===================

other than root user

Image description

ALTER USER 'root'@'localhost' IDENTIFIED BY 'pws?~,F(?Fkdfgbdfb';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

====================OR=======================

ALTER USER 'wizbrandos'@'localhost' IDENTIFIED BY 'myphpmyadminpassword';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter the new root password. Enter it, and you should be logged in to your database server.

If the ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:

UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)