441

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? Commented Jun 13, 2010 at 7:46
  • Of youre using wamp , you should also have phpmyadmin, with the export option Commented Feb 1, 2018 at 17:02

22 Answers 22

697

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

Sign up to request clarification or add additional context in comments.

5 Comments

I am getting this error mysqldump: Got error: 1556: You can't use locks with log tables. when using LOCK TABLES
@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.
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
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.
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
139

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 Comments

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.
Specifying gzip -9 makes the resulting file a bit smaller, because the maximum compression is used.
61

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 Comments

For those who get an error like "Access denied when using LOCK TABLES", you should add the "--single-transaction" parameter to the command.
I got an error mysqldump: [ERROR] unknown option '--database' but wit exclusion of --database it has worked.
@Skyrpex Thanks, that was the solution I've been looking for.
47

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 Comments

My friend i've tried to explain process a bit. rather then giving direct command
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
26

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 Comment

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
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 Comments

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.
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.
@Opentuned, What's with the linux fetish?
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.
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
|
22

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

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

3 Comments

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" :)
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 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
17

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 Comment

Take care about writing password in the command. It will be saved in the bash history and can break security.
11

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 Comment

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...
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 Comments

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.
Does it really work with the space between -p and password?
8
mysqldump -h [host] -p -u [user] [database name] > filename.sql

Example in localhost

mysqldump -h localhost -p -u root cookbook > cookbook.sql

1 Comment

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
8

For import:

mysql -u db_username -p newFileName < databasName.sql

For export:

mysqldump -u db_username -p databasName > newFileName.sql

Comments

6
mysqldump --no-tablespaces -u username -p pass database_name > db_backup_file.sql

1 Comment

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.
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.

Comments

1

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!

Comments

1

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.

Comments

1

Easier way for me to get exported db with preview.

mysqldump -u db_user -p"db_password" your_database > your_database.sql -v
  1. db_user = MySQL username E.g. root
  2. db_password = Password for the username
  3. your_database = Your database name
  4. Used -v to preview while exporting

Comments

1

If you are on MacOS you may need to add host information:

Export:

mysqldump -u user_name -p -h 127.0.0.1 db_name --routines > db_name.sql

Import:

mysql -u user_name -p -h 127.0.0.1 db_name < db_name.sql

Comments

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 Comment

--password need to be used if you are tring to type the password as well
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

Comments

-1

mysql -u -p databaseName>fileToPutDatabase

1 Comment

should be mysqldump
-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.