How to take the backup of full database using mysql? - mysql

How to take the backup of full database using mysql?

You could use mysqldump. Have a look at the reference manual: 4.5.4. mysqldump — A Database Backup Program
[mysqldump] can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both.

http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html

Here is an example using mysqldump, you can create an Scheduled task or a cron job to automatize the backup process:
mysqldump --opt --host=localhost --user=myUser --password=myPass --result-file=C:\Backups\myBackupFile.sql myDatabase

If you have access to phpMyadmin, try the export function as it's probably the easiest way.

Related

Complete database reset for MySQL dump?

This may seem like a very dumb question but I didn't learn it in any other way and I just want to have some clarification.
I started to use MySQL a while ago and in order to test various scenarios, I back up my databases. I used MySQL dump for that:
Export:
mysqldump -hSERVER -uUSER -pPASSWORD --all-databases > filename.sql
Import:
mysql -hSERVER -uUSER -pPASSWORD < filename.sql
Easy enough and it worked quite well up until now, when I noticed a little problem with this "setup": It does not fully "reset" the databases and tables. If, for example, there is an additional table added AFTER a dump file has been created, that additional table will not disappear if you import the same dump file. It essentially only "corrects" tables already there and recreates any databaes or tables missing, but does not remove any additional tables, which happen to have names that are not in the dump file.
What I want to do is to completely reset all the databases on a server when I import such a dump file. What would be the best solution? Is there a special import function reserved for that purpose or do I have to delete the databases myself first? Or is that a bad idea?
You can use the parameter --add-drop-database to add a "drop database" statement to the dump before each "create database" statement.
e.g.
mysqldump -hSERVER -uUSER -pPASSWORD --all-databases --add-drop-database >filename.sql
see here for details.
There's nothing magic about the dump and restore processes you describe. mysqldump writes out SQL statements that describe the current state of the database or databases you are dumping. It has to fetch a list of tables in each database you're dumping, then it has to read the tables one by one and write them out as SQL. On databases of any size, this takes time.
So, if you create a new table while mysqldump is running, it may not pick up that new table. Similarly, if your application software changes contents of tables while mysqldump is running, those changes may or may not show up in the backup.
You can look at the .sql files mysqldump writes out to see what they have picked up. If you want to be sure that your dumped .sql files are perfect, you need to run mysqldump on a quiet server -- one where nobody is running data definition language.
MySQL hot backup solutions are available. You may need to look into that.
The OP may want look into
mysql_install_db
if they want a fresh start with the post-install default
settings before restoring one or more dumped DBs. For
production servers, another useful script is:
mysql_secure_installation
Also, they may prefer to dump the DB(s) they created separately:
mysqldump -hSERVER -uUSER -pPASSWORD --database foo > foo.sql
to avoid inadvertently changing the internal DBs:
mysql, information_schema, performance_schema.

Avoid blocking mysql and PC with mysqldump

I have a root access to a mysql server, I need to dump ALL the database inside the server.
I tried with a simple mysqldump, but the server and pc seems blocked due to the large size of the databases and tables. Can I "optimize" this DUMP avoiding locking the server (and PC) ?
Thank you so much!
EDIT:
I want to EXPORT all the databases from a Mysql Server.
I need to understand what options to pass to mysqldump to avoid blocking:
The Mysql Server <---- it CAN'T go down
The PC that is goind to do this DUMP
You can disable locking:
mysqldump --skip-lock-tables
Of course you will not be able to create a consistent dump that way, so I would not recommend to use that option.
When only using MyISAM and ARCHIVE tables you might want to consider using mysqlhotcopy (included with a regular mysql package). There is similar software for other table engines like InnoDB available.
Another option is using a replication slave for backup.
Fire the dump command from commandline. :
mysqldump <other mysqldump options> --routines > outputfile.sql
If we want to backup ONLY the stored procedures and triggers and not the mysql tables and data then we should run something like:
mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt <database> > outputfile.sql
If you need to import them to another db/server you will have to run something like:
mysql <database> < outputfile.sql

Restoring selective tables from an entire database dump?

