369

I am moving away from Linode because I don't have the Linux sysadmin skills necessary; before I complete the transition to a more noob-friendly service, I need to download the contents of a MySQL database. Is there a way I can do this from the command line?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Phillip Copley
  • 4,238
  • 4
  • 21
  • 38

15 Answers15

783

You can accomplish this using the mysqldump command-line function.

For example:

If it's an entire DB, then:

   $ mysqldump -u [uname] -p db_name > db_backup.sql

If it's all DBs, then:

   $ mysqldump -u [uname] -p --all-databases > all_db_backup.sql

If it's specific tables within a DB, then:

   $ mysqldump -u [uname] -p db_name table1 table2 > table_backup.sql

You can even go as far as auto-compressing the output using gzip (if your DB is very big):

   $ mysqldump -u [uname] -p db_name | gzip > db_backup.sql.gz

If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):

   $ mysqldump -P 3306 -h [ip_address] -u [uname] -p db_name > db_backup.sql

It should drop the .sql file in the folder you run the command-line from.

EDIT: Updated to avoid inclusion of passwords in CLI commands, use the -p option without the password. It will prompt you for it and not record it.

nickhar
  • 19,981
  • 12
  • 60
  • 73
  • 1
    Your answer in conjunction with http://stackoverflow.com/questions/2989724/how-to-mysqldump-remote-db-from-local-machine -- Should do what he is asking, since he did include he needs it downloaded. It's either that or a `wget` or `scp` will be needed to retrieve said file once built. – Zak Nov 21 '12 at 01:06
  • 53
    Small note that it is safer to not enter the password right in the command. Only using the -p option without password will prompt for the password when run, that way the password is not stored in your command history (and potentially retrieved). So using the following command: mysqldump -P 3306 -h [ip_address] -u [uname] -p db_name > db_backup.sql – Pitt Oct 08 '16 at 19:05
  • How do you specify the ssh port if access is remote? Port is not default 22 in my use case .. – Timo Nov 29 '17 at 07:27
  • 5
    Instead of using `>` to save, I rather use `-r` in order to prevent trouble with foreign characters, or that nightmare concerning encoding problems, as stated [in this article](https://makandracards.com/makandra/595-dumping-and-importing-from-to-mysql-in-an-utf-8-safe-way). – Pathros Feb 07 '18 at 15:43
  • 1
    For large/actively updated dataases, use --single-transaction parameter. This creates a checkpoint and helps ensure consistency. Also, use switches --routines --triggers, if you have stored procedures/functions/triggers – accord_guy Jul 08 '19 at 08:48
  • 2
    You should use `--result-file=db_backup.sql` instead of `> db_backup.sql`. Quote from the [MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html): "UTF-16 is not permitted as a connection character set (see [Impermissible Client Character Sets](https://dev.mysql.com/doc/refman/8.0/en/charset-connection.html#charset-connection-impermissible-client-charset)), so the dump file will not load correctly. To work around this issue, use the `--result-file` option, which creates the output in ASCII format". – Martin Braun Dec 20 '19 at 19:23
  • When I run this command " >mysqldump -u [uname] -p db_name > db_backup.sql" return the Can not connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' error returning. Mysql is runnig. I can log in "./mysql -u root" command. thank you already – dincer.unal Jan 22 '21 at 05:27
  • NOTE: I'm using MySQL 8 - it should provide an absolute path for the output file mysqldump -u [user_name] -p [db_name] > D:\dump\mof_dump.sql – Nilupul Heshan Mar 14 '21 at 09:52
17

In latest versions of mysql, at least in mine, you cannot put your pass in the command directly.

You have to run:

mysqldump -u [uname] -p db_name > db_backup.sql

and then it will ask for the password.

Lorenzo Lerate
  • 3,552
  • 3
  • 26
  • 25
13

If downloading from remote server, here is a simple example:

mysqldump -h my.address.amazonaws.com -u my_username -p db_name > /home/username/db_backup_name.sql

The -p indicates you will enter a password, it does not relate to the db_name. After entering the command you will be prompted for the password. Type it in and press enter.

Andrew
  • 18,680
  • 13
  • 103
  • 118
9

On windows you need to specify the mysql bin where the mysqldump.exe resides.

cd C:\xampp\mysql\bin

mysqldump -u[username] -p[password] --all-databases > C:\localhost.sql

save this into a text file such as backup.cmd

drooh
  • 578
  • 4
  • 18
  • 46
  • 1
    or usually having mysql in your PATH variable so you can run `mysql` commands from everywhere without being in it's directory. – behz4d Jul 08 '17 at 04:55
4

Don't go inside mysql, just open Command prompt and directly type this:

mysqldump -u [uname] -p[pass] db_name > db_backup.sql
xKobalt
  • 1,498
  • 2
  • 13
  • 19
Nithin Raja
  • 1,144
  • 12
  • 11
3

Just type mysqldump or mysqldump --help in your cmd will show how to use

Here is my cmd result

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
3

Go to MySQL installation directory and open cmd from there. Then execute the below command to get a backup of your database.

mysqldump -u root -p --add-drop-database --databases db> C:\db-dontdelete\db.sql
CumminUp07
  • 1,936
  • 1
  • 8
  • 21
2

If you are running the MySQL other than default port:

mysqldump.exe -u username -p -P PORT_NO database > backup.sql
simhumileco
  • 31,877
  • 16
  • 137
  • 115
Anand Raj
  • 29
  • 1
2

For those who wants to type password within the command line. It is possible but recommend to pass it inside quotes so that the special character won't cause any issue.

mysqldump -h'my.address.amazonaws.com' -u'my_username' -p'password' db_name > /path/backupname.sql
Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76
2

mysqldump is another program (.exe file) in the MySQL directory

Program Files\MySQL\MySQL Server 8.0\bin

step 1: First you have to go to the path and open CMD from the folder.

step 2: Then type mysqldump in the CMD


it should display as follows

Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help

step 3: Then type this command

mysqldump -u [user_name] -p [database_name] > D:\db_dump.sql

Note : you should provide an absolute path for the output file. Here I provide D:\

Nilupul Heshan
  • 580
  • 1
  • 5
  • 18
1

If you have the database named archiedb, use this:

mysql -p <password for the database> --databases archiedb > /home/database_backup.sql

Assuming this is Linux, choose where the backup file will be saved.

xKobalt
  • 1,498
  • 2
  • 13
  • 19
1

For some versions of MySQL try.

sudo mysqldump [database name] > db_backup.sql
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
0

For Windows users you can go to your mysql folder to run the command

e.g.

cd c:\wamp64\bin\mysql\mysql5.7.26\bin
mysqldump -u root -p databasename > dbname_dump.sql
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Narayan P
  • 139
  • 1
  • 3
0

Note: This step only comes after dumping your MySQL file(which most of the answers above have addressed).

It assumes that you have the said dump file in your remote server and now you want to bring it down to your local computer.

To download the dumped .sql file from your remote server to your local computer, do

scp -i YOUR_SSH_KEY your_username@IP:name_of_file.sql ./my_local_project_dir
Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39
-2
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%.%MM%.%DD%.%HH%.%Min%.%Sec%"
set drive=your backup folder
set databaseName=your databasename
set user="your database user"
set password="your database password"
subst Z: "C:\Program Files\7-Zip" 
subst M: "D:\AppServ\MySQL\bin"
set zipFile="%drive%\%databaseName%-%datestamp%.zip"
set sqlFile="%drive%\%databaseName%-%datestamp%.sql"
M:\mysqldump.exe --user=%user% --password=%password% --result-file="%sqlFile%" --databases %databaseName%
@echo Mysql Backup Created
Z:\7z.exe a -tzip "%zipFile%" "%sqlFile%"
@echo File Compress End
del %sqlFile%
@echo Delete mysql file
pause;