Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to backup whole MySQL database with all users and permissions and passwords?

I need to backup the whole of a MySQL database with the information about all users and their permissions and passwords.

I see the options on http://www.igvita.com/2007/10/10/hands-on-mysql-backup-migration/, but what should be the options to backup all of the MySQL database with all users and passwords and permissions and all database data?

Just a full backup of MySQL so I can import later on another machine.

like image 548
Dakadaka Avatar asked Oct 21 '11 10:10

Dakadaka


People also ask

How do I backup multiple MySQL databases?

To backup multiple MySQL databases with one command you need to use the --database option followed by the list of databases you want to backup. Each database name must be separated by space. The command above will create a dump file containing both databases.

How do I transfer MySQL database from one computer to another?

To copy a MySQL database, you need to follow these steps: First, create a new database using CREATE DATABASE statement. Second, export all the database objects and data of the database from which you want to copy using mysqldump tool. Third, import the SQL dump file into the new database.


1 Answers

At it's most basic, the mysqldump command you can use is:

mysqldump -u$user -p$pass -S $socket --all-databases > db_backup.sql

That will include the mysql database, which will have all the users/privs tables.

There are drawbacks to running this on a production system as it can cause locking. If your tables are small enough, it may not have a significant impact. You will want to test it first.

However, if you are running a pure InnoDB environment, you can use the --single-transaction flag which will create the dump in a single transaction (get it) thus preventing locking on the database. Note, there are corner cases where the initial FLUSH TABLES command run by the dump can lock the tables. If that is the case, kill the dump and restart it. I would also recommend that if you are using this for backup purposes, use the --master-data flag as well to get the binary log coordinates from where the dump was taken. That way, if you need to restore, you can import the dump file and then use the mysqlbinlog command to replay the binary log files from the position where this dump was taken.

like image 141
desertwebdesigns Avatar answered Oct 11 '22 16:10

desertwebdesigns