I have a mysql dump created with mysqldump that holds all the tables in my database and all their data. However I only want to restore two tables. (lets call them kittens and kittens_votes)
How would I restore those two tables without restoring the entire database?
Well, you have three main options.
You can manually find the SQL statements in the file relating to the backed up tables and copy them manually. This has the advantage of being simple, but for large backups it's impractical.
Restore the database to a temporary database. Basically, create a new db, restore it to that db, and then copy the data from there to the old one. This will work well only if you're doing single database backups (If there's no CREATE DATABASE command(s) in the backup file).
Restore the database to a new database server, and copy from there. This works well if you take full server backups as opposed to single database backups.
Which one you choose will depend upon the exact situation (including how much data you have)...
You can parse out CREATE TABLE kittens|kitten_votes AND INSERT INTO ... using regexp, for example, and only execute these statements. As far as I know, there's no other way to "partially restore" from dump.
Open the .sql file and copy the insert statements for the tables you want.
create a new user with access to only those 2 tables. Now restore the DB with -f (force) option that will ignore the failed statements and execute only those statements it has permission to.
What you want is a "Single Table Restore"
http://hashmysql.org/wiki/Single_table_restore
A few options are outlined above ... However the one which worked for me was:
Create a new DB
$ mysql -u root -p CREATE DATABASE temp_db
Insert the .sql file ( the one with the desired table ) into the new DB
$ mysql -u root -p temp_db < ~/full/path/to/your_database_file.sql
dump the desired table
$ mysqldump -u root -p temp_db awesome_single_table > ~/awesome_single_table.sql
import desired table
$ mysql -u root -p original_database < ~/awesome_single_table.sql
Then delete the temp_db and you're all golden!

How to backup my MySQL's databases on Windows Vista?

How can I backup my MySQL's databases? I'm using Windows Vista and MySQL 5.1.
I have found the folder "C:\Users\All Users\MySQL\MySQL Server 5.1\data" with all my database files and copy them, but how can I restore them if I need?
Thank you.
You could use the mysqldump tool:
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
That way you'd get SQL files that you could just execute.
You can also go surf to localhost/phpmyadmin and go to 'export' and select the databases you want to export.
The backup process does not have anything to do with your operating system. Simply export your databases.
You can back up the database files directly, but this can be dangerous if the database is in active use at the time you do the backup. There's no guarantee that you'll make a consistent and valid backup if a query starts modifying on-disk data. You may end up with broken tables.
The safest route is to use mysqldump to output a set of sql statements which can recreate the database completely (table creation + data) in one go. Should you need to restore from backup, you can simply feed this dump file back to mysql:
mysqldump -p -u username nameofdatabase > backup.sql
and restore via:
mysql -p -u username nameofdatabase < backup.sql
The .sql file is just a plaintext dump of all the queries required to rebuild the table(s) and their data.

In MySQL, what are the practices to backup databases?

I am using mysql
How often do you back up your database?
How do you normally backup your database?
Export all data into sql or cvs format and keep it in a folder??
Setting up a cronjob to run a script that does a mysqldump and stores the dump on a separate disk from the database itself (or a remote server) is a quite easy and efficient way to backup a database in my opinion. You could even have it dump every database with the --all-databases switch
If you have more than one MySQL server, you could also use replication
Frequency of backups depends on how much data you are willing to lose in case of a failure.
How Often - Depends on the activity and traffic you have on your Database.
I use phpMyAdmin and then click on the export tab and export the database as .sql
you can reach phpmyadmin by either localhost/phpmyadmin if you are running xampp/wamp/lamp etc or you can click on the 'phpmyadmin' link in your webhost's cPanel if you are hosting your webstie online.
I backup databases daily, using 'mysqldump [options] db_name [tables]', and export all data into sql.
Of course, this depend on the size of your databases.
I use mysqldump:
mysqldump -uroot -p database_name > ~/backups/database_name-$(date +%s).sql
1.If you want the easiest way to back-up the database then you can opt for phpMyadmin.
2.If you prefer command line then follow this way.
You would simply modify the path and filename to reflect the specifics of your new backup file, like so:
C:\ mysql -uroot -p [DatabaseName] < C:/backups/[backUpDatabaseName.sql]
3.Mysql dump.
C:\ mysqldump [myDatabaseName] > [ExportDatabaseName]
You can also look hotbackup