how to mysqldump remote db from local machine
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To dump a remote MySQL database from your local machine, run mysqldump locally and point it at the remote host with the correct credentials. The basic idea is simple, but production-safe usage also depends on network access, authentication, consistency options, and how you protect credentials.
The Basic Command
The common form is:
This means:
- '
-hremote host' - '
-uMySQL user' - '
-pprompt for password' - '
my_databasedatabase name' - '
>write the dump to a local file'
When you run it, mysqldump connects from your local machine to the remote database server and writes the SQL backup file locally.
Use Better Defaults for InnoDB
For many real systems, especially InnoDB-backed databases, a safer backup command is:
--single-transaction gives a consistent snapshot for transactional tables without locking the whole database the way older workflows often did. --quick streams rows instead of buffering too much client-side memory.
Those options are much more appropriate than a bare minimal dump for many production databases.
Use a Non-Default Port If Needed
If the server uses a custom port, add -P:
Remember that -P is uppercase for port. Lowercase -p is for password prompting.
Prefer Password Prompting or Config Files
Do not put the password directly in the shell command unless you understand the exposure risk.
Less safe:
That can leak into shell history or process listings. Better options are:
- use
-pand enter the password interactively - use a MySQL option file such as
~/.my.cnf - use environment-controlled automation secrets in CI or ops tooling
Security practices matter because database dump credentials are usually powerful.
Use SSH Tunneling When Direct MySQL Access Is Blocked
Many production databases do not allow public remote MySQL connections. In that case, tunnel through SSH instead of opening the database port broadly.
Then, in another terminal:
This is often safer than exposing MySQL directly to the internet.
Compress Large Dumps
For larger databases, compression saves bandwidth and disk space.
This is a practical default for remote backups over slower links.
Dump Specific Tables or Skip Some Tables
You do not have to dump the entire database.
Dump specific tables:
Ignore a table:
This is useful when you want a focused backup or want to exclude huge ephemeral tables.
Common Pitfalls
The most common mistake is assuming the database is reachable remotely when firewall rules or cloud security groups block the connection. Another is running mysqldump without --single-transaction on large transactional databases and then being surprised by consistency or locking behavior. Developers also put passwords directly into the command line, which is convenient but unsafe. Finally, many teams forget that application servers and database servers are often only reachable through SSH tunneling or a bastion host, not direct public database access.
Summary
- Run
mysqldumplocally with the remote host, user, and database name. - Use
--single-transactionand--quickfor safer large InnoDB backups. - Add
-Pif the remote server uses a non-default port. - Avoid exposing passwords on the command line.
- Use SSH tunneling when direct remote MySQL access is restricted.
Related reading
- How to optimize text search for inverted index and relational database?
- How to organise a many to many relationship in MongoDB
- How to pass along username and password to cassandra in python
- How to pass an empty string as value of a field in dynamodb?
- How to pass TTL in Cassandra Java Driver QueryBuilder?
- How to perform a mysqldump without a password prompt?
- How to perform better document version control on Excel files and SQL schema files
- How to perform OR condition in django queryset?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.