Home Database Mysql Tutorial 企业之Mysql备份恢复

企业之Mysql备份恢复

Jun 07, 2016 pm 05:39 PM
mysql backup Database backup and recovery

为什么要备份:备份的几个重要理由:灾难恢复、需求改变、审计、测试等;如测试定期用最新的生产环境中的数据更新到测试环境服务器需要考虑的问题:1、在不导致严

7、对于安全性要考虑周全,如果有人能接触到生产服务器,他是否能访问备份服务器

使用Mysqldump备份工具实现完全备份,并结合二进制日志实现增量备份

   特点:逻辑备份工具、支持InnoDB热备份、MyISAM温备份;备份与恢复较慢

使用LVM逻辑卷快照功能实现几乎热血备份的完全备份,并结合二进制日志实现增量备份;简单介绍"mylvmbackup"一键备份工具的使用

   特点:几乎接近于热备份、物理备份、备份与恢复较快

使用Xtrabackup备份工具实现完全备份与增量备份

   特点:物理备份工具、支持InnoDB热备份、MyISAM温备份、速度较快

一、环境准备

1、安装Mysql数据库服务器

######安装编译环境 [root@localhost ~]# yum -y groupinstall "Server Platform Development" "Development tools" ######安装编译工具 [root@localhost ~]# yum install -y cmake ######创建Mysql用户 [root@localhost ~]# useradd -r mysql ######解压并安装Mysql [root@localhost ~]# tar xf mysql-5.5.33.tar.gz [root@localhost ~]# cd mysql-5.5.33 [root@localhost mysql-5.5.33]# cmake . \ > -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #安装路径 > -DMYSQL_DATADIR=/mydata/data \ #数据存放路径 > -DSYSCONFDIR=/etc \ #主配置文件目录 > -DWITH_INNOBASE_STORAGE_ENGINE=1 \ #下面三项启用支持的存储引擎 > -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ > -DWITH_READLINE=1 \ #支持readline库 > -DWITH_SSL=system \ #支持SSL加密 > -DWITH_ZLIB=system \ #支持ZLIB压缩 > -DWITH_LIBWRAP=0 \ #支持Libwrap库 > -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ #Sock文件路径 > -DDEFAULT_CHARSET=utf8 \ #默认字符集 > -DDEFAULT_COLLATION=utf8_general_ci #默认字符集排序规则 ######编译并安装 [root@localhost mysql-5.5.33]# make && make install 注释: 如果想清理此前的编译所生成的文件,则需要使用如下命令 make clean rm CMakeCache.txt

2、为Mysql提供主配置文件与Sysv服务脚本

######为Mysql提供主配置文件 [root@localhost mysql-5.5.33]# cp support-files/my-large.cnf /etc/my.cnf ######为Mysql提供Sysv服务脚本 [root@localhost mysql-5.5.33]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld ######赋于Sysv脚本执行权限 [root@localhost mysql-5.5.33]# chmod +x /etc/rc.d/init.d/mysqld ######将Mysqld加入系统服务 [root@localhost mysql-5.5.33]# chkconfig --add mysqld [root@localhost mysql-5.5.33]# chkconfig mysqld on

3、让系统识别源码包安装的软件

[root@localhost mysql]# echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile [root@localhost mysql]# . /etc/profile [root@localhost mysql]# ln -s /usr/local/mysql/include /usr/include/mysql [root@localhost ~]# echo "/usr/local/mysql/lib" >> /etc/ld.so.conf [root@localhost ~]# ldconfig

4、修改Mysql主配置文件

[root@localhost mysql]# vim /etc/my.cnf datadir = /mydata/data #添加此行,数据存放目录 innodb_file_per_table = 1 #启用InnoDB表每表一个文件,,默认所有数据库使用一个表空间 log-bin=/binlog/mysql-bin #修改二制作存放目录5、为Mysql创建数据与二投井

5、为Mysql创建数据库与二进制存放目录

######创建数据、二进制存放目录与备份目录 [root@localhost ~]# mkdir -p /backup /mydata/data /binlog ######修改Mysql二进制与数据及备份目录的属主、属组为Mysql用户 [root@localhost mysql]# chown -R mysql.mysql /mydata/data [root@localhost mysql]# chown -R mysql.mysql /binlog /backup ######修改Mysql安装目录的属组为Mysql组 [root@localhost mysql]# chown -R :mysql *

6、初始化Mysql并启动服务

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/data [root@localhost ~]# service mysqld start Starting MySQL......... SUCCESS! ######修改Mysql的root用户密码 [root@localhost ~]# mysqladmin -uroot password 'root@pass'

7、创建测试数据库与表并插入数据

[root@localhost ~]# mysql -uroot -proot@pass mysql> create database allen; mysql> use allen; mysql> create table tab1 (ID tinyint unsigned not null primary key auto_increment,Name char(20) not null unique key,Age tinyint unsigned,Gender char(1) default 'M'); mysql> insert into tab1 (Name,Age,Gender) values ('Zhang WuJi',26,'M'),('Zhao Min',24,'F');

二、使用Mysqldump备份工具备份

1、使用Mysqldump对"allen"数据库做备份并且滚动二进制日志,记录日志位置

