Modifying SQL dump file - mysql

I try to restore Database from dump file, but it only creates tables. Is there a way to modify dump file so it would create databases ?
mysql -u ***** -p < localhost.sql
Many thanks, Martin.

Related

How to import a specific table from a .sql file in DataGrip?

I accidentally DROP a column from a table in a database. I want to restore that column, luckily I have an .sql file of a backup this morning. I use DataGrip.
Is there a way to import only that table from the .sql file? That way I will not have to reset all other tables too (that other people are working on).
What I ended up doing was
create a temporary database:
mysqladmin -u root -p create temp
restore the .sql into it mysql -u root -p restore < mydump.sql
dump the specific table mysqldump -u root -p restore mytable > mytable.sql
Import the file in DataGrip, right-click the tab and press 'Run...' and set your database as target.

MySQL data backup?

I have an database with tons of data in it.
I also wrote a new program that will change the data in the database.
Is there any way to make a copy of the database before i run my program?
Or is there any other solution?
What I'm thiking is making a copy of the database,
Run the program, which modified the main database. If things goes wrong, how do I use my copied database data to revert the main database?
Please provide steps and commands on linux. I'm new with the database mysql and its commands.
You can use the mysqldump command to make a backup of your database and overwrite the backup file everytime
mysqldump -u <user> -p <db> > dump.sql
Read the following link this will tell you how to dump your database via different ways and restore it.
http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/
Basic command to dump a single database is:
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
Is there any way to make a copy of the database before i run my program?
Yes, there's. You have to use the client utility mysqldump before run you app.
It is something like
shell> mysqldump [options] > dump.sql

Restoring mysql dump text file to database

I am working with analysing big data using opencl. I got the data in mysql dump text file (dump.txt). I want to restore the data into mysql database. Then I can connect to the database and do the other stuff related to parallel programming to analysis big data.
I went through some tutorials and questions related this. I found a related question here. link here. But there they mentioned the way to restore .sql dump file. what can we do for dump text files?
Abstract part of my dump text file is here
"94723605719","435035","2013-06-01 23:51:36","2013-06-01","2013","6","1","7","22
","23","51","36","1","202","-1002409728","1005823215","50000.000",\N,"1613003749
10","50026.000","226","0","0","94723605719","34399725","0","0","0",\N
"94722616878","435014","2013-06-01 23:51:52","2013-06-01","2013","6","1","7","22
","23","51","52","1","202","-1002099361","1005511506","50000.000",\N,"1613002394
31","50127.000","157","0","0","94722616878","34438596","0","0","0",\N
"94726556777","435022","2013-06-01 23:51:52","2013-06-01","2013","6","1","7","22
","23","51","52","1","202","-1002496570","1005910182","50000.000",\N,"1614002967
42","61046.000","226","0","0","94726556777","34399744","0","0","0",\N
Any help is appreciated.
Thanks in advance.
If you have 'real' dump, then you can use:
mysql -u root -p -h local host < your_dump.sql
That said, your example listed in the question seems to hint that you just have the values in a CSV file, in which case you need to first create the table and then load the data into it:
LOAD DATA INFILE 'your_file.txt'
INTO TABLE your_table
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"';
See MySQL manual for LOAD DATA INFILE for more details.
I'm used to migrate MySql databases and the best way for me is like this:
Export Database(backup):
mysqldump -u root -p [database_name] > [dump_filename].sql
Import Database:
mysql -u root -p [database_name] < [dump_filename].sql
If the database you want to restore doesn't already exist, you need to create it first.
On the command-line, if you're in the same directory that contains the dumped file, use these commands (with appropriate substitutions):
C:\> mysql -u root -p
mysql> create database mydb;
mysql> use mydb;
mysql> source db_backup.dump;
https://dev.mysql.com/doc/mysql-backup-excerpt/5.6/en/using-mysqldump.html
http://meta.wikimedia.org/wiki/Database_dump_and_restore_examples

mysql restore and overwrite

I have a backup of my entire host 11 DB and some tables. I messed some stuff up and I want to restore and while restoring OVERWRITE.
mysql -u root -p < plasesavetheday.sql
I get errors about database existing and Duplicate entry '11' for key 'id'.
how can I tell the import or edit the file to be an overwtire.
thanks
It sounds like your dump file has a CREATE DATABASE in it. If so, you should drop the database before you load the dump file:
$ mysqladmin -u root -p drop database
Now you can load the dump file:
$ mysql -u root -p < plasesavetheday.sql
Of course, you might want to backup the database first, just in case!

Backing up a MySQL database and restoring it under another name

I am trying to do some maintenance on MySQL database data and I created a dump file with the backed up current database.
I want to restore all that data into another database called something like original_db_name_test
Is there a command for that?
This depends on how you invoked mysqldump
If you used mysqldump dbname, then your dump contains neither CREATE DATABASE nor USE DATABASE.
Just create the database with the new name and feed the dump to mysql -D new_dbname.
If you used mysqldump --database dbname, then the dump contains CREATE DATABASE and USE DATABASE statements.
You need to comment them out or replace with new_dbname.
mysql -u usernamehere -p original_db_name_test < yourdumpfilehere.sql
If you used mysqldump to create the dump file, simply:
Create a new database (use the mysqladmin command line tool - "mysqladmin create [new database name]").
Edit the dump file to add a "USE [new database name];" at the top. (There might be an existing use statement that's commented out, so you can change this and un-comment it.)
Import the dump into the new table via "mysql -u <user name> -p < [dump file name]".
Incidentally, when creating a dump via mysqldump, I'd be tempted to use the "--add-drop-table" option, as this will cull any existing table with the same name prior to issuing the table creation statement.
you can use the 'MySQL Workbench' Application and do this with a nice gui