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.
Related
Normally, when I backup the database, I run a command like this:
mysqldump -uuser -p -hhost -Ddatabase > C:\TEMP\db_2018-04-05.sql
Inside that file, there are DROP table statements. This is normally fine, but I've modified my localhost to have a different schema than the production database.
If I execute this file, it will blow away the important changes to the database schema on my localhost.
All I need is the INSERT statements. Is there any flag I can pass mysqldump to achieve this?
Include the command for the mysqldump ignore the structure.
mysqldump --no-create-info ...
All you need is add --skip-add-drop-table option when using mysqldump.
$ mysqldump -uuser -p -hhost -Ddatabase --skip-add-drop-table > C:\TEMP\db_2018-04-05.sql
Now no DROP TABLE IF EXISTS in SQL files.
see docs of mysql on --skip-add-drop-table.
Normally, when I backup the database, I run a command like this:
mysqldump -uuser -p -hhost -Ddatabase > C:\TEMP\db_2018-04-05.sql
Inside that file, there are DROP table statements. This is normally fine, but I've modified my localhost to have a different schema than the production database.
If I execute this file, it will blow away the important changes to the database schema on my localhost.
All I need is the INSERT statements. Is there any flag I can pass mysqldump to achieve this?
Include the command for the mysqldump ignore the structure.
mysqldump --no-create-info ...
All you need is add --skip-add-drop-table option when using mysqldump.
$ mysqldump -uuser -p -hhost -Ddatabase --skip-add-drop-table > C:\TEMP\db_2018-04-05.sql
Now no DROP TABLE IF EXISTS in SQL files.
see docs of mysql on --skip-add-drop-table.
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
this is probably massively simple, however I will be doing this for a live server and don't want to mess it up.
Can someone please let me know how I can do a mysqldump of all databases, procedures, triggers etc except the mysql and performance_schema databases?
Yes, you can dump several schemas at the same time :
mysqldump --user=[USER] --password=[PASS] --host=[HOST] --databases mydb1 mydb2 mydb3 [...] --routines > dumpfile.sql
OR
mysqldump --user=[USER] --password=[p --host=[HOST] --all-databases --routines > dumpfile.sql
concerning the last command, if you don't want to dump performance_schema (EDIT: as mentioned by #Barranka, by default mysqldump won't dump it), mysql, phpMyAdmin schema, etc. you just need to ensure that [USER] can't access them.
As stated in the reference manual:
mysqldump does not dump the INFORMATION_SCHEMA or performance_schema database by default. To dump either of these, name it explicitly on the command line and also use the --skip-lock-tables option. You can also name them with the --databases option.
So that takes care of your concern about dumping those databases.
Now, to dump all databases, I think you should do something like this:
mysqldump -h Host -u User -pPassword -A -R > very_big_dump.sql
To test it without dumping all data, you can add the -d flag to dump only database, table (and routine) definitions with no data.
As mentioned by Basile in his answer, the easiest way to ommit dumping the mysql database is to invoke mysqldump with a user that does not have access to it. So the punch line is: use or create a user that has access only to the databases you mean to dump.
There's no option in mysqldump that you could use to filter the databases list, but you can run two commands:
# DATABASES=$(mysql -N -B -e "SHOW DATABASES" | grep -Ev '(mysql|performance_schema)')
# mysqldump -B $DATABASES
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'