How to export mysql query result so I can import it back again - mysql

I want to dump data from my table by certain criteria, here is the sql query:
"SELECT * FROM document WHERE date BETWEEN 20160101 AND 20160131"
Table that I am selecting from is MyISAM Merge.
I have deleted rows that are matching that query, and I want to import them back from backup, but only those rows.
I have tried making dump like this:
mysql -uroot -proot mydb -e "SELECT * INTO OUTFILE '/tmp/doc.sql' from document WHERE date BETWEEN 20160101 AND 20160131".
But it will generate a file that can not be imported by phpmyadmin import tool. And yet again PMA does not want to do any export of this query result.
Any help ? I can not post structure of the real database/table because of NDA.
EDIT: for all that are having trouble dumping data from MyISAM MRG table, you need to dump from the subtable, not the main one.

If be honest i don't use phpmyadmin at all and phpmyadmin import tool, but i know how it can be done in shell:
1st step - export dump:
mysqldump -uUSER -pPASS \
--databases DB_NAME --tables document \
--where 'date BETWEEN 20160101 AND 20160131' \
--no-create-info > document.dump.sql
2nd step - import dump:
mysql -uUSER -pPASS -DDB_NAME < document.dump.sql
Suppose you have access to shell where db is located and i hope it'll be helpful for you...

mysqldump -uroot -proot mydb document --where="date BETWEEN 20160101 AND 20160131" > dump.sql
mysql -uroot -proot mydb < dump.sql

Related

Is there a way to import all databases into one database with Mysqldump?

