Home » Mysql » How to use MySQL dump from a remote machine

How to use MySQL dump from a remote machine

Posted by: admin November 1, 2017 Leave a comment

Questions:

How can I backup a mysql database which is running on a remote server, I need to store the back up file in the local pc.

Answers:

Try it with Mysqldump

#mysqldump --host=the.remotedatabase.com -u yourusername -p yourdatabasename > /User/backups/adump.sql

Questions:
Answers:

Have you got access to SSH?

You can use this command in shell to backup an entire database:

mysqldump -u [username] -p[password] [databasename] > [filename.sql]

This is actually one command followed by the > operator, which says, “take the output of the previous command and store it in this file.”

Note: The lack of a space between -p and the mysql password is not a typo. However, if you leave the -p flag present, but the actual password blank then you will be prompted for your password. Sometimes this is recommended to keep passwords out of your bash history.

Questions:
Answers:

No one mentions anything about the –single-transaction option. People should use it by default for InnoDB tables to ensure data consistency. In this case:

mysqldump --single-transaction -h [remoteserver.com] -u [username] -p [password] [yourdatabase] > [dump_file.sql]

This makes sure the dump is run in a single transaction that’s isolated from the others, preventing backup of a partial transaction.

For instance, consider you have a game server where people can purchase gears with their account credits. There are essentially 2 operations against the database:

  1. Deduct the amount from their credits
  2. Add the gear to their arsenal

Now if the dump happens in between these operations, the next time you restore the backup would result in the user losing the purchased item, because the second operation isn’t dumped in the SQL dump file.

While it’s just an option, there are basically not much of a reason why you don’t use this option with mysqldump.

Questions:
Answers:

This topic shows up on the first page of my google result, so here’s a little useful tip for new comers.

You could also dump the sql and gzip it in one line:

mysqldump -u [username] -p[password] [database_name] | gzip > [filename.sql.gz]

Questions:
Answers:
mysqldump -h [domain name/ip] -u [username] -p[password] [databasename] > [filename.sql]

Questions:
Answers:

you can choose an other way :

mysql -u [username] [databasename]

And choose your sql file like :

source mydumpdatabase.sql