420

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.

Mandar Pande
  • 12,250
  • 16
  • 45
  • 72
Starx
  • 77,474
  • 47
  • 185
  • 261

20 Answers20

666

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

Adriano
  • 19,463
  • 19
  • 103
  • 140
Starx
  • 77,474
  • 47
  • 185
  • 261
  • I am getting this error mysqldump: Got error: 1556: You can't use locks with log tables. when using LOCK TABLES – Ammad Apr 01 '16 at 00: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 01 '16 at 07: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 '17 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 '18 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: https://programalitics.substack.com/p/mysql-database-export-tips – Adriaan Feb 14 '22 at 18:36
130

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.

circuitry
  • 1,169
  • 16
  • 16
Srinivasan.S
  • 3,065
  • 1
  • 24
  • 15
  • 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 05 '14 at 19:59
  • 3
    Specifying `gzip -9` makes the resulting file a bit smaller, because the maximum compression is used. – AntonK Mar 04 '19 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 :)

Umesh Patil
  • 4,668
  • 32
  • 24
  • 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 '15 at 10:32
  • I got an error `mysqldump: [ERROR] unknown option '--database'` but wit exclusion of `--database` it has worked. – hejdav Jan 23 '16 at 14:24
  • @Skyrpex Thanks, that was the solution I've been looking for. – Martijn Jul 22 '16 at 09:55
44

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.

Rajesh Patel
  • 1,946
  • 16
  • 20
Mitesh vaghela
  • 470
  • 4
  • 6
  • My friend i've tried to explain process a bit. rather then giving direct command – Mitesh vaghela Oct 02 '17 at 08: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 – Md Maidul Islam Mar 22 '21 at 12:03
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

Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
user2484830
  • 251
  • 3
  • 2
  • 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 – Md Maidul Islam Mar 22 '21 at 12:04
25

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 user@xxx.xxx.xxx.xxx:/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]
amd
  • 20,637
  • 6
  • 49
  • 67
Opentuned
  • 1,477
  • 17
  • 21
  • 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 05 '14 at 20:00
  • 1
    @Opentuned, What's with the linux fetish? – Pacerier Mar 13 '15 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._ – Marwan Salim Dec 14 '18 at 04:10
  • *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 '14 at 08:55
  • 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 – Md Maidul Islam Mar 22 '21 at 12:04
  • @MdMaidulIslam looks like you have the flags -p and -u but you need the credentials after each of the flags I believe, rather than both flags and then both credentials, you will also need to specify the db name last. Lastly if thats a real password you might want to delete the comment :-) – Opentuned Mar 23 '21 at 16:19
  • @Opentuned Could you please give me an example query. I did mysqldump -u [username] -p [password] [dbname] > filename.sql and mysqldump -h [host] -p -u [user] [database name] > filename.sql but same error. – Md Maidul Islam Mar 24 '21 at 06:25
20

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

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

arcadius
  • 319
  • 2
  • 4
  • 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 04 '19 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](https://dev.mysql.com/doc/refman/5.6/en/mysqldump-stored-programs.html)) – AntonK Mar 04 '19 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 https://programalitics.substack.com/p/mysql-database-export-tips for details – Adriaan Feb 14 '22 at 18:42
15

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
Starx
  • 77,474
  • 47
  • 185
  • 261
Colin Claverie
  • 789
  • 9
  • 11
  • 3
    Take care about writing password in the command. It will be saved in the bash history and can break security. – Tsounabe Oct 13 '20 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)

Ossama
  • 2,401
  • 7
  • 46
  • 83
  • 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 04 '19 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

Sachin Shukla
  • 1,249
  • 14
  • 17
  • 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 '14 at 02:26
  • Does it really work with the space between `-p` and `password`? – Davis Jul 18 '18 at 06:34
7
mysqldump -h [host] -p -u [user] [database name] > filename.sql

Example in localhost

mysqldump -h localhost -p -u root cookbook > cookbook.sql
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
  • 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 – Md Maidul Islam Mar 22 '21 at 12:03
5
mysqldump --no-tablespaces -u username -p pass database_name > db_backup_file.sql
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Zafar Ahmed
  • 329
  • 3
  • 4
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) 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 '21 at 09:01
2

For import:

mysql -u db_username -p newFileName < databasName.sql

For export:

mysqldump -u db_username -p databasName > newFileName.sql
umekalu
  • 169
  • 1
  • 5
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.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Aswin k
  • 11
  • 1
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
npcoder
  • 414
  • 1
  • 5
  • 13
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!

Vikash Choudhary
  • 1,439
  • 12
  • 9
0

mysql -u -p databaseName>fileToPutDatabase

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.

Pawan Verma
  • 1,152
  • 14
  • 22
-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
Ridhwan Luthra
  • 121
  • 1
  • 5