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...
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityHow 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...
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
To backup:
mysqldump -u user -p database > backup.sql
To import:
mysql -u user -p database < backup.sql
Follow the following steps.
C:\>cd wamp
C:\wamp>cd bin
C:\wamp\bin>cd mysql
C:\wamp\bin\mysql>cd mysql15.5.8
C:\wamp\bin\mysql\mysql15.5.8>cd bin
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
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, wherefile_name
is the full path name to the file.
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
mysqldump -uroot -p mydb > backup.sql
mysqldump -u <username> -p <password> <databasename> > backup.sql
(provide the username and password and dbname)
c:\>where *mysqldump*
say that it not exists.
Jul 18, 2019 at 11:26
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.