Several ways to backup MySql database

Several ways to backup MySql database

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 databases in the system

$> mysqldump -u root -h host -p --all-databases > backdb.sql

Directly copy the entire database directory (not applicable for InnoDB storage engine) to back up

windowsns: installpath/mysql/data

linux: /var/lib/mysql

Before copying, you need to execute the following command:

MYSQL> LOCK TABLES;
# Allow clients to continue querying tables during the copy process.
MYSQL> FLUSH TABLES;
# Write the activated index page to disk.

mysqlhotcopy tool backup

The fastest way to back up a database or table can only be run on the machine where the database directory is located, and can only back up MyISAM type tables.

The table files to be backed up must be accessible to use this backup method.

$> mysqlhotcopy -u root -p dbname /path/to/new_directory;
#Copy the database to the new_directory directory.

mysql command import sql file restore

$> mysql -u root -p [dbname] < backup.sql
# You need to create the dbname database before executing. If backup.sql is a backup file created by mysqldump, dbname is not required for execution.
MYSQL> source backup.sql;
# You need to select a database before executing the source command.

Directly copy the database directory to restore

Note: This method must ensure that the major version numbers of the original database and the database to be restored are consistent, and is only applicable to tables with the MyISAM engine.

  1. Shut down the mysql service.
  2. Overwrite the mysql data directory with the backed-up files or directories.
  3. Start the mysql service.
  4. For Linux systems, after copying the file, you need to change the user and group of the file to the user and group running MySQL.

mysqlhotcopy fast recovery

Stop the MySQL service, copy the backup database file to the location where the data is stored (mysql's data folder), and restart the MySQL service (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, the recovery can only be successful after the existing database is deleted using the DROP statement. The database version compatibility also needs to be ensured.

Migrate between databases of the same version

$> mysqldump -h www.abc.com -uroot -p password dbname | 
$>mysqldump -h www.bcd.com -uroot -p password
# Migrate the database dbname on the server www.abc.com to the database of the same version on the server www.bcd.com.

Migration between different versions of MySQL database

Back up the original database.

Uninstall the original database.

Install the new database.

Restore the backed-up database data in the new database.

Database user access information requires backing up the mysql database.

Default character set problem, MySQL4.x uses latin1 as the default character set, and mysql5.x uses utf8 as the default character set. If there is Chinese data, the default character set needs to be changed.

Migration between different databases

MyODBC tool realizes migration between MySQL and SQL Server.

MySQL Migration Toolkit.

Exporting and importing tables

SELECT ...... INTO OUTFILE exports a text file. This method can only export to the database server, and the export file cannot already exist.

MYSQL> SELECT ...... INTO OUTFILE filename [OPTIONS]
MYSQL> SELECT * FROM test.person INTO OUTFILE "C:\person0.txt";
# Import the data in the person table into a text file person0.txt.

mysqldump file exports a text file (unlike INTO OUTFILE, all options of this method do not need to be quoted)

$> mysqldump -T path -u root -p dbname [tables] [OPTIONS]
# The -T parameter indicates exporting a text file. path is the directory where the data is exported.
$> mysqldump -TC:\test person -u root -p
# Export the person table in the test table to a text file. After successful execution, there will be two files in the test directory, person.sql and person.txt

mysql command to export text file

MYSQL> mysql -u root -p --execute="SELECT * FROM person;" test > C:\person3.txt;
# Export the person table data in the test database to the person3.txt text file. The --vartical parameter can be used to split a line into multiple lines.
MYSQL> mysql -u root -p --vartical --execute="SELECT * FROM person;" test > C:\person3.txt;
# --html exports the table as an html file, --xml exports the table as an xml file

LOAD DATA INFILE imports a text file

MYSQL> LOAD DATA INFILE 'filename.txt' INTO TABLE tablename [OPTIONS] [IGNORE number LINES];
# [IGNORE number LINES] means ignore the number of lines MYSQL> LOAD DATA INFILE 'C:\person0.txt' INTO TABLE test.person;

mysqlimport import text file

$> mysqlimport -u root -p dbname filename.txt [OPSTONS]
# The name of the imported table is determined by the file name. The table must exist before importing data. $> mysqlimport -uroot -p test C:\backup\person.txt
# Import data into the person table of the test database.

Recovering Data Using mysqlbinlog

$> mysqlbinlog [option] filename | mysql -u user -p password
# filename is the binary log file.
$> mysqlbinlog --stop-date="2013-03-30 15:27:47" D:\MySQL\log\binlog\binlog.000008 | mysql -u root -p password
# Restore data to operations before 2013-03-30 15:27:47 based on the log file binlog.000008.

Start binary logging

log-bin = path/filename #Log file storage directory and file name expire_log_days = 10 #Log automatic deletion time max_binlog_size = 100M # Maximum size of the log file

Viewing the binary log

MYSQL> SHOW VARIABLES LIKE 'log_%';
MYSQL> SHOW BINARY LOGS;
$>mysqlbinlog filename
# filename is the binary log file name.

Deleting binary logs

MYSQL> RESET MASTER; #Delete all binary logs MYSQL> PURGE {MASTER | BINARY} LOGS TO 'log_name'; #Delete files with file numbers less than log_name MYSQL> PURGE {MASTER | BINARY} LOGS BEFORE 'date'; #Delete files before the specified date

Temporarily stop binary logging (no need to restart MySQL service)

MYSQL> SET sql_log_bin = {0|1} #Pause or start binary log.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Several different ways to import data into MYSQL
  • MySQL 4 methods to import data
  • Summary of six ways to optimize MySQL database
  • Summary of several ways to overwrite and import databases in MySQL

<<:  Methods and steps to build nginx file server based on docker

>>:  jQuery+h5 realizes the nine-square lottery special effect (front-end and back-end code)

Recommend

Dynamic SQL statement analysis in Mybatis

This article mainly introduces the dynamic SQL st...

30 free high-quality English ribbon fonts

30 free high-quality English ribbon fonts for down...

How to use http and WebSocket in CocosCreator

Table of contents 1. HttpGET 2. HTTP POST WebSock...

Analysis of Mysql data migration methods and tools

This article mainly introduces the analysis of My...

Pure CSS3 to achieve mouse over button animation Part 2

After the previous two chapters, do you have a ne...

CentOS 7 builds hadoop 2.10 high availability (HA)

This article introduces how to build a high-avail...

How to express relative paths in Linux

For example, if your current path is /var/log and...

WEB standard web page structure

Whether it is the background image or the text siz...

Solution for adding iptables firewall policy to MySQL service

If your MySQL database is installed on a centos7 ...

CentOS system rpm installation and configuration of Nginx

Table of contents CentOS rpm installation and con...

Design theory: On the issues of scheme, resources and communication

<br />This problem does not exist in many sm...

You may need a large-screen digital scrolling effect like this

The large-screen digital scrolling effect comes f...