426

I have a database that is quite large so I want to export it using Command Prompt but I don't know how to.

I am using WAMP.

2
  • 7
    DOS? are you using Windows 98? Or do you mean the command prompt?
    – jle
    Jun 13, 2010 at 7:46
  • Of youre using wamp , you should also have phpmyadmin, with the export option Feb 1, 2018 at 17:02

20 Answers 20

672

First check if your command line recognizes mysql command. If not go to command & type in:

set path=c:\wamp\bin\mysql\mysql5.1.36\bin

Then use this command to export your database:

mysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql

You will then be prompted for the database password.

This exports the database to the path you are currently in, while executing this command

Note: Here are some detailed instructions regarding both import and export

5
  • I am getting this error mysqldump: Got error: 1556: You can't use locks with log tables. when using LOCK TABLES
    – Ammad
    Apr 1, 2016 at 0:57
  • 1
    @Ammad, You might be trying to dump the logs tables as well. You need to exclude them. I suggest asking a different question with further details. If you want to me to look into it, give me the link as well. I am happy to look into it.
    – Starx
    Apr 1, 2016 at 7:36
  • 1
    I am running it from VSCODE extension and it is showing Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump -u mypu -p mypu>abc.sql' at line 1 and I have 5.7.20 version of mysql
    – Sunil Garg
    Dec 19, 2017 at 10:25
  • 1
    If you dont want to set a variable path in windows globally (like me) just cd C:\wamp\bin\mysql\mysql(mysql version)\bin\ and then run your mysqldump command.
    – fat_mike
    Sep 16, 2018 at 20:43
  • If you have any Stored Procedures or Functions or events you need to add --routines and --events to export them also, see a blog post about it here: programalitics.substack.com/p/mysql-database-export-tips
    – Adriaan
    Feb 14, 2022 at 18:36
131

Simply use the following command,

For Export:

mysqldump -u [user] -p [db_name] | gzip > [filename_to_compress.sql.gz] 

For Import:

gunzip < [compressed_filename.sql.gz]  | mysql -u [user] -p[password] [databasename] 

Note: There is no space between the keyword '-p' and your password.

2
  • 1
    I think you missed the fact that he's using WAMP - Windows... gunzip / gzip are not bundled with Windows. It might be possible the run your command after installing some stuff, but I think you've missed that fact.
    – Rolf
    Feb 5, 2014 at 19:59
  • 3
    Specifying gzip -9 makes the resulting file a bit smaller, because the maximum compression is used.
    – AntonK
    Mar 4, 2019 at 23:06
57

Well you can use below command,

mysqldump --databases --user=root --password your_db_name > export_into_db.sql

and the generated file will be available in the same directory where you had ran this command.

You could find more on the official reference for mysqldump: Import Export MySQL DB

Note: use --databases instead of --database since the last one is no more supported.

Enjoy :)

3
  • 5
    For those who get an error like "Access denied when using LOCK TABLES", you should add the "--single-transaction" parameter to the command.
    – Skyrpex
    Oct 19, 2015 at 10:32
  • I got an error mysqldump: [ERROR] unknown option '--database' but wit exclusion of --database it has worked.
    – hejdav
    Jan 23, 2016 at 14:24
  • @Skyrpex Thanks, that was the solution I've been looking for.
    – Martijn
    Jul 22, 2016 at 9:55
46

First of all open command prompt then open bin directory in cmd (i hope you're aware with cmd commands) go to bin directory of your MySql folder in WAMP program files.

run command

mysqldump -u db_username -p database_name > path_where_to_save_sql_file

press enter system will export particular database and create sql file to the given location.

i hope you got it :) if you have any question please let me know.

2
  • My friend i've tried to explain process a bit. rather then giving direct command Oct 2, 2017 at 8:03
  • ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump -h 10.0.0.31 -p -u asfuser asfd8crm > var/downloads/db.sql' at line 1 mysql> mysqldump -h 10.0.0.31 -p -u asfuser asfd8crm > var/downloads Mar 22, 2021 at 12:03
26

Locate your mysql instance with:

which mysql

If this is correct then export with the following (else navigate to the mysql instance in your mamp folder in bin):

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

And if you wish to zip it at the sametime:

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

You can then move this file between servers with:

scp [email protected]:/path_to_your_dump/filename.sql.gz your_detination_path/

(where xxx.xxx.xxx.xxx is the server IP address)

And then import it with:

