How to back up and restore MySQL database

To backup a mysql database to a file use the following syntax:
mysqldump -u[username] -p[password] nameofdatabaseĀ > nameofdatabase.sql
To restoreĀ from the backup use the following method:
mysql -u root -p
password: <type password> <enter>
> create database <nameofdatabase>; <enter>
> grant all on nameofdatabase.* to nameofuser@localhost identified by ‘nameofuserpassword’;
> flush privileges;
> exit;
mysql -u root -p nameofdatabase < nameofdatabase.sql
password: <type password> <enter>