How to backup MySQL in Linux?

There are two ways of backing up your MySQL files in Linux;

1. Copying the data directory:

    • First stop the mysqld server with service mysqld stop.
    • Then copy the directories with cp -Rp /var/lib/MySQL/*.* /backup.
    • This will copy all of the MySQL server data to the /backup directory.
    • Start the mysqld server back up with service mysqld start

2. Use the mysqldump utility to dump individual or all databases to sql files:

    • For individual databases run mysqldump u username p database > database_backup.sql
    • For all databases on the server run mysqldump u root p all-databases > all_databases.sql
    • For all databases separated into individual files run for I in echo show databases; | mysql -u root — password ="passwordhere" | grep - v Database`; do mysqldump - u root — password =" passwordhere" $I > "$I.sql"; done