Taking MySQL database backup with mysqldump

Open the command line, navigate to the directory where MySQL is installed. Further, navigate to the directory 'bin' inside the MySQL directory and enter the following command to take the dump of a MySQL database.


mysqldump -u USER -p -h --databases DATABASENAME > OUTPUTTEXT.sql

In my case, the directory structure is C:\mysql\bin>

C:\mysql\bin>mysqldump -u root -p -h --databases exam > exam.sql
Enter Password:

You will be prompted to enter a password for this user. Enter the password or leave it blank in case there's no password.

The parameters in this command are -u for user, -p for password for the user, -h for the host which is localhost in our case ( hence we gave the IP which corresponds to our localhost), --databases for the database name. And finally the last part after '>' is the full path to where our sql statement dump file will reside. We can give the filename as desired.

If no path to the dump file is provided and just the filename, the database dump will be generated in the same directory you are in (Inside 'bin' in my case).

Incase you entered an incorrect command or an incorrect password, you may get an error similar to the one below:


mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: YES)
when trying to connect

And make sure the user has admin privileges (In my case, it's the root user itself).