mysql database - Backup and restore
Backup
mysqldump Command backup data
stay MySQL Using the command line... Is provided in , A convenient tool for exporting database data to files mysqldump , We can directly export the database content through the command line .
First of all, let's have a brief understanding of mysqldump Command usage :
# MySQLdump Commonly used
/usr/local/mysql/bin/mysqldump -u xx -p databases > /xxx/xxxdb.sql
- mysqldump : mysql After installation bin The executable file in the directory
- -u :mysql user name
- -p :mysql password
- databases : Database name
- ">" : Direction of backup output
- /xxx/xxxdb.sql : Indicates which file to back up to
Be careful : Backing up multiple databases , Need to use "--databases" Parameters , Later, according to the database name , Use spaces to separate .
mysqldump -uroot -proot --databases db1 db2 > /xxx/xxxdb.sql
mysqldump Common operation examples
1. Back up the data and structure of all databases
mysqldump -uroot -p123456 -A > /data/mysqlDump/mydb.sql
2. Back up the structure of all databases ( Add -d Parameters )
mysqldump -uroot -p123456 -A -d > /data/mysqlDump/mydb.sql
3. Back up all database data ( Add -t Parameters )
mysqldump -uroot -p123456 -A -t > /data/mysqlDump/mydb.sql
4. Back up a single database " take -A Replace The corresponding database name ". Be careful : -d -t The effect is the same .
mysqldump -uroot-p123456 mydb > /data/mysqlDump/mydb.sql
5. Backing up multiple databases
mysqldump -uroot -p123456 --databases db1 db2 > /data/mysqlDump/mydb.sql
Restore
Restore MySQL Backup content
There are two ways to restore :
First use mysql Service command restore :
- On the system command line , Enter the following to restore :
mysql -uroot -proot databases < /xxx/xxxdb.sql
mysql : The command means mysql The service procedure of
Be careful :-u -p databases Same meaning as backup .
Second use sql Command restore :
- Log in mysql In the library , adopt source Command to find the file in the corresponding system to restore :
mysql> source /xxx/xxxdb.sql
source : Database restore command
/xxx/xxxdb.sql : Represents the restored backup file
The third kind of full database restore :
mysql -uroot -proot< /xxx/xxxdb.sql
Be careful :xxxdb.sql The file must be the backup file of all databases
To write mysql Database backup script
1. Create script file
stay Linux in , Use vi perhaps vim Write the script and name it :mysql_dump_script.sh
example : This document is prepared according to my own needs mysql Script .( Please modify it by yourself according to the situation )
#!/bin/bash
# Save the number of backup , Backup 31 Day data
number=31
# Backup save path
backup_dir=/back
# date
dd=`date +%Y-%m-%d-%H-%M-%S`
# Backup tools
tool=/usr/local/mysql/bin/mysqldump
# user name
username=root
# password
password=root
# The database to be backed up
database_name=jn-dev
# If the folder does not exist, create
if [ ! -d $backup_dir ];
then
mkdir -p $backup_dir;
fi
# Database backup command - Simple writing mysqldump -u root -p123456 users > /root/mysqlbackup/users-$filename.sql
$tool -u $username -p$password $database_name > $backup_dir/$database_name-$dd.sql
# Write create backup log
echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt
# Find the backup that needs to be deleted
delfile=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | head -1`
# Determine whether the current number of backups is greater than $number
count=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | wc -l`
if [ $count -gt $number ]
then
# Delete the earliest generated backup , Only keep number Number of backups
rm $delfile
# Write delete file log
echo "delete $delfile" >> $backup_dir/log.txt
fi
The main meaning of the above code is as follows :
1. First, set the parameters , for example number The maximum number of backups required , Backup path , user name , Password etc. .
2. perform mysqldump Command save backup file , And print the operation to log.txt Mark the operation log in .
3. Define files to delete : adopt ls Command to get the ninth column , That is, the list of documents , Then define the file to be deleted with the latest operation time through the implementation .
4. Define the number of backups : adopt ls Command plus wc -l Statistics to sql The number of lines in the ending file .
5. If the file exceeds the limit size , Delete the first sql file
2. Set the execution permission of the script
Execute in the directory where the script file exists
chmod u+x *.sh
chmod Detailed explanation
chmod: file / Directory permission setting command
Use a text setting method that contains letters and operator expressions
Its grammatical form is :chmod [who] [opt] [mode] file / Directory name
chmod u+x *.sh
who Representative object , Is one or a combination of the following letters :
u:User, The owner of a file or directory .
g:Group, That is, the group of files or directories .
o:Other, Except for the owner or group of files or directories , Other users are in this range .
a:All, All users , Include the owner , Group and other users .opt It represents operation , It can be for :
+: Add a permission
-: Cancel a permission
=: Give given permission , And cancel the original permissionmode It means authority :
r: Can be read
w: Can write
x: ExecutableSymbol :
"*": wildcard
Be careful : To ensure the correctness of the script file, you can test .( The following directory is written in the same level directory of the file )
sh mysql_dump_script.sh
Use crontab Execute backup scripts on a regular basis
Price
- stay Linux in , Periodic tasks are usually performed by cron This daemons will handle
[ps -ef|grep cron] - cron Read one or more configuration files , These configuration files contain the command line and its call time
- cron The configuration file for is called “crontab”, yes “cron table” Abbreviation
- cron The service is started by default
crontab Use
crontab [-u username] // Omitting the user table indicates the operation of the current user crontab
-e ( Edit the sheet )
-l ( List the orders in the worksheet )
-r ( Delete the work )
cron service
- CentOS6 Upper cron command
service crond start // Start the service
service crond stop // Close the service
service crond restart // Restart the service
service crond reload // service crond reload
service crond status // Check the status
- CentOS7 Upper cron command
systemctl start crond.service // Start the service
systemctl stop crond.service // Close the service
systemctl restart crond.service // Restart the service
systemctl reload crond.service // service crond reload
systemctl status crond.service // Check the status
// perhaps
crond start
crond stop
crond restart
crond reload
crond status
lock situation
error :crond: can’t lock /var/run/crond.pid
rm -rf /var/run/crond.pid
crontab Use
crontab The order of is : Time + action
Time : From front to back is branch 、 when 、 Japan 、 month 、 Zhou Five kinds , Operators have :
"*" All numbers in the range
"/" How many numbers are passed
"-" from X To Z
"," Hash numbers
action : Specify task script .
/back/mysql_dump_script.sh
Be careful :
Use /etc/crontab When you file You need to add the user before the action .
Time user / action >> Log address
Create a scheduled task
Use crontab There are two ways to create a scheduled task :
The first one is :crontab -e ( It is the periodic planning task of a user )
1. Carry out orders
crontab -e
2. After execution, a file will be opened , Add a scheduled task to the file .
*/5 * * * * /back/mysql_dump_script.sh >> /back/mysql_dump_script.log
<--mysql_dump_script.log Log file for scheduled task execution -->
<-- Every five minutes -->
The second kind :vi /etc/crontab (/etc/crontab It is the periodic task of the system )
1. Carry out orders
vi /etc/crontab
2. After execution, a file will be opened , Add a scheduled task to the file ( Be careful root Is the user ).
*/5 * * * * root/back/mysql_dump_script.sh >> /back/mysql_dump_script.log
<--mysql_dump_script.log Log file for scheduled task execution -->
<-- Every five minutes -->
/etc/crontab Document and crontab -e Command difference
- The format is different :crontab -e And /etc/crontab The modified syntax format is different ,/etc/crontab More than a user Appoint
- crontab -e It's for users cron To design the , Any user can edit the task . It should be noted that :crontab -e It's actually /usr/bin/crontab This executive file .
- /etc/crontab For the system cron To design the . If it is a routine task of the system , Edit to /etc/crontab Just file it , however /etc/crontab It's a plain text file , Only use root Edit this file as .
Be careful : To modify and edit the files of the above two methods, you need to restart cron service
Cancel scheduled tasks
crontab -l Indicates that all scheduled tasks are listed
crontab -lCancel scheduled tasks
cancel all crontab -r
crontab -rCancel a certain crontab -e The configuration file , Delete which row configuration to cancel
crontab -e
mysql Common mistakes
mysql Service not started
mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect
When trying to connect , Unable to access the socket ‘/tmp/mysql.sock’(2) Connect to local MySQL The server
Password risk prompt
mysql: [Warning] Using a password on the command line interface can be insecure.
Using passwords on the command line interface may not be secure .









