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!
Related
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.
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.
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 googled a lot and i can't found nothing about it !
[root#someday backups]# mysql -u username_1 -p db_1 < tables_to_import/tables.sql
ERROR 1050 (42S01) at line 19: Table 'ps_customer' already exists
with mysql -f is the same. i wish simply import that .sql and rewrite that tables, can someone help me ?
p.s. i know that when you export a db you can choose option "DROP TABLE" but if i have a backup, without this declaration ? how can i force ? Thanks
When you do mysqldump add --add-drop-table (like twihoX mentioned). Then do your import as usual. So something like:
mysqldump --add-drop-table -u user -p db_1 > dumpfile.sql
mysql -u user -p db_1 < dumpfile.sql
Are you trying to overwrite the entirety of the database? If so, you could manually drop all the tables, and then run your import script. This is easy to do in phpmyadmin. If you're using the CLI, the fastest way would be to use DROP DATABASE databasename and then create database, though I think you'd then have to re-grant privileges for any non-root users.
Another option would be to open up your dump file and add DROP TABLE tablename before each of the CREATE TABLE commands. You could probably do this easily with some clever regex.
I'd suggest --add-drop-table option.
I know this question is a bit old and it's been marked as answered correctly, I'd just like to add this here for those (like me) who didn't use --add-drop-table when exporting.
What you can do is log in to MySQL and drop the tables that you plan to overwrite, then use --force on import.
So login to MySQL
mysql -h HOSTNAME - USERNAME -p
then tell mysql which database you wish to use
mysql> use DATABASE_NAME
drop tables that you want to overwrite
mysql> DROP TABLE my_images;
Then you are ready to import, so log out of mysql and back to where your SQL file was uploaded and run the following command
$ mysql --force -uDB_USER -p DB_NAME < myuploadedfile.sql
This will force MySQL to continue importing any new tables and ignore the 'table already exists error'
I have a trouble in restoring MySQL table back to the database from command line. Taking backup of a table is working with mysqldump.Taking backup and restoring of a database is also working properly. I have used:
mysql -uroot -p DatabaseName TableName < path\TableName.sql
Thanks in advance
Ah, I think I see the problem here.
Your backup script looks fine. tbl_name works correctly as the optional 2nd argument.
To restore, you should simply run
mysql -uroot -p DatabaseName < path\TableName.sql
Running man mysql would have shown you the correct arguments and options
mysql [options] db_name
As your backup script only contains one table, only that table will be restored into your database.
Taking backup
mysqldump -u -p mydatabase table1 > database_dump.sql
restoring from backup flie need not include table name
mysql -u -p mydatabase < database_dump.sql
Best way to restore your database:
open cmd at bin folder
login to mysql:
mysql -uroot -pyour_password
show databases;
use db_name;
now hit source and put the complete path from address bar where your sql file is stored and hit ;
for example :
source db_name.sql;
Copy your db.sql file to your Mysql Server if you are in a remote machine:
$rsync -Cravzp --progress db.sql user#192.168.10.1:/home/user
Now you can go to your remote server as:
$ssh -l user 192.168.10.1
In the Mysql Server you must to do this:
user#machine:~$mysql -h localhost -u root -p
Obs: The file db.sql must be in the same place (/home/user).
Now type this command in you Mysql Server:
mysql>'\'. db.sql + Enter. Obs: Remove all ' from this command to work