gunzip filename.sql.gz | mysql -u [user] -p [password] [database]
7
  • 4
    Again, W AMP => Windows. which is a Linux command. gunzip / gzip are not bundled with Windows - and may not be available. scp... you get the point.
    – Rolf
    Feb 5, 2014 at 20:00
  • The answer of Opentuned is great. One small thing is removing the actual password from the command line : mysqldump -u [username] -p [dbname] > filename.sql This prevents people searching your history for the password.
    – Vincent
    Sep 23, 2014 at 8:55
  • 1
    @Opentuned, What's with the linux fetish?
    – Pacerier
    Mar 13, 2015 at 12:55
  • for -p [pasword] must be near e.g : mysqldump -u tutorials -p'mypassword' -h 127.0.0.1 database_name > file_backup_database_name_.sql Warning: Using a password on the command line interface can be insecure. Dec 14, 2018 at 4:10
  • ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump -h 10.0.0.31 -p -u asfuser asfd8crm > var/downloads/db.sql' at line 1 mysql> mysqldump -h 10.0.0.31 -p -u asfuser asfd8crm > var/downloads Mar 22, 2021 at 12:04
25

Go to command prompt at this path,

C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin>

Then give this command to export your database (no space after -p)

mysqldump -u[username] -p[userpassword] yourdatabase > [filepath]wantedsqlfile.sql

1
  • ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump -h 10.0.0.31 -p -u asfuser asfd8crm > var/downloads/db.sql' at line 1 mysql> mysqldump -h 10.0.0.31 -p -u asfuser asfd8crm > var/downloads Mar 22, 2021 at 12:04
20

To export PROCEDUREs, FUNCTIONs & TRIGGERs too, add --routines parameter:

mysqldump -u YourUser -p YourDatabaseName --routines > wantedsqlfile.sql

3
  • The switch --routines makes the difference :) Any hints how to export only PROCEDUREs, FUNCTIONs & TRIGGERs? For those who have forgotten about the switch and exported "pure" DB without the "algorithms" :)
    – AntonK
    Mar 4, 2019 at 22:13
  • BTW the switch --events is needed to dump Event Scheduler events, and the TRIGGERs are always exported unless --skip-triggers is specified (according to 7.4.5.3 Dumping Stored Programs)
    – AntonK
    Mar 4, 2019 at 23:02
  • @AntonK it is a bit late, but to export only triggers and procedures you can use: mysqldump -u YourUser -p YourDatabaseName -n -d -t --routines --triggers --add-drop-trigger --quote-names --allow-keywords > wantedsqlfile.sql See programalitics.substack.com/p/mysql-database-export-tips for details
    – Adriaan
    Feb 14, 2022 at 18:42
16

The problem with all these solutions (using the > redirector character) is that you write your dump from stdout which may break the encoding of some characters of your database.

If you have a character encoding issue. Such as :

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ...

then, you MUST use -r option to write the file.

MySQL

mysqldump -u user -pyour-password-without-space-between-letter-p-and-your-password --default-character-set=utf8 --host $HOST database-name -r dump.sql

Using Docker

docker exec --rm -v $pwd:dump -it mysql:5:7 mysqldump -u user -pyour-password-without-space-between-letter-p-and-your-password --default-character-set=utf8 --host $HOST database-name -r dump/dump.sql

Note: this mounts the current path as dump inside the instance.

We found the answer here

Conversely, don't use < to import your dump into your database, again, your non-utf8 characters may not be passed; but prefer source option.

mysql -u user -pYourPasswordYouNowKnowHow --default-character-set=utf8 your-database
mysql> SET names 'utf8'
mysql> SOURCE dump.sql
1
  • 3
    Take care about writing password in the command. It will be saved in the bash history and can break security.
    – Tsounabe
    Oct 13, 2020 at 12:27
10

Give this command to export your database, this will include date as well

mysqldump -u[username] -p[userpassword] --databases yourdatabase | gzip > /home/pi/database_backup/database_`date '+%m-%d-%Y'`.sql.gz

(no space after -p)

1
  • 1
    Note, that specifying --databases will insert statements as CREATE DATABASE XYZ; and USE XYZ;, which isn't handy when importing the SQL into another DB with another name...
    – AntonK
    Mar 4, 2019 at 23:00
8

I have installed my wamp server in D: drive so u have to go to the following path from ur command line->(and if u have installed ur wamp in c: drive then just replace the d: wtih c: here)

D:\>cd wamp
D:\wamp>cd bin
D:\wamp\bin>cd mysql
D:\wamp\bin\mysql>cd mysql5.5.8 (whatever ur verserion will be displayed here use keyboard Tab button and select the currently working mysql version on your server if you have more than one mysql versions)
D:\wamp\bin\mysql\mysql5.5.8>cd bin
D:\wamp\bin\mysql\mysql5.5.8\bin>mysqldump -u root -p password db_name > "d:\backupfile.sql"

