2

I'm trying to make a copy of a database so I can send it as to someone else as a file so they can use the copy. I'm trying to use mysqldump for this, I'm looking at the documentation but I can't get it to work.

I'm just trying to copy a database called test2 right now. In the MySQL command line client I'm doing mysqldump test2 > dump.sql; I'm getting the error message: You have an error in your SQL suntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump test2 > dump.sql' at line 1.

Go on, tell me I'm stupid. I just need some help, please.

MitchCool1
  • 329
  • 3
  • 14

1 Answers1

3

I use the following from the Unix command line to back up and recreated databases:

  1. Back up a database:

General:

$ mysqldump -u<username> -p<password> -h<hostname> <database> | gzip - > <database>.dump.<datestamp>.gz

Example:

$ mysqldump -usandro -psecrete -hlocalhost books | gzip - > books.dump.2015-06-20.gz
  1. Load a database from backup

    $ gunzip -c books.dump.2015-06-20.gz | mysql -usandro2 -psecrete2 -host2

gzip and gunzip are used to compress and uncompress the backup file to save space. The - and the -c are used for stdin and stdout. Normally gzip and gunzip work on files.

The loading example will create the database should it not exist. If the database already exists, the loading will replace all existing tables with new ones.

dlink
  • 1,489
  • 17
  • 22
  • In the cmd, do you have to run the `$ mysqldum...` from a specific directory? I've tried running the windows version of that code in the cmd, just in my users account `C:\Users\Mitch`, and I get errors. – MitchCool1 Jun 20 '15 at 16:15
  • You do not need to run it from any spec. directory - but the mysqldump command needs to be in your path. What errors are you getting? – dlink Jun 24 '15 at 14:57