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.
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
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.
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 :)
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.
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
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]
To export PROCEDUREs, FUNCTIONs & TRIGGERs too, add --routines parameter:
mysqldump -u YourUser -p YourDatabaseName --routines > wantedsqlfile.sql
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
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)
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
mysqldump -h [host] -p -u [user] [database name] > filename.sql
Example in localhost
mysqldump -h localhost -p -u root cookbook > cookbook.sql
mysqldump --no-tablespaces -u username -p pass database_name > db_backup_file.sql
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.
For import:
mysql -u db_username -p newFileName < databasName.sql
For export:
mysqldump -u db_username -p databasName > newFileName.sql
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.
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
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!
mysql -u -p databaseName>fileToPutDatabase
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.
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
Related
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.
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
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.
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 :)
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.
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
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]
To export PROCEDUREs, FUNCTIONs & TRIGGERs too, add --routines parameter:
mysqldump -u YourUser -p YourDatabaseName --routines > wantedsqlfile.sql
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
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)
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
mysqldump -h [host] -p -u [user] [database name] > filename.sql
Example in localhost
mysqldump -h localhost -p -u root cookbook > cookbook.sql
mysqldump --no-tablespaces -u username -p pass database_name > db_backup_file.sql
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.
For import:
mysql -u db_username -p newFileName < databasName.sql
For export:
mysqldump -u db_username -p databasName > newFileName.sql
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.
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
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!
mysql -u -p databaseName>fileToPutDatabase
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.
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
I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.
I have a Windows Server 2008 R2 installation. I placed the .sql file on the C drive, and I tried this command
database_name < file.sql
It is not working. I get syntax errors.
How can I import this file without a problem?
Do I need to create a database first?
Try:
mysql -u username -p database_name < file.sql
Check MySQL Options.
Note 1: It is better to use the full path of the SQL file file.sql.
Note 2: Use -R and --triggers with mysqldump to keep the routines and triggers of the original database. They are not copied by default.
Note 3 You may have to create the (empty) database from MySQL if it doesn't exist already and the exported SQL doesn't contain CREATE DATABASE (exported with --no-create-db or -n option) before you can import it.
A common use of mysqldump is for making a backup of an entire database:
mysqldump db_name > backup-file.sql
You can load the dump file back into the server like this:
Unix
mysql db_name < backup-file.sql
The same in the Windows command prompt:
mysql -p -u [user] [database] < backup-file.sql
PowerShell
cmd.exe /c "mysql -u root -p db_name < backup-file.sql"
MySQL command line
mysql> use db_name;
mysql> source backup-file.sql;
Regarding the time taken for importing huge files: most importantly, it takes more time because the default setting of MySQL is autocommit = true. You must set that off before importing your file and then check how import works like a gem.
You just need to do the following thing:
mysql> use db_name;
mysql> SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;
Among all the answers, for the problem above, this is the best one:
mysql> use db_name;
mysql> source file_name.sql;
Easiest way to import into your schema:
Login to mysql and issue below mention commands.
mysql> use your_db_name;
mysql> source /opt/file.sql;
We can use this command to import SQL from the command line:
mysql -u username -p password db_name < file.sql
For example, if the username is root and password is password. And you have a database name as bank and the SQL file is bank.sql. Then, simply do like this:
mysql -u root -p password bank < bank.sql
Remember where your SQL file is. If your SQL file is in the Desktop folder/directory then go the desktop directory and enter the command like this:
cd ~/Desktop
mysql -u root -p password bank < bank.sql
And if you are in the Project directory and your SQL file is in the Desktop directory. If you want to access it from the Project directory then you can do like this:
cd ~/Project
mysql -u root -p password bank < ~/Desktop/bank.sql
If you already have the database, use the following to import the dump or the sql file:
mysql -u username -p database_name < file.sql
if you don't you need to create the relevant database(empty) in MySQL, for that first log on to the MySQL console by running the following command in terminal or in cmd
mysql -u userName -p;
And when prompted provide the password.
Next, create a database and use it:
mysql>create database yourDatabaseName;
mysql>use yourDatabaseName;
Then import the sql or the dump file to the database from
mysql> source pathToYourSQLFile;
Note: if your terminal is not in the location where the dump or sql file exists, use the relative path in above.
Open the MySQL command line
Type the path of your mysql bin directory and press Enter
Paste your SQL file inside the bin folder of mysql server.
Create a database in MySQL.
Use that particular database where you want to import the SQL file.
Type source databasefilename.sql and Enter
Your SQL file upload successfully.
A solution that worked for me is below:
Use your_database_name;
SOURCE path_to_db_sql_file_on_your_local;
While most answers here just mention the simple command
mysql -u database_user -p [db_name] < database_file.sql
today it's quite common that databases and tables have utf8-collation where this command is not sufficient.
Having utf8-collation in the exported tables it's required to use this command:
mysql -u database_user -p --default-character-set=utf8 [db_name] < database_file.sql
An according export can be done with
mysqldump -u database_user -p --default-character-set=utf8 [db_name] > database_file.sql
Surely this works for other charsets too, how to show the right notation can be seen here:
https://dev.mysql.com/doc/refman/5.7/en/show-collation.html
One comment mentioned also that if a database never exists an empty database had to be created first. This might be right in some cases but depends on the export file. If the exported file includes already the command to create the database then the database never has to be created in a separate step, which even could cause an error on import. So on import, it's advisable to have a look first in the file to know which commands are included there, on export, it's advisable to note the settings, especially if the file is very large and hard to read in an editor.
There are still more parameters for the command which are listed and explained here:
https://dev.mysql.com/doc/refman/5.7/en/mysql-command-options.html
If you use another database version consider searching for the corresponding version of the manual too. The mentioned links refer to MySQL version 5.7.
EDIT:
The same parameters are working for mysqldump too. So while the commands for export and import are different, the mentioned parameters are not.
Nevertheless there exists a special site in the manual that describes the options for mysqldump: https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
To dump a database into an SQL file use the following command.
mysqldump -u username -p database_name > database_name.sql
To import an SQL file into a database (make sure you are in the same directory as the SQL file or supply the full path to the file), do:
mysql -u username -p database_name < database_name.sql
I think it's worth mentioning that you can also load a gzipped (compressed) file with zcat like shown below:
zcat database_file.sql.gz | mysql -u username -p -h localhost database_name
Go to the directory where you have the MySQL executable. -u for username and -p to prompt for the password:
C:\xampp\mysql\bin>mysql -u username -ppassword databasename < C:\file.sql
To import a single database, use the following command.
mysql -u username -p password dbname < dump.sql
To import multiple database dumps, use the following command.
mysql -u username -p password < dump.sql
To import a database, use the following command.
mysql> create new_database;
mysql> use new_database;
mysql> source (Here you need to import the path of the SQL file);
E.g.:
mysql> source E:/test/dump.sql;
You need to use forward slashes (/) even on Windows, e.g., E:/test/dump.sql instead of E:\test\dump.sql
Or double backslashes (\\) because of escaping, i.e., E:\\test\\dump.sql
mysql --user=[user] --password=[password] [database] < news_ml_all.sql
I kept running into the problem where the database wasn't created.
I fixed it like this:
mysql -u root -e "CREATE DATABASE db_name"
mysql db_name --force < import_script.sql
For exporting a database:
mysqldump -u username -p database_name > file.sql
For importing a database:
mysql -u username -p database_name < file.sql
For importing multiple SQL files at one time, use this:
# Unix-based solution
for i in *.sql ; do mysql -u root -pPassword DataBase < $i ; done
For simple importing:
# Unix-based solution
mysql -u root -pPassword DataBase < data.sql
For WAMP:
REM mysqlVersion - replace with your own version
C:\wamp\bin\mysql\mysqlVersion\bin\mysql.exe -u root -pPassword DataBase < data.sql
For XAMPP:
C:\xampp\mysql\bin\mysql -u root -pPassword DataBase < data.sql
You do not need to specify the name of the database on the command line if the .sql file contains CREATE DATABASE IF NOT EXISTS db_name and USE db_name statements.
Just make sure you are connecting with a user that has the permissions to create the database, if the database mentioned in the .sql file does not exist.
Import a database
Go to drive:
d:
MySQL login
c:\xampp\mysql\bin\mysql -u root -p
It will ask for pwd. Enter it:
pwd
Select the database
use DbName;
Provide the file name
\.DbName.sql
Use:
mysql -u root -p password -D database_name << import.sql
Use the MySQL help for details - mysql --help.
I think these will be useful options in our context:
[~]$ mysql --help
mysql Ver 14.14 Distrib 5.7.20, for osx10.12 (x86_64) using EditLine wrapper
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--bind-address=name IP address to bind to.
-D, --database=name Database to use.
--delimiter=name Delimiter to be used.
--default-character-set=name Set the default character set.
-f, --force Continue even if we get an SQL error.
-p, --password[=name] Password to use when connecting to server.
-h, --host=name Connect to host.
-P, --port=# Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).
--protocol=name The protocol to use for connection (tcp, socket, pipe,
-s, --silent Be more silent. Print results with a tab as separator, each row on new line.
-v, --verbose Write more. (-v -v -v gives the table output format).
-V, --version Output version information and exit.
-w, --wait Wait and retry if connection is down.
What is fun, if we are importing a large database and not having a progress bar. Use Pipe Viewer and see the data transfer through the pipe
For Mac, brew install pv
For Debian/Ubuntu, apt-get install pv.
For others, refer to pv - Pipe Viewer
pv import.sql | mysql -u root -p password -D database_name
1.45GiB 1:50:07 [339.0KiB/s] [=============> ] 14% ETA 11:09:36
1.46GiB 1:50:14 [ 246KiB/s] [=============> ] 14% ETA 11:09:15
1.47GiB 1:53:00 [ 385KiB/s] [=============> ] 14% ETA 11:05:36
Go to the directory where you have MySQL.
c:\mysql\bin\> mysql -u username -p password database_name <
filename.sql
Also to dump all databases, use the -all-databases option, and no databases’ name needs to be specified anymore.
mysqldump -u username -ppassword –all-databases > dump.sql
Or you can use some GUI clients like SQLyog to do this.
You can try this query.
Export:
mysqldump -u username –-password=your_password database_name > file.sql
Import:
mysql -u username –-password=your_password database_name < file.sql
and detail following this link:
https://chartio.com/resources/tutorials/importing-from-and-exporting-to-files-using-the-mysql-command-line/
Add the --force option:
mysql -u username -p database_name --force < file.sql
The following command works for me from the command line (cmd) on
Windows 7 on WAMP.
d:/wamp/bin/mysql/mysql5.6.17/bin/mysql.exe -u root -p db_name < database.sql
Providing credentials on the command line is not a good idea. The above answers are great, but neglect to mention
mysql --defaults-extra-file=etc/myhost.cnf database_name < file.sql
Where etc/myhost.cnf is a file that contains host, user, password, and you avoid exposing the password on the command line. Here is a sample,
[client]
host=hostname.domainname
user=dbusername
password=dbpassword
Import into the database:
mysql -u username -p database_name < /file path/file_name.sql
Export from the database:
mysqldump -u username -p database_name > /file path/file_name.sql
After these commands, a prompt will ask for your MySQL password.
Similarly to vladkras's answer to How do import an SQL file using the command line in MySQL?.
Key differences for me:
The database has to exist first
No space between -p and the password
shell> mysql -u root -ppassword #note: no space between -p and password
mysql> CREATE DATABASE databasename;
mysql> using databasename;
mysql> source /path/to/backup.sql
I am running Fedora 26 with MariaDB.
I thought it could be useful for those who are using Mac OS X:
/Applications/xampp/xamppfiles/bin/mysql -u root -p database < database.sql
Replace xampp with mamp or other web servers.
I'm under VPN and I don't have SSH access to remote server.
I can connect to remote database by console
mysql -u username -p -h remote.site.com
Now I'm trying to clone the remote database to local computer
mysqldump -u username -p -h remote.site.com mysqldump | mysql -u root -ppassword webstuff
And I've got error
mysqldump: Got error: 1045: Access denied for user 'webstaff'#'10.75.1.2'
(using password: YES) when trying to connect
How to copy mysql database from remote server to local computer?
Assuming the following command works successfully:
mysql -u username -p -h remote.site.com
The syntax for mysqldump is identical, and outputs the database dump to stdout. Redirect the output to a local file on the computer:
mysqldump -u username -p -h remote.site.com DBNAME > backup.sql
Replace DBNAME with the name of the database you'd like to download to your computer.
Check syntax and execute one command at a time, then verify output.
mysqldump -u remoteusername -p remotepassword -h your.site.com databasename > dump.sql
mysql -u localusername -p localpassword databasename < dump.sql
Once you've matched all passwords, you can use pipe.
Often our databases are really big and the take time to take dump directly from remote machine to other machine as our friends other have suggested above.
In such cases what you can do is to take the dump on remote machine using MYSQLDUMP Command
MYSQLDUMP -uuser -p --all-databases > file_name.sql
and than transfer that file from remote server to your machine using Linux SCP Command
scp user#remote_ip:~/mysql_dump_file_name.sql ./
This can have different reasons like:
You are using an incorrect password
The MySQL server got an error when trying to resolve the IP address of the client host to a name
No privileges are granted to the user
You can try one of the following steps:
To reset the password for the remote user by:
SET PASSWORD FOR some_user#ip_addr_of_remote_client=PASSWORD('some_password');
To grant access to the user by:
GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON YourDB.* TO user#Host IDENTIFIED by 'password';
Hope this helps you, if not then you will have to go through the documentation
Please check this gist.
https://gist.github.com/ecdundar/789660d830d6d40b6c90
#!/bin/bash
# copymysql.sh
# GENERATED WITH USING ARTUR BODERA S SCRIPT
# Source script at: https://gist.github.com/2215200
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
REMOTESERVERIP=""
REMOTESERVERUSER=""
REMOTESERVERPASSWORD=""
REMOTECONNECTIONSTR="-h ${REMOTESERVERIP} -u ${REMOTESERVERUSER} --password=${REMOTESERVERPASSWORD} "
LOCALSERVERIP=""
LOCALSERVERUSER=""
LOCALSERVERPASSWORD=""
LOCALCONNECTION="-h ${LOCALSERVERIP} -u ${LOCALSERVERUSER} --password=${LOCALSERVERPASSWORD} "
IGNOREVIEWS=""
MYVIEWS=""
IGNOREDATABASES="select schema_name from information_schema.SCHEMATA where schema_name != 'information_schema' and schema_name != 'mysql' and schema_name != 'performance_schema' ;"
# GET A LIST OF DATABASES
databases=`$MYSQL $REMOTECONNECTIONSTR -e "${IGNOREDATABASES}" | tr -d "| " | grep -v schema_name`
# COPY ALL TABLES
for db in $databases; do
# GET LIST OF ITEMS
views=`$MYSQL $REMOTECONNECTIONSTR --batch -N -e "select table_name from information_schema.tables where table_type='VIEW' and table_schema='$db';"
IGNOREVIEWS=""
for view in $views; do
IGNOREVIEWS=${IGNOREVIEWS}" --ignore-table=$db.$view "
done
echo "TABLES "$db
$MYSQL $LOCALCONNECTION --batch -N -e "create database $db; "
$MYSQLDUMP $REMOTECONNECTIONSTR $IGNOREVIEWS --compress --quick --extended-insert --skip-add-locks --skip-comments --skip-disable-keys --default-character-set=latin1 --skip-triggers --single-transaction $db | mysql $LOCALCONNECTION $db
done
# COPY ALL PROCEDURES
for db in $databases; do
echo "PROCEDURES "$db
#PROCEDURES
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick --routines --no-create-info --no-data --no-create-db --skip-opt --skip-triggers $db | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
done
# COPY ALL TRIGGERS
for db in $databases; do
echo "TRIGGERS "$db
#TRIGGERS
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick --no-create-info --no-data --no-create-db --skip-opt --triggers $db | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
done
# COPY ALL VIEWS
for db in $databases; do
# GET LIST OF ITEMS
views=`$MYSQL $REMOTECONNECTIONSTR --batch -N -e "select table_name from information_schema.tables where table_type='VIEW' and table_schema='$db';"`
MYVIEWS=""
for view in $views; do
MYVIEWS=${MYVIEWS}" "$view" "
done
echo "VIEWS "$db
if [ -n "$MYVIEWS" ]; then
#VIEWS
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick -Q -f --no-data --skip-comments --skip-triggers --skip-opt --no-create-db --complete-insert --add-drop-table $db $MYVIEWS | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
fi
done
echo "OK!"
Copy mysql database from remote server to local computer
I ran into the same problem. And I could not get it done with the other answers. So here is how I finally did it (yes, a beginner tutorial):
Step 1: Create a new database in your local phpmyadmin.
Step 2: Dump the database on the remote server into a sql file (here I used Putty/SSH):
mysqldump --host="mysql5.domain.com" --user="db231231" --password="DBPASSWORD" databasename > dbdump.sql
Step 3: Download the dbdump.sql file via FTP client (should be located in the root folder)
Step 4: Move the sql file to the folder of your localhost installation, where mysql.exe is located. I am using uniform-server, this would be at C:\uniserver\core\mysql\bin\, with XAMPP it would be C:\xampp\mysql\bin
Step 5: Execute the mysql.exe as follows:
mysql.exe -u root -pYOURPASSWORD YOURLOCALDBNAME < dbdump.sql
Step 6: Wait... depending on the file size. You can check the progress in phpmyadmin, seeing newly created tables.
Step 7: Done. Go to your local phpmyadmin to check if the database has been filled with the entire data.
Hope that helps. Good luck!
Note 1: When starting the uniformer-server you can specify a password for mysql. This is the one you have to use above for YOURPASSWORD.
Note 2: If the login does not work and you run into password problems, check your password if it contains special characters like !. If so, then you probably need to escape them \!.
Note 3: In case not all mysql data can be found in the local db after the import, it could be that there is a problem with the mysql directives of your dbdump.sql
Better yet use a oneliner:
Dump remoteDB to localDB:
mysqldump -uroot -pMypsw -h remoteHost remoteDB | mysql -u root -pMypsw localDB
Dump localDB to remoteDB:
mysqldump -uroot -pmyPsw localDB | mysql -uroot -pMypsw -h remoteHost remoteDB
C:\Users\>mysqldump -u root -p -h ip address --databases database_name -r sql_file.sql
Enter password: your_password
This answer is not remote server but local server. The logic should be the same. To copy and backup my local machine MAMP database to my local desktop machine folder, go to console then
mysqldump -h YourHostName -u YourUserNameHere -p YourDataBaseNameHere > DestinationPath/xxxwhatever.sql
In my case YourHostName was localhost. DestinationPath is the path to the download; you can drag and drop your desired destination folder and it will paste the path in.
Then password may be asked:
Enter password: xxxxxxxx
I am moving away from Linode because I don't have the Linux sysadmin skills necessary; before I complete the transition to a more noob-friendly service, I need to download the contents of a MySQL database. Is there a way I can do this from the command line?
You can accomplish this using the mysqldump command-line function.
For example:
If it's an entire DB, then:
$ mysqldump -u [uname] -p db_name > db_backup.sql
If it's all DBs, then:
$ mysqldump -u [uname] -p --all-databases > all_db_backup.sql
If it's specific tables within a DB, then:
$ mysqldump -u [uname] -p db_name table1 table2 > table_backup.sql
You can even go as far as auto-compressing the output using gzip (if your DB is very big):
$ mysqldump -u [uname] -p db_name | gzip > db_backup.sql.gz
If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):
$ mysqldump -P 3306 -h [ip_address] -u [uname] -p db_name > db_backup.sql
It should drop the .sql file in the folder you run the command-line from.
EDIT: Updated to avoid inclusion of passwords in CLI commands, use the -p option without the password. It will prompt you for it and not record it.
In latest versions of mysql, at least in mine, you cannot put your pass in the command directly.
You have to run:
mysqldump -u [uname] -p db_name > db_backup.sql
and then it will ask for the password.
If downloading from remote server, here is a simple example:
mysqldump -h my.address.amazonaws.com -u my_username -p db_name > /home/username/db_backup_name.sql
The -p indicates you will enter a password, it does not relate to the db_name. After entering the command you will be prompted for the password. Type it in and press enter.
On windows you need to specify the mysql bin where the mysqldump.exe resides.
cd C:\xampp\mysql\bin
mysqldump -u[username] -p[password] --all-databases > C:\localhost.sql
save this into a text file such as backup.cmd
Don't go inside mysql, just open Command prompt and directly type this:
mysqldump -u [uname] -p[pass] db_name > db_backup.sql
Just type mysqldump or mysqldump --help in your cmd will show how to use
Here is my cmd result
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
Go to MySQL installation directory and open cmd from there.
Then execute the below command to get a backup of your database.
mysqldump -u root -p --add-drop-database --databases db> C:\db-dontdelete\db.sql
If you are running the MySQL other than default port:
mysqldump.exe -u username -p -P PORT_NO database > backup.sql
For those who wants to type password within the command line. It is possible but recommend to pass it inside quotes so that the special character won't cause any issue.
mysqldump -h'my.address.amazonaws.com' -u'my_username' -p'password' db_name > /path/backupname.sql
If you have the database named archiedb, use this:
mysql -p <password for the database> --databases archiedb > /home/database_backup.sql
Assuming this is Linux, choose where the backup file will be saved.
For some versions of MySQL try.
sudo mysqldump [database name] > db_backup.sql
mysqldump is another program (.exe file) in the MySQL directory
Program Files\MySQL\MySQL Server 8.0\bin
step 1: First you have to go to the path and open CMD from the folder.
step 2: Then type mysqldump in the CMD
it should display as follows
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
step 3: Then type this command
mysqldump -u [user_name] -p [database_name] > D:\db_dump.sql
Note :
you should provide an absolute path for the output file.
Here I provide D:\
For Windows users you can go to your mysql folder to run the command
e.g.
cd c:\wamp64\bin\mysql\mysql5.7.26\bin
mysqldump -u root -p databasename > dbname_dump.sql
Note: This step only comes after dumping your MySQL file(which most of the answers above have addressed).
It assumes that you have the said dump file in your remote server and now you want to bring it down to your local computer.
To download the dumped .sql file from your remote server to your local computer, do
scp -i YOUR_SSH_KEY your_username#IP:name_of_file.sql ./my_local_project_dir
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%.%MM%.%DD%.%HH%.%Min%.%Sec%"
set drive=your backup folder
set databaseName=your databasename
set user="your database user"
set password="your database password"
subst Z: "C:\Program Files\7-Zip"
subst M: "D:\AppServ\MySQL\bin"
set zipFile="%drive%\%databaseName%-%datestamp%.zip"
set sqlFile="%drive%\%databaseName%-%datestamp%.sql"
M:\mysqldump.exe --user=%user% --password=%password% --result-file="%sqlFile%" --databases %databaseName%
#echo Mysql Backup Created
Z:\7z.exe a -tzip "%zipFile%" "%sqlFile%"
#echo File Compress End
del %sqlFile%
#echo Delete mysql file
pause;
This question already has answers here:
Downloading MySQL dump from command line
(15 answers)
How do I import an SQL file using the command line in MySQL?
(54 answers)
Closed 4 years ago.
Not Duplicate! looking for some feature have phpmyadmin during export in command line
I want to export and import a .sql file to and from a MySQL database from command line.
Is there any command to export .sql file in MySQL? Then how do I import it?
When doing the export/import, there may be constraints like enable/disable foreign key check or export only table structure.
Can we set those options with mysqldump?
some example of Options
Type the following command to import sql data file:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
In this example, import 'data.sql' file into 'blog' database using vivek as username:
$ mysql -u vivek -p -h localhost blog < data.sql
If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
To export a database, use the following:
mysqldump -u username -p databasename > filename.sql
Note the < and > symbols in each case.
If you're already running the SQL shell, you can use the source command to import data:
use databasename;
source data.sql;
mysqldump will not dump database events, triggers and routines unless explicitly stated when dumping individual databases;
mysqldump -uuser -p db_name --events --triggers --routines > db_name.sql
Well you can use below command to export,
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.
Now login to mysql using command,
mysql -u[username] -p
then use "source" command with the file path.
Dump an entire database to a file:
mysqldump -u USERNAME -p password DATABASENAME > FILENAME.sql
Try
mysqldump databaseExample > file.sql
since I have no enough reputation to comment after the highest post, so I add here.
use '|' on linux platform to save disk space.
thx #Hariboo, add events/triggers/routints parameters
mysqldump -x -u [uname] -p[pass] -C --databases db_name --events --triggers --routines | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/ ' | awk '{ if (index($0,"GTID_PURGED")) { getline; while (length($0) > 0) { getline; } } else { print $0 } }' | grep -iv 'set ##' | trickle -u 10240 mysql -u username -p -h localhost DATA-BASE-NAME
some issues/tips:
Error: ......not exist when using LOCK TABLES
# --lock-all-tables,-x , this parameter is to keep data consistency because some transaction may still be working like schedule.
# also you need check and confirm: grant all privileges on *.* to root#"%" identified by "Passwd";
ERROR 2006 (HY000) at line 866: MySQL server has gone away
mysqldump: Got errno 32 on write
# set this values big enough on destination mysql server, like: max_allowed_packet=1024*1024*20
# use compress parameter '-C'
# use trickle to limit network bandwidth while write data to destination server
ERROR 1419 (HY000) at line 32730: You do not have the SUPER privilege and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)
# set SET GLOBAL log_bin_trust_function_creators = 1;
# or use super user import data
ERROR 1227 (42000) at line 138: Access denied; you need (at least one of) the SUPER privilege(s) for this operation
mysqldump: Got errno 32 on write
# add sed/awk to avoid some privilege issues
hope this help!
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