I want to gather automatically all my databases into one with Mysqldump, is there a way to do it ?
For example what I want is to move all the tables from DB1, DB2 and DB3 into DB4 (DB4 can already contain some tables or can be created during the import, it doesn't matter to me).
I tried mysqldump -uroot -p --all-databases > dump.sql
Then import it with mysql -uroot -p allInOne < dump.sql
But the resulting database is only filled with it's own data.
I think you will have to do the databases individually but with:
mysqldump -uroot -p DB1 > dump.sql
This will not include the Database name and Use in the dump so you can re-import into the new database.
If you really have a lot of databases then not sure if any of the other options will help :https://dev.mysql.com/doc/refman/5.5/en/mysqldump.html
Might be worth exploring:
mysqldump -uroot -p --all-databases --tables > dump.sql

ssh Mysql dump table parts (11GB DB to smaller pieces)

I'm facing the following:
We have a DB table of 11GB with over 257 million records and need a backup. Exporting via PHPmyAdmin isn't possible (chrome keeps crashing) and backing up with SSH mysqldump tablename will give a insufficient space disk error (error 28).
Now I'd like to know if there is a way to export a mysqldump with a row 0 till ~100.000.000 command so we can make 3 parts (or smaller parts if required).
What I'm using:
mysqldump -p -u username database_name database_table > dbname.sql
[EDIT]
Found out how to get a row of <50.0000.0000 to SQL with the following:
mysqldump -p -u db_name db_table --where='id<50000000'
But the big question remains now, how to go further? Now I want to get all records between 50.000.000 and 100.000.000 ..
Anybody knows the answer if it's possible and what command I should use?
Problem solved:
Part 1 (<50.000.000):
mysqldump -p -u db_name db_table --where='id<50000000' >part_1.sql
Part 2 (>50.000.000 till <100.0000.000):
mysqldump -p -u db_name db_table --where='id>=50000000 &&
id<100000000' >part_2.sql
Part last (>250.000.000)
mysqldump -p -u db_name db_table --where='id>250000000' >part_final.sql
And so on..
mysqldump creates a text file that contains sql statements, if want to take mysql backup in parts then you will have to run mysqldump like this
mysqldump --where "id%2=0" database_name table > table_even.sql
mysqldump --where "id%2=1" database_name table > table_odd.sql
OR
you need to write some program, script to achieve that
I found a nice solution for heavy transfers! This might also help you to avoid to transfer your database in parts (as in this example) - since it does this super fast:
Exporting a full database or in parts as mentioned using mysqldump:
mysqldump -p -u db_name db_table --where='id<50000000' >part_1.sql
To import to the new database - login via terminal to the new database:
mysql -h localhost -upotato -p123456
Enter the database:
USE databasename;
Use the source command:
source /path/to/file.sql;
This works X1000 faster than the standard:
mysql -h localhost_new -upotato -p1234567 table_name < /path/to/file.sql
Since you enter the database.

Import Tables From Database

Sorry if this is dumb question. I want to know if/how to import tables from one database to another using the mysql console/command line. I would love to speed up my workflow with Something like this
CREATE DATABASE dbname;
IMPORT tables into dbname
I have been creating a lot of databases locally using wamp and would like to easily create my database and import some WordPress tables using the command line. Thanks in advanced.
Use CREATE TABLE targetdb.tablename SELECT * FROM sourcedb.tablename to copy the table structure and its content from one database to another database.
Note that auto increment and indizes are not preserved, but can be added to the CREATE TABLE statement explicitly.
You can export one database and then can import that to another database.
mysqldump --host=hostip --user=username --password=password --database databasename > dump.sql
mysql --host=hostip --user=username --password=password databasename < dump.sql
Thanks
You should familiarize yourself with the use of the mysqldump tool (http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html) and command line mysql usage
> mysqldump --no-create-db [DB name] > /path/to/file.sql
> mysql -h [hostname] -u [username] -p[password] [new db name] < /path/to/file.sql
Note here the use of the --no-create-db option, which will remove the CREATE DATABASE part of the SQL, allowing you to create the DB with new name.

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.

Dump only the data with mysqldump without any table information?

I am looking for the syntax for dumping all data in my mysql database. I don't want any table information.
mysqldump --no-create-info ...
Also you may use:
--skip-triggers: if you are using triggers
--no-create-db: if you are using --databases ... option
--compact: if you want to get rid of extra comments
This should work:
# To export to file (data only)
mysqldump -u [user] -p[pass] --no-create-info mydb > mydb.sql
# To export to file (structure only)
mysqldump -u [user] -p[pass] --no-data mydb > mydb.sql
# To import to database
mysql -u [user] -p[pass] mydb < mydb.sql
NOTE: there's no space between -p & [pass]
If you just want the INSERT queries, use the following:
mysqldump --skip-triggers --compact --no-create-info
>> man -k mysqldump [enter in the terminal]
you will find the below explanation
--no-create-info, -t
Do not write CREATE TABLE statements that re-create each dumped table.
Note This option does not not exclude statements creating log file
groups or tablespaces from mysqldump output; however, you can use the
--no-tablespaces option for this purpose.
--no-data, -d
Do not write any table row information (that is, do not dump table
contents). This is useful if you want to dump only the CREATE TABLE
statement for the table (for example, to create an empty copy of the
table by loading the dump file).
# To export to file (data only)
mysqldump -t -u [user] -p[pass] -t mydb > mydb_data.sql
# To export to file (structure only)
mysqldump -d -u [user] -p[pass] -d mydb > mydb_structure.sql
Best to dump to a compressed file
mysqldump --no-create-info -u username -hhostname -p dbname | gzip > /backupsql.gz
and to restore using pv apt-get install pv to monitor progress
pv backupsql.gz | gunzip | mysql -uusername -hhostip -p dbname
Would suggest using the following snippet. Works fine even with huge tables (otherwise you'd open dump in editor and strip unneeded stuff, right? ;)
mysqldump --no-create-info --skip-triggers --extended-insert --lock-tables --quick DB TABLE > dump.sql
At least mysql 5.x required, but who runs old stuff nowadays.. :)
Just dump the data in delimited-text format.
Try to dump to a delimited file.
mysqldump -u [username] -p -t -T/path/to/directory [database] --fields-enclosed-by=\" --fields-terminated-by=,
When attempting to export data using the accepted answer I got an error:
ERROR 1235 (42000) at line 3367: This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
As mentioned above:
mysqldump --no-create-info
Will export the data but it will also export the create trigger statements. If like me your outputting database structure (which also includes triggers) with one command and then using the above command to get the data you should also use '--skip-triggers'.
So if you want JUST the data:
mysqldump --no-create-info --skip-triggers