77

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.

Richard Garside
  • 87,839
  • 11
  • 80
  • 93
Harsha
  • 3,548
  • 20
  • 52
  • 75

9 Answers9

86

Try it with Mysqldump

#mysqldump --host=the.remotedatabase.com -u yourusername -p yourdatabasename > /User/backups/adump.sql
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • 1
    To use this command we should enable the access from remote on the mysql server? – Gspoon Jun 18 '20 at 09:36
  • @Gspoon Yes, it connects from the computer it runs on to the server, just like any other client. – Alejandro Sep 04 '20 at 15:18
  • 1
    If you get Unknown table 'COLUMN_STATISTICS error you should take a look at https://serverfault.com/questions/912162/mysqldump-throws-unknown-table-column-statistics-in-information-schema-1109 answer – naveen.panwar Dec 16 '20 at 16:04
57

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.

Ian Jamieson
  • 4,376
  • 2
  • 35
  • 55
44

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.

datasn.io
  • 12,564
  • 28
  • 113
  • 154
  • 1
    Excellent! Thanks man. I had mysql access to remote server, but couldn't dump DBs. The error was `mysqldump: Got error: 1044: "Access denied for user 'USER'@'%' to database 'DB_NAME'" when using LOCK TABLES` , but using `--single-transaction` option in `mysqldump` solved my problem. – IAmAliYousefi Mar 05 '19 at 14:39
20

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]
Unnawut
  • 7,500
  • 1
  • 26
  • 33
3
mysqldump -h [domain name/ip] -u [username] -p[password] [databasename] > [filename.sql]
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
1

Tried all the combinations here, but this worked for me:

mysqldump -u root -p --default-character-set=utf8mb4 [DATABASE TO BE COPIED NAME] > [NEW DATABASE NAME]
jsibs
  • 666
  • 2
  • 7
  • 25
0

If you haven't install mysql_client yet and using Docker container instead:

sudo docker exec MySQL_CONTAINER_NAME /usr/bin/mysqldump --host=192.168.1.1 -u username --password=password db_name > dump.sql

logbasex
  • 1,688
  • 1
  • 16
  • 22
0

You can directly pipe it to the remote server where you wish to copy your data to:

mysqldump -u your_db_user_name -p --set-gtid-purged=OFF --triggers --routines --events --compress --skip-lock-tables --verbose your_local_sql_db_name | mysql -u your_db_user_name -p -h your_remote_server_ip your_remote_server_db_name

You need to have created the db on your remote sql server.

Using the above command, I was able to copy from my local sql server version 8.0.23 to my remote sqlserver running 8.0.25

Shery
  • 1,808
  • 5
  • 27
  • 51
-4

This is how you would restore a backup after you successfully backup your .sql file

mysql -u [username] [databasename]

And choose your sql file with this command:

source MY-BACKED-UP-DATABASE-FILE.sql
Community
  • 1
  • 1
Nico
  • 25
  • 1