change root password code example

Example 1: change mysql root password

$ sudo cat /etc/mysql/debian.cnf
Note the lines which read:

user     = debian-sys-maint
password = blahblahblah
Then:

$ mysql -u debian-sys-maint -p
Enter password: // type 'blahblahblah', ie. password from debian.cnf

mysql> USE mysql
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| root             | localhost | auth_socket           |
| mysql.session    | localhost | mysql_native_password |
| mysql.sys        | localhost | mysql_native_password |
| debian-sys-maint | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)

mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> COMMIT;  // When you don't have auto-commit switched on
Either:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
Or:

// For MySQL 5.7+
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') where user='root';
Then:

mysql> FLUSH PRIVILEGES;
mysql> COMMIT;  // When you don't have auto-commit switched on
mysql> EXIT

$ sudo service mysql restart
$ mysql -u root -p
Enter password: // Yay! 'new_password' now works!

Example 2: replace password root

Step 1: Open a Terminal Window.
    		Right-click the desktop, then left-click Open in terminal.
            Alternately, you can click Menu > Applications > Accessories > Terminal.
    Step 2: Change Your Root Password.
    		In the terminal window, type the following:
            	- sudo passwd root.

Example 3: how to get root password

How to change root password in Ubuntu
The procedure to change the root user password on Ubuntu Linux:

Type the following command to become root user and issue passwd:
sudo -i
passwd
OR set a password for root user in a single go:
sudo passwd root
Test it your root password by typing the following command:
su -