here root is user of my phpmyadmin password is the password for phpmyadmin so if u haven't set any password for root just nothing type at that place, db_name is the database (for which database u r taking the backup) ,backupfile.sql is the file in which u want ur backup of ur database and u can also change the backup file location(d:\backupfile.sql) from to any other place on your computer

2
  • Of all the ways I found to do this, this answer is the only one that worked for me. I'm using MySQL version 5.6, so the path was ...\MySQL\MySQL Server 5.6\bin for the command prompt. You will have to use your MySQL admin user name and password, those specified for a particular database won't work. Thank you Sachin. Even the instructions in the MySQL manual for v.5.6 don't work. The syntax is incomplete there.
    – Max West
    Dec 12, 2014 at 2:26
  • Does it really work with the space between -p and password?
    – Davis
    Jul 18, 2018 at 6:34
7
mysqldump -h [host] -p -u [user] [database name] > filename.sql

Example in localhost

mysqldump -h localhost -p -u root cookbook > cookbook.sql
1
  • ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysqldump -h 10.0.0.31 -p -u asfuser asfd8crm > var/downloads/db.sql' at line 1 mysql> mysqldump -h 10.0.0.31 -p -u asfuser asfd8crm > var/downloads Mar 22, 2021 at 12:03
5
mysqldump --no-tablespaces -u username -p pass database_name > db_backup_file.sql
1
  • 2
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – Yunnosch
    Feb 12, 2021 at 9:01
4

For import:

mysql -u db_username -p newFileName < databasName.sql

For export:

mysqldump -u db_username -p databasName > newFileName.sql
1

Syntax

(mysqldump.exe full path) -u (user name) -p (password) (database name) > (export database file full path)

Example

c:>d:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump.exe -u root -p mydbname > d:\mydb.sql

where d:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump.exe will be your actual mysqldump.exe path, mydbname is the name of database which you want to export and d:\mydb.sql is the path where you want to store the exported database.

0

I have used wamp server. I tried on

c:\wamp\bin\mysql\mysql5.5.8\bin\mysqldump -uroot -p db_name > c:\somefolder\filename.sql

root is my username for mysql, and if you have any password specify it with:

-p[yourpassword]

Hope it works.

1
  • --password need to be used if you are tring to type the password as well
    – Erce
    Mar 15, 2019 at 9:26
0

For windows OS :

When you get error 1064 mysql (42000) while trying to execute mysqldump command, exit from current terminal. And execute mysqldump command.

mysql>exit 
c:\xampp\mysql\bin>mysqldump -uroot -p --databases [database_name] > name_for_export_db.sql
0

I was trying to take the dump of the db which was running on the docker and came up with the below command to achieve the same:

docker exec <container_id/name> /usr/bin/mysqldump -u <db_username> --password=<db_password> db_name > .sql

Hope this helps!

0

Login in your databse server and then hit the below command:-

mysql -u username -p databasename > exportfilename.sql

Then it will ask for password Enter the password and hit enter,it will take some time your database will be exported.

-1

mysql -u -p databaseName>fileToPutDatabase

1
  • should be mysqldump
    – ajib
    Mar 2, 2022 at 13:59
-2

You can use this script to export or import any database from terminal given at this link: https://github.com/Ridhwanluthra/mysql_import_export_script/blob/master/mysql_import_export_script.sh

echo -e "Welcome to the import/export database utility\n"
echo -e "the default location of mysqldump file is: /opt/lampp/bin/mysqldump\n"
echo -e "the default location of mysql file is: /opt/lampp/bin/mysql\n"
read -p 'Would like you like to change the default location [y/n]: ' location_change
read -p "Please enter your username: " u_name
read -p 'Would you like to import or export a database: [import/export]: ' action
echo

mysqldump_location=/opt/lampp/bin/mysqldump
mysql_location=/opt/lampp/bin/mysql

if [ "$action" == "export" ]; then
    if [ "$location_change" == "y" ]; then
        read -p 'Give the location of mysqldump that you want to use: ' mysqldump_location
        echo
    else
        echo -e "Using default location of mysqldump\n"
    fi
    read -p 'Give the name of database in which you would like to export: ' db_name
    read -p 'Give the complete path of the .sql file in which you would like to export the database: ' sql_file
    $mysqldump_location -u $u_name -p $db_name > $sql_file
elif [ "$action" == "import" ]; then
    if [ "$location_change" == "y" ]; then
        read -p 'Give the location of mysql that you want to use: ' mysql_location
        echo
    else
        echo -e "Using default location of mysql\n"
    fi
    read -p 'Give the complete path of the .sql file you would like to import: ' sql_file
    read -p 'Give the name of database in which to import this file: ' db_name
    $mysql_location -u $u_name -p $db_name < $sql_file
else
    echo "please select a valid command"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.