MySQL backup and restore from command line

Dragan Eror's picture
mysql logo

Backup

Simple backup
mysqldump --opt -u[user] -p[pass] [database] > [filename].sql
  • [user] Your database username
  • [pass] The password for your database (note there is no space between -p and the password)
  • [database] The name of your database
  • [filename].sql The filename for your database backup
  • [--opt] The mysqldump option
Backup just few tables
mysqldump -u[user] -p [database] [table_a] [table_b] > [filename].sql
Backup more databases
mysqldump -u[user] -p --databases [db_a] [db_b] [db_c] > [filename].sql
Backup all database from mysql
mysqldump -u[user] -p --all-databases > [filename].sql

The mysqldump command has also some other useful options:

  • Tells MySQL to add a DROP TABLE statement before each CREATE TABLE in the dump.
    --add-drop-table
  • Dumps only the database structure, not the contents.
    --no-data
  • Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.
    --add-locks
Simple backup with compression
mysqldump -u[user] -p [database] | gzip -9 > [filename].sql.gz
Extract
gunzip [filename].sql.gz

Restore

Simple restore
mysql -u[user] -p [database] < [filename].sql
Restore compressed
gunzip < [filename].sql.gz | mysql -u[user] -p [database]
Import to existing database
mysqlimport -u[user] -p [database] [filename].sql

Comments

Anonymous's picture

MySQL export/import

Evo jedan trik kojeg ja svakodnevno koristim kada zelim da sinhronizujem lokalnu bazu s onom na serveru:

ssh user@server mysqldump --opt -uuser -ppass baza | mysql -uuser -ppass baza

Tadaaa!

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.