当前位置:网站首页>[Tencent cloud] how can the MySQL database on the cloud fully back up the data base script?
[Tencent cloud] how can the MySQL database on the cloud fully back up the data base script?
2022-06-24 02:55:00 【TCS-F】
《MySQL The way of management 》 A Book Learned that for MySQL Database management , how Full volume backup MySQL database :
Script code :
#!/bin/bash
mkdir /backup
cd /backup
dateDIR='date+"%y-%m-%d"'
mkdir -p $dateDIR/data
path=/usr/local/mysql/data
for i in 'mysql -u root -p123456 -e "show database"|grep -v "Database"'
do
mysqldump -uroot -p123456 --default-chareacter-set=utf-8 \-q --lock-all-tables --flush-logs -E -R --triggers -B $i | gzip > /backup/$dateDIR/${i}_${dateDIR}.sql.gz
done
bilog_rm='tail -n 1 $path/mysql-bin.index | sed 's/.\///''
msyql -uroot -p123456 -e "purge binary logs to '$binlog_rm'"》》》》 Refer to the internet article , Learn more MySQL Database backup
mysqldump Tool backup
Back up the entire database
$> mysqldump -u root -h host -p dbname > backdb.sql
Back up a table in the database
$> mysqldump -u root -h host -p dbname tbname1, tbname2 > backdb.sql
Backing up multiple databases
$> mysqldump -u root -h host -p --databases dbname1, dbname2 > backdb.sql
Back up all the databases in the system
$> mysqldump -u root -h host -p --all-databases > backdb.sql
Copy the entire database directory directly ( about InnoDB Storage engine not applicable ) Backup
windowns: installpath/mysql/data
linux: /var/lib/mysql
You need to execute the following commands before copying :
MYSQL> LOCK TABLES; # Allow customers to continue querying tables during replication , MYSQL> FLUSH TABLES; # Write the active index page to the hard disk .
mysqlhotcopy Tool backup
The fastest way to back up a database or table , It can only run on the machine where the database directory is located , And can only back up MyISAM Type of watch .
To use this backup method, you must have access to the backed up table file .
$> mysqlhotcopy -u root -p dbname /path/to/new_directory; # Copy the database to new_directory Catalog .
mysql Command import sql File restore
$> mysql -u root -p [dbname] < backup.sql # You need to create a dbname database , If backup.sql yes mysqldump The backup file created does not need to be executed dbname. MYSQL> source backup.sql; # perform source You need to select the database before the command .
Direct replication database directory restore
notes : This method must ensure that the original database and the database to be restored The main version number of the database is consistent , And only for MyISAM engine Table of .
- close mysql service .
- Overwrite the backed up files or directories mysql Of data Catalog .
- start-up mysql service .
- about linux System , After copying the file, you need to change the users and groups of the file Change it to mysql Users and groups running .
mysqlhotcopy Fast recovery
stop it mysql service , The database files will be backed up Copy to the location where the data is stored (mysql Of data Folder ), Restart first mysql service that will do ( You may need to specify the owner of the database file ).
$> cp -R /usr/backup/test /usr/local/mysql/data # If the restored database already exists , Then use DROP Statement after deleting an existing database , Recovery can be successful , You also need to ensure that the database version is compatible .
Migration between databases of the same version
$> mysqldump -h www.abc.com -uroot -p password dbname | $> mysqldump -h www.bcd.com -uroot -p password # Put the server www.abc.com The database of dbname Migrate to the server www.bcd.com On the same version of the database .
Different versions mysql Migration between databases
Back up the original database .
Uninstall the original database .
Install new database .
In the new database Restore backup Database data .
Database user access information needs to be backed up mysql database .
Default character set problem ,MySQL4.x Use in latin1 As the default character Set ,mysql5.x Use utf8 As default character set . If you have Chinese data, you need to change the default character set .
Migration between different databases
MyODBC Tools Realization MySQL and SQL Server Migration between .
MySQL Migration Toolkit Tools .
Table export and import
SELECT ...... INTO OUTFILE Export text file , The method Can only be exported to the database server On , And the export file cannot already exist .
MYSQL> SELECT ...... INTO OUTFILE filename [OPTIONS] MYSQL> SELECT * FROM test.person INTO OUTFILE "C:\person0.txt"; # Will table person Import the data in as a text file person0.txt.
mysqldump File export text file ( and INTO OUTFILE The difference is that there is no need to add quotation marks to all the options of this method )
$> mysqldump -T path -u root -p dbname [tables] [OPTIONS] # -T The parameter indicates that the exported text file .path Directory for exporting data . $> mysqldump -T C:\test person -u root -p # take test In the table person Export table to text file . After successful execution test There will be two files in the directory ,person.sql and person.txt
mysql Command to export a text file
MYSQL> mysql -u root -p --execute="SELECT * FROM person;" test > C:\person3.txt; # take test In the database person Export table data to person3.txt In the text file .--vartical Parameter can divide a row into multiple rows for display . MYSQL> mysql -u root -p --vartical --execute="SELECT * FROM person;" test > C:\person3.txt; # --html Export table as html file ,--xml File exports the table as xml file
LOAD DATA INFILE Import a text file
MYSQL> LOAD DATA INFILE 'filename.txt' INTO TABLE tablename [OPTIONS] [IGNORE number LINES]; # [IGNORE number LINES] Indicates the number of rows ignored MYSQL> LOAD DATA INFILE 'C:\person0.txt' INTO TABLE test.person;
mysqlimport Import a text file
$> mysqlimport -u root -p dbname filename.txt [OPSTONS] # The imported table name is determined by the file name , The table must exist before importing data $> mysqlimport -uroot -p test C:\backup\person.txt # Import data into test Database person In the table .
Use mysqlbinlog Restore data
$> mysqlbinlog [option] filename | mysql -u user -p password # filename Is a binary log file , $> mysqlbinlog --stop-date="2013-03-30 15:27:47" D:\MySQL\log\binlog\binlog.000008 | mysql -u root -p password # According to log file binlog.000008 Recover data to 2013-03-30 15:27:47 Previous operations .
Start binary log
log-bin = path/filename # Log file storage directory and file name expire_log_days = 10 # Log auto delete time max_binlog_size = 100M # Maximum log file size
View binary logs
MYSQL> SHOW VARIABLES LIKE 'log_%'; MYSQL> SHOW BINARY LOGS; $> mysqlbinlog filename # filename Is the binary log file name .
Delete binary log
MYSQL> RESET MASTER; # Delete all binary logs
MYSQL> PURGE {MASTER | BINARY} LOGS TO 'log_name'; # Delete file number less than log_name Numbered documents
MYSQL> PURGE {MASTER | BINARY} LOGS BEFORE 'date'; # Delete files before the specified date Temporarily stop binary logging ( No need to reboot mysql service )
MYSQL> SET sql_log_bin = {0|1} # Pause or start binary logging .Recommended reading :
- Based on Tencent cloud CVM Self built high availability Redis practice https://cloud.tencent.com/act/cps/redirect?redirect=11
- Based on Tencent cloud CVM build Hadoop Cluster and do data migration https://cloud.tencent.com/act/cps/redirect?redirect=12&
- Tencent cloud CVM Tag practice sharing https://cloud.tencent.com/act/cps/redirect?redirect=14&
- Host migration practice sharing https://cloud.tencent.com/act/cps/redirect?redirect=15&
- Local IDC Computer room database disaster recovery solution https://cloud.tencent.com/act/cps/redirect?redirect=16&
- Solutions for inter account cloud resource intranet interworking based on cloud networking https://cloud.tencent.com/act/cps/redirect?redirect=34664&
- Lightweight application server best practices :https://cloud.tencent.com/act/pro/lighthouse_new
- Tencent cloud “ A double tenth ” Event offer list :https://cloud.tencent.com/act/cps/redirect?redirect=34875
边栏推荐
- How to recover the garbled words in the software?
- Is AI face detection and face recognition a concept? What's the difference?
- How does easydss handle the problem that the sharing page cannot be opened due to cache problems?
- The cost of on-site development of software talent outsourcing is higher than that of software project outsourcing. Why
- What are the performance characteristics of cloud desktop? How to choose the most cost-effective cloud desktop server?
- I have a server. What can I do?
- How does easynvr call the interface to modify a user-defined page?
- Grc: GRC interface is mixed with restful API
- MySQL case deep excavation information_ Root causes of slow schema view query (Part 2)
- How to view the speech synthesis platform how to use the speech synthesis platform
猜你喜欢
![[51nod] 2653 section XOR](/img/2d/cb4bf4e14939ce432cac6d35b6a41b.jpg)
[51nod] 2653 section XOR

