How to dump result of a sql query into a file - mysql

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/

Related

Backing up a Single Table in mysql

what is the proper way to backup a Single file in Mysql and save it as a .bak Extention?
I tried
mysqldump db_name table_name > table_name.sql
but i just get a syntax error saying its not a proper statement
Thankyou
mysqldump -uUsername -pPassword db_name table_name>C:\table_name.sql
-u: your db username
-p your db password
C:\table_name.sql need be absolute path
First Make Sure that you run your MYSQL then
mysqldump –u[user name] -p[password] -h[hostname] [database name] [table_name] > dump.sql

MySQL : Restore dump file

I have a single .sql file which is 800MB in size and contains a few of databases including tables and datas.
The problem is, how to restore this kind of dump since there is no CREATE DATABASE syntax in the file?
I try mysql> -u root -p --all-database < c:\data.sql but no joy.
Conducted a backup of the following.
mysqldump -u xxx -p --all-database > c:\data.sql
Or, in the database unit
mysqldump -u xxx -p --databases db_name > c:\data.sql
Recovery in the following code.
mysql -u root -p < c:\data.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.

mysql dump, windows, seperate files for each DB, all databases option

Does anybody have an example on how to dump all databases uses mysql dump? And possible all a new file for each DB?
I'm using the follow command:
mysqldump -u root -p pw --all-databases > backup.sql;
It's returning with "You have an error in your SQL sytax";
Thanks!
There is an error in your command, it should be no space after -p,
like
mysqldump -u root -ppw --all-databases > backup.sql;
I not sure how many database you have, usually you can do this :-
mysqldump -u root -ppw db_a > db_a.sql;
mysqldump -u root -ppw db_b > db_b.sql;
...
... for all the databases

create dump file from database in mysql

following is the that I create dump from mysql database.
mysqldump -u root tempbkk > ttt.dump
but I want to create a dump that exclude one or more file while creating dump from database we select.What is the command for that ?
mysqldump -u user -p some_database > some_database_dump.sql
mysqldump can skip tables, you need the --ignore-table parameter. Check out the manual of mysqldump.
mysqldump -u <user> -p<password> --databases <dbname> -r <NameofBackup.sql>
Simply type this command mysqldump -u user -p database_name_in_database > name_of_file.sql
it will ask password for user. and there you go, your dumb file is ready. on same the location from where you run the command