Export database tables to sql files individually through SSH - mysql

As the question says, I have been trying to export each table of a database in individual .sql files. I know mysqldump -u username -p DBName TableName > Export.sql exports one specific table, but is there any way I can loop it and export all tables in individual files using SSH command?

Find the tables in the database and iterate over it.
mysql -u USERNAME -p -N -D DATABASENAME -e 'show tables';
This will list the tables of a particular database. Do something like this to export all the tables of the database. (Note: you need to remove the formatting charaters)
for tab in `mysql -u USERNAME -p -N -D DATABASENAME -e 'show tables';` do
mysqldump -u USERNAME -p DATABASENAME $tab > ${tab}.sql
done

Related

MySQL 5.7 - Export Dump [duplicate]

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

How to dump result of a sql query into a file

Ok so I am in a situation where I need to dump the results of a sql query into a file and this is for backup purpose I tried running th following from my terminal :
mysql -e "select * from products where brand_id=5" -u root -p database_name > dumpfile.sql
this dumps the data but with the "insert into statements", so if later on I want to dump this data back into my database I will be re-composing it. And that is not the correct approach. So please suggest me how do I dump query results for back up purpose ?
The following command line (using --where option) should work:
mysqldump -u root -p database_name products --where="brand_id=5" > dumpfile.sql
More information about the mysqldump command: https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html
use mysqldump
mysqldump -u root -p database_name products --where "brand_id=5">
dump.sql
For more read:
http://mechanics.flite.com/blog/2012/11/15/3-methods-to-extract-a-subset-of-your-data-using-mysqldump/

how can i take backup of mysql database tables

i want to move mysql database tables which resides in one computer to another computer. how can i create dump file as we created in Oracle ?
i m using exp command but not working.
Use mysqldump.
mysqldump -u <username> -p<password> <db_name> > <filename>.sql
To import, create empty database named <db_name>, thena -
mysql -u <username> -p<password> <db_name> < <filename>.sql
To export all databases -
mysqldump -u <username> -p<password> --all-databases > <filename>.sql
mysqldump -u <username> -p<password> -h <hostname> <dbname> <tablename> > filename.sql
Now you may need to take the dump of just the schema. For example you use a command called like.
mysql> show create table tablename;
this will give you the query that created the table. Now for some reason you need to take schema dump of all the tables inside you database/databases you may use -d option like this
mysqldump -d -u <username> -p<password> -h <hostname> <dbname> > filename.sql
The -d option means "without data".
Now you have full dump without the data and just the schema.

How to import a single table in to mysql database using command line

I had successfully imported a database using command line, but now my pain area is how to import a single table with its data to the existing database using command line.
Linux :
In command line
mysql -u username -p databasename < path/example.sql
put your table in example.sql
Import / Export for single table:
Export table schema
mysqldump -u username -p databasename tableName > path/example.sql
This will create a file named example.sql at the path mentioned and write the create table sql command to create table tableName.
Import a single table into database
mysql -u username -p databasename < path/example.sql
This command needs an sql file containing data in form of insert statements for table tableName. All the insert statements will be executed and the data will be loaded.
Export:
mysqldump --user=root databasename > whole.database.sql
mysqldump --user=root databasename onlySingleTableName > single.table.sql
Import:
Whole database:
mysql --user=root wholedatabase < whole.database.sql
Single table:
mysql --user=root databasename < single.table.sql
Importing the Single Table
To import a single table into an existing database you would use the following command:
mysql -u username -p -D database_name < tableName.sql
Note:It is better to use full path of the sql file tableName.sql
First of all, login to your database and check whether the database table which you want to import is not available on your database.
If it is available, delete the table using the command. Else it will throw an error while importing the table.
DROP TABLE Table_Name;
Then, move to the folder in which you have the .sql file to import and run the following command from your terminal
mysql -u username -p databasename < yourtable.sql
The terminal will ask you to enter the password. Enter it and check the database.
Command Line
Import / Export for single table:
Exporting table schema
-> mysqldump -u your_user_name -p your_database_name table_name > test.sql
This will create a file named test.sql and creates table sql command to create table table_name.
Importing data into table
-> mysql -u your_user_name -p database_name table_name < test.sql
Make sure your test.sql file is in the same directory, if not navigate through the path and then run the command.
It works correctly...
C:\>mysql>bin>mysql -u USERNAME DB_NAME < tableNameFile.sql
please note .sql file specified your current database..
We can import single table using CMD as below:
D:\wamp\bin\mysql\mysql5.5.24\bin>mysql -h hostname -u username -p passowrd databasename < filepath
If you're in the pwd of an SQL dump and you need a table from that, do this:
sed -n '/-- Table structure for table `'TableNameTo_GrabHere'`/,/-- Table/{ /^--.*$/d;p }' dump_file_to_extract_from.sql > table_name_here.sql
Then just import the table you extracted from the above into the needed database
you can do it in mysql command instead of linux command.
1.login your mysql.
2.excute this in mysql command:
use DATABASE_NAME;
SET autocommit=0 ; source ABSOLUTE_PATH/TABLE_SQL_FILE.sql ; COMMIT ;
if you already have the desired table on your database, first delete it and then run the command below:
mysql -u username -p databasename < yourtable.sql
From server to local(Exporting)
mysqldump -u username -p db_name table_name > path/filename.sql;
mysqldump -u root -p remotelab welcome_ulink >
/home_local/ladmin/kakalwar/base/welcome_ulink.sql;
From local to server(Importing)
mysql -u username -p -D databasename < path/x/y/z/welcome_queue.sql
mysql -u root -p -D remotelab <
/home_local/ladmin/kakalwar/instant_status/db_04_12/welcome_queue.sql
Also its working. In command form
cd C:\wamp\bin\mysql\mysql5.5.8\bin //hit enter
mysql -u -p databasename //-u=root,-p=blank
It would be combination of EXPORT INTO OUTFILE and LOAD DATA INFILE
You need to export that single table with EXPORT INTO OUTFILE, this will export table data to a file. You can import that particular table using LOAD DATA INFILE
Refer doc1 , doc2
To import a particular table in database follow below command.
Here table_name.sql is dump of taht particular table that you are going to import
mysql -u root -p database_name table_name < table_name.sql
To export a particular table from database follow below command.
mysqldump -u root -p database_name table_name > table_name.sql
-> mysql -h host -u user -p database_name table_name < test_table.sql
Using a temporary database could be a solution depending on the size of the database.
mysql -u [username] -p -e "create database tempdb"
mysql -u [username] -p tempdb < db.sql
mysqldump -u [username] -p tempdb _table_to_import_ > table_to_import.sql
mysql -u [username] -p maindb < table_to_import.sql
To import a table into database, if table is already exist then add this line in your sql file DROP TABLE IF EXIST 'table_name' and run command mysql -u username -p database_name < import_sql_file.sql.
Please verify the sql file path before execute the command.
Open the backup file in the VScode and search the table name copy the create table and insert command for the table. Copy and execute those two commands in the database where it is required.
Use the below command to import a single table into the database on RDS
mysql -h rds_end_point -u username -p databasename < example.sql
First of all take backup of your both database, step 2 select table which you want to export now select export button now download sql file now you have to import into another database simply select database and then import sql file ... simple and easy.

How to export a mysql database using Command Prompt?

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