restoring with mysqldump, but where is the data? - mysql

Ok, so I'm in need to restore a table and I do:
mysqldump --opt database table_name < table_name.sql
I hit enter and Done! Well, not really, when I go to see if there is anything on the table it show 0 records.
I have look into the table_name.sql and I see two records.
What am I doing wrong?

mysqldump is the wrong command for restoring from a backup.
You need to run mysql, as in, the mysql client. It's generally something like this:
mysql -u username -p database_name < sqlfile.sql
That will use your file as input to the mysql client, which subsequently executes the SQL.

mysqldump just exports the data to an SQL script. You can restore with this:
mysql db < file.sql

Related

Restore MySQL table from backup

I have created backup of a specific table from my database by using the command below.
mysqldump -u root -p db_name table_name > table.sql
Is it possible to restore the specific backup table without affecting the data of the rest of the tables? Which means, whatever data from my backup file for my table will be the only one affected?
The reverse will be:
mysql database_name < database_name.sql
But this is for the whole database. How to do it with table backup alone?
While the answer given by "Pradeep Reddy" is absolutely correct there is another way of doing it as well from inside mysql prompt using the SOURCE command.
mysql> USE database_name;
mysql> SOURCE /my_fullpath_to_backup_folder/table.sql
mysql -u root -p databasename < mytable.sql

mysqlimport using dump

I need to restore a dumped database, but without discarding existing rows in tables.
To dump I use:
mysqldump -u root --password --databases mydatabase > C:\mydatabase.sql
To restore I do not use the mysql command, since it will discard all existing rows, but instead mysqlimport should do the trick, obviously. But how? Running:
mysqlimport -u root -p mydatabase c:\mydatabase.sql
says "table mydatabase.mydatabase does not exist". Why does it look for tables? How to restore dump with entire database without discarding existing rows in existing tables? I could dump single tables if mysqlimport wants it.
What to do?
If you are concerned with stomping over existing rows, you need to mysqldump it as follows:
MYSQLDUMP_OPTIONS="--no-create-info --skip-extended-insert"
mysqldump -uroot --ppassword ${MYSQLDUMP_OPTIONS} --databases mydatabase > C:\mydatabase.sql
This will do the following:
remove CREATE TABLE statements and use only INSERTs.
It will INSERT exactly one row at a time. This helps mitigate rows with duplicate keys
With the mysqldump performed in this manner, now you can import like this
mysql -uroot -p --force -Dtargetdb < c:\mydatabase.sql
Give it a Try !!!
WARNING : Dumping with --skip-extended-insert will make the mysqldump really big, but at least you can control each duplicate done one by one. This will also increase the length of time the reload of the mysqldump is done.
I would edit the mydatabase.sql file in a text editor, dropping the lines that reference dropping tables or deleting rows, then manually import the file normally using the mysql command as normal.
mysql -u username -p databasename < mydatabase.sql
The mysqlimport command is designed for dumps created with the mysql command SELECT INTO OUTFILE rather than direct database dumps.
This sounds like it is much more complicated than you are describing.
If you do a backup the way you describe, it has all the records in your database. Then you say that you do not want to delete existing rows from your database and load from the backup? Why? The reason why the backup file (the output from mysqldump) has the drop and create table commands is to ensure that you don't wind up with two copies of your data.
The right answer is to load the mysqldump output file using the mysql client. If you don't want to do that, you'll have to explain why to get a better answer.

How to import a MySQL dump from command line WITH overwrite

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'

What is the SQL command that 'resets' the database in MySQL?

Is there a SQL command that 'resets' the database in MySQL?
By reset I mean, all rows are deleted and auto increment are reset.
You're looking for TRUNCATE, as in TRUNCATE TABLE mystuff
More info: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
mysqldump -uuser -hhost -p --no-data name_of_database > backup_file_name.sql
The above command will keep the entire structure of your database but without the data. (that's what the --no-data param does).
Now when you want to reset your database you just:
mysql -uuser -hhost -p < backup_file_name.sql
View the .sql file created to see what its doing. There are a bunch of options you can add to the mysqldump command.
Enjoy.

mysql restore database script with create clause?

mysql> -u username -p [database] < file.sql
restores database. I do not have a create clause in my dumped file, hence I need to create a database and then restore. I can do this to create.
mysql> create database [database];
Fair enough. Now how can I achieve the above restoration in one line? Is there anyway to restore a database by creating the database before that with a single line of code?
I can certainly live without it, but would be nice to know.. Thanks..
(echo "create database XYZ; use XYZ;"; cat file.sql) | mysql -u username -p
could do the trick.