Friday, March 18, 2016

Export a MySQL database from Windows Command Prompt (mysqldump)

Some how my PhpmyAdmin stopped working And I had to take up a back up of one of my databases. So that time, I thought of using the Mysql Command prompt service.

Why use mysqldump (and where to run it)

To take the back up, we can export the tables and contents of the database using mysqldump. Please note that mysqldump is not a command inside the mysql interactive prompt — it's a separate utility installed with MySQL. If you try to run it from the mysql> prompt you'll get an error like this:

mysqldump error screenshot

You need to run mysqldump from a regular command prompt (cmd.exe on Windows), not from inside mysql.

Locate mysqldump.exe

On Windows, mysqldump.exe is in MySQL's "bin" directory inside the MySQL installation folder. For example:

MySQL bin folder screenshot

You can either:

  • open Command Prompt and change directory into that bin folder, e.g.:
cd "C:\Program Files\MySQL\MySQL Server X.Y\bin"
  • or add the bin folder to your PATH environment variable so mysqldump can be run from any folder:
setx PATH "%PATH%;C:\Program Files\MySQL\MySQL Server X.Y\bin"

(Replace X.Y with your MySQL version and restart your command prompt after modifying PATH.)

Basic export command (example)

Change to the directory where you want the dump file saved (or use an absolute path in the redirection). Then run:

mysqldump -u root -p mrms >mrms.sql

Here:

  • -u root specifies the MySQL user
  • -p tells mysqldump to prompt for the password (safer than passing it on the command line)
  • mrms is the database name
  • >mrms.sql redirects the SQL output into a file named mrms.sql in the current directory

After entering the root password when prompted, mrms.sql is generated in that directory. Example confirmation screenshot:

dump file created screenshot

Additional useful examples

  • Export a single table from a database:
mysqldump -u root -p mrms table_name >table_name.sql
  • Export all databases:
mysqldump -u root -p --all-databases >alldb.sql
  • Export a specific database and include routines/triggers:
mysqldump -u root -p --routines --triggers mrms >mrms_with_routines.sql
  • For large InnoDB databases to get a consistent dump without locking tables, use single-transaction (works for InnoDB only):
mysqldump -u root -p --single-transaction mrms >mrms.sql
  • Export from a remote host:
mysqldump -h remote_host -u user -p database >database.sql
  • Compress the dump on the fly (Windows with gzip available, or use 7-Zip):
mysqldump -u root -p mrms | gzip >mrms.sql.gz

Security notes

  • Do not put the password directly in the command line (e.g. --password=secret) on multi-user systems — it may be visible in process lists or command histories. Use -p to be prompted for the password.
  • Store dump files securely and limit access (they contain all data and possibly sensitive information).

How to restore a dump

To restore the dump into a database:

mysql -u root -p mrms < mrms.sql

If the database does not exist, create it first or use the --databases option when dumping (which includes CREATE DATABASE statements):

mysqldump -u root -p --databases mrms >mrms_with_create.sql

Troubleshooting

  • "'mysqldump' is not recognized": you are not in the MySQL bin directory and it isn't on PATH — cd to bin or add it to PATH.
  • Permission denied when creating the .sql file: change to a writable directory or run the command prompt as a user with write permissions for that folder.
  • Connection errors: ensure MySQL server is running and the credentials are correct. For remote hosts, ensure network access and firewall rules allow the connection.
  • If the dump is incomplete or fails, check the mysqldump output for error messages and ensure you have sufficient disk space.

Summary

That's it. Happy Coding.

No comments: