40

How to export / import a database in MySQL through the command line?

MySQL system configuration, WampServer server installed.

OS: Windows

The advice for my local host, not an live hosting...

47

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
| improve this answer | |
  • 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 '10 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 '10 at 5:47
22

To backup:

mysqldump -u user -p database > backup.sql

To import:

mysql -u user -p database < backup.sql
| improve this answer | |
  • 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 – Bharanikumar Jan 23 '13 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. – appl3r Aug 12 '15 at 6:23
9

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
| improve this answer | |
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.

| improve this answer | |
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)

| improve this answer | |
  • I am using MySQL on Windows, but c:\>where *mysqldump* say that it not exists. – Peter Krauss Jul 18 '19 at 11:26
0

mysql -u user -p database < backup.sql

| improve this answer | |
0

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

| improve this answer | |
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.

| improve this answer | |

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