43

How do I import and export a local instance of a MySQL database via command-line interface?

My operating system is Windows and WampServer server is installed.

1

9 Answers 9

51

Use this for export:

mysqldump -u username -p databasename > filename.sql

this is my Sample Export in CMD and i'll save in drive D:\

mysqldump -u root -pmypassword pos > d:\pos.sql
2
  • This is the first time am doing the export using sql, Tell my my sql can place any where , that is my file may in desktop or d drive , or is there any particular location i have past my sql file
    – Bharanikumar
    Apr 25, 2010 at 12:04
  • 1
    you can place your file anywhere as long as you have your mysql bin folder in your PATH. the alternative is to call the mysql and mysqldump executables using the full path to them (e.g F:\wamp\bin\mysql\mysql5.1.36\bin\mysql and F:\wamp\bin\mysql\mysql5.1.36\bin\mysqldump) . In your screenshot the problem is that you didn't supply the username and password when starting the mysql program (you supply it with: mysql -u USERNAME -p <enter> and then type your password)
    – matei
    Apr 26, 2010 at 5:47
24

To backup:

mysqldump -u user -p database > backup.sql

To import:

mysql -u user -p database < backup.sql
2
  • mysql> mysqldump -u root -p sale > backup.sql -> ; 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 'mysql dump -u root -p sale > backup.sql' at line 1 Jan 23, 2013 at 9:14
  • @Bharanikumar I don't think you didn't find out, but you need to use the mysqldump.exe not the mysql.exe.
    – szabkel
    Aug 12, 2015 at 6:23
10

Follow the following steps.

  1. Run cmd command
  2. Type c: or d: on command prompt. This will be based on your WAMP server installations.
  3. Assuming you have installed wamp on C: drive.
  4. C:\>cd wamp
  5. C:\wamp>cd bin
  6. C:\wamp\bin>cd mysql
  7. C:\wamp\bin\mysql>cd mysql15.5.8
  8. C:\wamp\bin\mysql\mysql15.5.8>cd bin
  9. C:\wamp\bin\mysql\mysql15.5.8\bin>mysql.exe -u root

Now you'll get mysql command prompt.

** Note Here My MySql version ins 15.5.8 it may change based on your wamp installation. Now you can follow the answer by @Matei. I'm pasting his answer here. Hope this will help you.

to export:

mysqldump -u username -p databasename > filename.sql

and to Import:

mysql -u username -p databasename < filename.sql
3

Go to folder C:\wamp\bin\mysql\MYSQL_VERSION\bin

Hit shift + right-click in that folder and select "Open command window here" from the menu that pops up.

This will open a command window in the path you are already in, so C:\wamp\bin\mysql\MYSQL_VERSION\bin

Enter any of the MySQL commands you like with any of the executables you find in that folder.

To export:

mysqldump -u db_user -pDB_PASSWORD db_name > /path/to/save/backup_file.sql

To import:

mysql -u db_user -p db_name < /path/to/save/backup_file.sql

The user has a password (DB_PASSWORD) and that needs to be directly behind -p without a space. Beware like this the password can be seen by other programs.

If you do not specify a password in the command directly after the -p flag and just use the db_name you will be asked to put in the password interactively every time you run the command.

This means every time you want to export or import your databse you have to put in the password. This is also not very secure and like this you cannot run a script to automatically export your database at a given time interval.

So it is best to follow this advice:

Quoting the MySQL documentation: (http://dev.mysql.com/doc/refman/5.1/en/password-security-user.html):

Store your password in an option file. For example, on Unix you can list your password in the [client] section of the .my.cnf file in your home directory:

[client]
password=your_pass

To keep the password safe, the file should not be accessible to anyone but yourself. To ensure this, set the file access mode to 400 or 600. For example:

shell> chmod 600 .my.cnf

To name from the command line a specific option file containing the password, use the --defaults-file=file_name option, where file_name is the full path name to the file.

1

To export data in MySQL through msdos is

Traverse bin folder of your mysql directory and search for mysqldump or put it in environmental variables to access it from anywhere

  1. If user is root with no password

mysqldump -uroot -p mydb > backup.sql

  1. If username and password

mysqldump -u <username> -p <password> <databasename> > backup.sql

(provide the username and password and dbname)

1
  • I am using MySQL on Windows, but c:\>where *mysqldump* say that it not exists. Jul 18, 2019 at 11:26
0

mysql -u user -p database < backup.sql

0

Install HeidiSQl it Easy way to Export Mysql File. Also it is SQL Editor

0

If you want to export all databases, use this:

set path=c:/wamp/bin/mysql/mysql5.5.24/bin/
mysqldump -u root -p --all-databases > exported_dbs.sql 

It worked for me in windows.

0

mysqldump has one important drawback, it is performance. On Windows, you can use new shel for mysql - mysqlsh. By default, it is not installed with MySQL. You can download it from the MySQL website (windows).

mysqlsh can export databases in multiple threads, which can significantly improve export speed for large databases.

To export all databases please use the following command:

mysqlsh --mysql -u user -p -h localhost -P 3306 -- util dump-instance /tmp/sample-backup-instane

More examples

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