I am attempting to restore my mysql database to my website, and all of the tables in my database get dropped into individual files, so I am trying to figure out how can I restore all of the database .sql files through SSH with a single (or easy command) instead of restoring all 100 tables individually.
cat *.sql > data.sql
mysql -u <username> -p < data.sql
It depends on how you created the individual files - if they have all the instructions for recreating the tables (i.e., "Drop if exists...", "Create ...", and "Insert into ..."), then you can either concatenate them into mysql:
cat *.sql | mysql -u xxx -pxxx dbname
or write a script to do it
#!/bin/sh
mysql -u xxx -pxxx dbname < file001.sql
mysql -u xxx -pxxx dbname < file002.sql
The second choice lets you more easily control the order of files processed.
Finally, you might want to create your backups in a more convenient way - check out mysqldump for how to dump a database (or several!) into one file (basically, "mysqldump -u xxx -pxxx dbname > dbname.sql", but there are some helpful flags you might want to add).
Related
So I wanted to format my system and I had a lot of works that I have done on my localhost that involves databases. I followed the normal way of backing up the database by exporting it into an SQL file but I think I made a mess by making a mistake of backing up everything in one SQL file (I mean the whole localhost was exported to just one SQL file).
The problem now is: when I try to import the backed up file I mean the (localhost.sql), I get an error like
tables already exist.
information_schema
performance_schema
an every other tables that comes with Xampp, which has been preventing me from importing the database.
These tables are the phpmyadmin tables that came with Xampp. I have been trying to get past this for days.
My question now is that can I extract different databases from the same compiled SQL database file?
To import a database you can do following things:
mysql -u username -p database_name < /path/to/database.sql
From within mysql:
mysql> use database_name;
mysql> source database.sql;
The error is quite self-explanatory. The tables information_schema and performance_schema are already in the MySQL server instance that you are trying to import to.
Both of these databases are default in MySQL, so it is strange that you would be trying to import these into another MySQL installation. The basic syntax to create a .sql file to import from the command line is:
$ mysqldump -u [username] -p [database name] > sqlfile.sql
Or for multiple databases:
$ mysqldump --databases db1 db2 db3 > sqlfile.sql
Then to import them into another MySQL installation:
$ mysql -u [username] -p [database name] < sqlfile.sql
If the database already exists in MySQL then you need to do:
$ mysqlimport -u [username] -p [database name] sqlfile.sql
This seems to be the command you want to use, however I have never replaced the information_schema or performance_schema databases, so I'm unsure if this will cripple your MySQL installation or not.
So an example would be:
$ mysqldump -uDonglecow -p myDatabase > myDatabase.sql
$ mysql -uDonglecow -p myDatabase < myDatabase.sql
Remember not to provide a password on the command line, as this will be visible in plain text in the command history.
The point the previous responders seem to be missing is that the dump file localhost.sql when fed into mysql using
% mysql -u [username] -p [databasename] < localhost.sql
generates multiple databases so specifying a single databasename on the command line is illogical.
I had this problem and my solution was to not specify [databasename] on the command line and instead run:
% mysql -u [username] -p < localhost.sql
which works.
Actually it doesn't work right away because of previous attempts
which did create some structure inside mysql, and those bits in localhost.sql
make mysql complain because they already exist from the first time around, so
now they can't be created on the second time around.
The solution to THAT is to manually edit localhost.sql with modifications like
INSERT IGNORE for INSERT (so it doesn't re-insert the same stuff, nor complain),
CREATE DATABASE IF NOT EXISTS for CREATE DATABASE,
CREATE TABLE IF NOT EXISTS for CREATE TABLE,
and to delete ALTER TABLE commands entirely if they generate errors because by then
they've already been executed ((and INSERTs and CREATEs perhaps too for the same reasons). You can check the tables with DESCRIBE TABLE and SELECT commands to make sure that the ALTERations, etc. have taken hold, for confidence.
My own localhost.sql file was 300M which my favorite editor emacs complained about, so I had to pull out bits using
% head -n 20000 localhost.sql | tail -n 10000 > 2nd_10k_lines.sql
and go through it 10k lines at a time. It wasn't too hard because drupal was responsible for an enormous amount, the vast majority, of junk in there, and I didn't want to keep any of that, so I could carve away enormous chunks easily.
unzip -p /pathoffile/database_file.zip | mysql -uusername -p databsename;
Best way to import database in localhost has simple 5 steps:
zip sql file first to compress databse size.
go to termianl.
create empty database.
Run Command unzip databse With Import database: unzip -p /pathoffile/database_file.zip | mysql -uusername -p databsename;
Enter Password
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.
I have a large sql file (500mb) and want to split it in chunks.
I used the shell command split but it doesn't split context-aware before a special pattern (e.g. INSERT) and thus breaks the SQL statement.
The aim is to have two 250mb files both still containing only valid SQL commands. Is this possible?
Use:
mysqldump -u admin -p database1 > /backup/db/database1.sql
or
mysqldump -u admin -p --all-databases > /backup/db/all_databases.sql
If you have only MyISAM tables you can use:
mysqlhotcopy -u admin -p password123 database1 /backup
for faster backups. mysqlhotcopy doesn't generating sql but copying the files of the database.
For recovery of mysqldumped databases use:
mysql -u admin -p database1 < database.sql
or
mysql -u admin -p <all_databases.sql
For mysqlhotcopy:
To restore the backup from the mysqlhotcopy backup, simply copy the files from the backup directory to the /var/lib/mysql/{db-name} directory. Just to be on the safe-side, make sure to stop the mysql before you restore (copy) the files. After you copy the files to the /var/lib/mysql/{db-name} start the mysql again.
See here: http://www.thegeekstuff.com/2008/07/backup-and-restore-mysql-database-using-mysqlhotcopy/
I back up my production database with the following command:
mysqldump -u root --opt --skip-extended-insert --databases my_production_db
The resulting dump file has the following lines near the top:
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `my_production_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `my_production_db `;
In order to restore the database to a different destination ie. my_debvelopment_db I have to open the dump file and edit the bits where the database is named.
Then I run:
mysql -u root -p <password> < mydumpfile
I have not figured out another way to do it.
As the database gets bigger this becomes impractical.
Am I missing something? Cant I somehow specify where I want to restore the database? Would I need a different backup command?
#minaz answer was good, but I want to append a little bit more.
The problem was caused by --databases keyword. If you omit the keyword, it will not contain any database creation contents.
So, Dump without --databases keyword.
mysqldump -u username -p database_name > dump.sql
And restore it with the target database name.
mysql -u username -p target_database_name < dump.sql
Also, there are several ways to do this. See the similar problem on here (dba.stackexchange).
If you drop the option --databases but still specify the database name, you will NOT get the create database statements. ie:
mysqldump -u root --opt --skip-extended-insert my_production_db
On your dev machine simply create any database you wish to restore to.
If you already have your dump you can strip the commands for creating and using the database. Simply remove the fourth and the new fifth line.
sed '4d' dump.sql | sed '5d' > dump-striped.sql
On windows xampp I used following commands to achieve this
Export
mysqldump -u root -p mydb > mydb.sql
Import
mysql -u root -p mynewdb < mydb.sql
The database "db" is backuped in backup.sql. Is there a way to restore database from script with different from "db" name?
thank you in advance!
Sure, when you import it you do this right:
mysql -uuser -ppassword databasename < mydump.sql
You can put anything you want where I wrote databasename - as long as that database actually exists :)
This depends on how you created your MySQL dB dump file
for example, if you do
mysqldump -h localhost -u user mydb -pXXX > mydb.sql
There won't be any CREATE DATABASE statements in your sql dump file. But I think you can only backup one database.
If you create your mysql dump file with --database or --all-databases option
for example
mysqldump -h localhost -u user --database mydb -pXXX > mydb.sql
mysqldump -h localhost -u user --all-databases -pXXX > alldb.sql
then you will see CREATE DATABASE statement in your mysql dump file. If you want a different dB name, you will need to change it before DB restore.
If the name of the database is include the SQL file, I didn't find any other way than modify the SQL file.
My favorite command to do it :
sed -i "s/\`old_db_name\`/\`new_db_name\`/g" my_sql_file.sql
Open up the .sql file and change the database name inside.
You can use a text editor, like Notepad or gedit.