The cost of on-site development of software talent outsourcing is higher than that of software project outsourcing. Why
Cloudpods golang practice

2022-2028 global aircraft wireless intercom system industry research and trend analysis report

IOS development - multithreading - thread safety (3)

2022-2028 global aircraft front wheel steering system industry research and trend analysis report
![[51nod] 3047 displacement operation](/img/cb/9380337adbc09c54a5b984cab7d3b8.jpg)
[51nod] 3047 displacement operation

2022-2028 global third-party data platform industry research and trend analysis report

2022-2028 global portable two-way radio equipment industry research and trend analysis report

Permission maintenance topic: domain controller permission maintenance
随机推荐
What is data matrix code
Uiscrollview add gestures show and hide keyboard
Gin framework: add Prometheus monitoring
How to transfer files from the server connected to the fortress machine and how to access the server through the fortress machine
[Tencent cloud double 12.12] from 56 yuan! New users of Tencent cloud buy for the first time, which is more cost-effective!
Initial experience of creating partitioned tables under MySQL cases tdsql for MySQL (I)
Start tcapulusdb process
Industry ranks first in blackmail attacks, hacker organizations attack Afghanistan and India | global network security hotspot
Heavy release! Tencent security hosting service TA is here!
October 27, 2021: curriculum. You must take numcourses this semester
UI automation based on Selenium
File access methods on Fortress server how to log in to the server
Where is the domain name filed? What materials are required for domain name filing?
Tke single node risk avoidance
What about foreign trade companies? Is this another difficult year?
How does easydss handle the problem that the sharing page cannot be opened due to cache problems?
How does [lightweight application server] build a cross-border e-commerce management environment?
Wkwebview audio and video media playback processing
A variety of fixed assets inventory methods to deal with the year-end large inventory of fixed assets
Is your posture correct—— A detailed discussion on horizontal sub database and sub table