How to back up the database regularly in Linux
This article mainly introduces how to back up the database regularly in Linux. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
Backup database regularly under Linux
The method of using crontab to back up MYSQL Database regularly under linux
Just follow these three steps and everything is under control:
Step 1: configure the backup directory code on the server:
Mkdir / var/lib/mysqlbackup cd / var/lib/mysqlbackup
Step 2: write the backup script code:
Vi dbbackup.sh pastes the following code, be sure to change the username,password and dbname in it. #! / bin/sh mysqldump-uuser-ppassword dbname | gzip > / var/lib/mysqlbackup/dbnamedate +% Y-%m-%d_%H%M%S.sql.gz cd / var/lib/mysqlbackup rm-rf find. -name'* .sql.gz'- mtime 10 # Delete backup files from 10 days ago
Step 3: change the permissions of the backup script
Chmod + x dbbackup.sh
Step 4: execute the backup script code regularly with crontab:
Crontab-e
If you back up at 21: 00 every night, add the following code
00 21 * / var/lib/mysqlbackup/dbbackup.sh
Problems you will encounter:
1.mysqldump is a backup database command, do not understand the direct Baidu is good.
2.Crontab is a command for scheduled tasks. If you don't understand it, you can visit http://www.thinkphp.cn/code/1....
3. Backup data must first make sure that a password has been set for root, otherwise it will be reported to mysqldump execution time Got error: 1045: Access denied for user 'root'@'localhost' (using password: YES) when trying to connect
Solution:
Log in to the mysql client
Mysql-hserverip-uroot-p mysql > use mysql; Database changed mysql > update user set password=password ('new password') where user='root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql > flush privileges Query OK, 0 rows affected (0.00 sec) / / this command gives users new permissions or passwords to read directly into memory without the need to restart the database to prevent errors mysql > quit is all the contents of the article "how to back up the database regularly in Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!