[root@localhost ~]# mysqldump -uroot -proot@pass --lock-all-tables --routines --events --triggers --master-data=2 --flush-logs --databases allen > /backup/allen_`date +%F`.sql 注释: -u #指定用户名 -p #指定用户密码 -h #指定主机地址 -A|--all-databases #备份所有数据库 --databases #备份指定数据库 --single-transcation #基于此项可以实现对InnoDB表做热备份,但不需要使用 --lock-all-tables #执行备份时为所有表请求加锁 -E|--events #备份事件调度器代码 --opt #同时启动各种高级选项 -R|--routines #备份存储过程和存储函数 --flush-logs #备份之前刷新日志 --triggers #备份触发器 --master-data=2 #该选项将会记录binlog的日志位置与文件名并追加到文件中,如果为1将会输出CHANGE MASTER命令,主从下有用 ######更多选项请参考man手册
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Multiple backup solutions for MySql: How to efficiently create and restore MySQL backups Multiple backup solutions for MySql: How to efficiently create and restore MySQL backups Jun 15, 2023 pm 03:28 PM

MySql is a commonly used relational database management system that is widely used in various business and application scenarios. For MySQL backup issues, the selection and execution method of the backup plan are crucial. In this article, we will introduce various backup options and how to create and restore MySQL backups efficiently. 1. Selection of backup plan In the process of selecting a MySQL backup plan, you should choose a backup plan that suits you based on the business scenario and actual situation. Cold backup The so-called cold backup is to complete the MySQL database.

How to use MySQL data backup and recovery tools for disaster recovery How to use MySQL data backup and recovery tools for disaster recovery Aug 02, 2023 am 09:06 AM

How to use MySQL data backup and recovery tools to achieve disaster recovery. Data backup and recovery are a very important part of the database management process. Backing up your data protects your database from accidental corruption, hardware failure, or other catastrophic events. As a popular relational database management system, MySQL provides some powerful tools to achieve data backup and recovery. This article will introduce how to use MySQL's data backup and recovery tools to achieve disaster recovery. MySQL data backup tool-mysql

How to effectively manage and maintain ibd files in MySQL database How to effectively manage and maintain ibd files in MySQL database Mar 16, 2024 am 11:21 AM

In the MySQL database, each InnoDB table corresponds to an .ibd file, which stores the table's data and indexes. Therefore, for the management and maintenance of MySQL database, the management of ibd files is also particularly important. This article will introduce how to effectively manage and maintain ibd files in a MySQL database and provide specific code examples. 1. Check and optimize table space First, we can check the disk space usage of the table using the following SQL statement: SELECTTAB

MySQL rolling backup techniques for data MySQL rolling backup techniques for data Jun 15, 2023 pm 07:47 PM

MySQL is a popular relational database that is widely used in various fields. However, like other applications, MySQL has risks such as data corruption, crashes, and malicious attacks. Therefore, backing up your data is crucial. Backups can provide security and some form of "undo" functionality to data, reducing or even eliminating instability and risk. The most common backup types are full backup and incremental backup. However, if you need frequent, real-time backups, rolling backups are a better approach. A rolling backup is when an acceptable

MySql database backup: How to achieve efficient MySQL database backup and recovery MySql database backup: How to achieve efficient MySQL database backup and recovery Jun 15, 2023 pm 11:37 PM

MySQL is one of the most widely used relational database management systems currently. Its efficiency and reliability make it the first choice for many enterprises and developers. But for various reasons, we need to back up the MySQL database. Backing up a MySQL database is not an easy task because once the backup fails, important data may be lost. Therefore, in order to ensure data integrity and recoverability, some measures must be taken to achieve efficient MySQL database backup and recovery. This article will introduce how to achieve

Analysis of project experience on MySQL database backup and recovery performance tuning Analysis of project experience on MySQL database backup and recovery performance tuning Nov 04, 2023 am 09:46 AM

Analysis of MySQL database backup and recovery performance tuning project experience In daily operation and maintenance, MySQL database backup and recovery work is indispensable. However, in the face of multi-terabyte or even petabyte-level data scale, the time and resource consumption required for backup and recovery often become key factors restricting database performance. This article will share some practical experiences and techniques through a practical case of backup and recovery performance tuning for a large Internet enterprise. 1. Selection of backup solutions. Based on different business needs and data scale, the selection of backup solutions should also be considered.

How to backup and restore database after mysql installation How to backup and restore database after mysql installation Apr 08, 2025 am 11:45 AM

There is no absolutely optimal MySQL database backup and recovery solution, and it needs to be selected based on the amount of data, business importance, RTO and RPO. 1. Logical backup (mysqldump) is simple and easy to use, suitable for small databases, but slow and huge files; 2. Physical backup (xtrabackup) is fast, suitable for large databases, but is more complicated to use. The backup strategy needs to consider the backup frequency (RPO decision), backup method (data quantity and time requirement decision) and storage location (off-site storage is more secure), and regularly test the backup and recovery process to avoid backup file corruption, permission problems, insufficient storage space, network interruption and untested issues, and ensure data security.

A comprehensive guide to MySQL backup and recovery A comprehensive guide to MySQL backup and recovery Jun 15, 2023 am 09:48 AM

MySQL is currently one of the most popular relational database management systems and is widely used in enterprise-level applications. Whether you are a developer or a data administrator, you need to understand the basic knowledge of MySQL backup and recovery. Backup and recovery not only help enterprises protect data, but also enable systems to respond quickly to adverse situations and restore them to normal operating conditions as much as possible. This article will detail the steps for MySQL backup and recovery and provide some best practices to help readers go further in protecting their MySQL databases.

See all articles