Recover the MySQL root password.

To recover the MySQL root Password, please follow these steps:

1. Stop the MySQL server process.

root# /etc/init.d/mysql stop

2. Start the MySQL process with the –skip-grant-tables option, so that it will not prompt for a password.

root# mysqld_safe --skip-grant-tables &

root@mad:/home/user# mysqld_safe –skip-grant-tables &
[1] 23126
root@mad:/home/user# 140729 11:09:49 mysqld_safe Logging to syslog.
140729 11:09:49 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

3. Connect to a MySQL as the root user.

root# mysql -u root

Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 9
Server version: 5.5.38-0+wheezy1-log (Debian)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.
mysql>

4. Set the new password for the MySQL root account.

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-PASSWORD") where user='root';
mysql> flush privileges;
mysql> quit

5. Exit and then stop and start or restart the MySQL server.

root# /etc/init.d/mysql restart

How to backup and restore a MySQL database.

The mysqldump command creates a text version of the database. Specifically, it creates a list of SQL statements which can be used to restore/recreate the original database.
The syntax is:
# mysqldump -u root -p[root_password] [database_name] > backup.sql – backup.
# mysql -u root -p[root_password] [database_name] < backup.sql - restore.

Back up a single database:
The mysqldump command will backup the mydb database and dumps the output to the mydb.sql file

# mysqldump -u root -pmysqlpassword mydb > mydb.sql

Actually, if you don't provide a password at this stage, later you will be prompted for it.

Back up the multiple databases:

# mysqldump -u root -pmysqlpassword --databases mydb somedb > mydb_somedb.sql

Back up all of the databases:


# mysqldump -u root -pmysqlpassword --all-databases > all_databases.sql

Back up a specific table:
In this example, we takes a back up only of the users table from the mydb database.

# mysqldump -u root -pmysqlpassword mydb users > mysb_table_users.sql

To restore the MySQL database from a backup:
Execute the mysql command with "<" as shown below. If you are going to restore the mydb.sql to a different server, the mydb database must be created on this server.
# mysql -u root -pmysqlpassword

mysql> create database mydb;
Query OK, 1 row affected (0.02 sec)

# mysql -u root -pmysqlpassword mydb < mydb.sql

If you are trying to restore a single database from dump of all the databases, you have to let mysql know like this:

mysql --one-database database_name < all_databases.sql