MySQL自动备份及灾难恢复
当数据库服务器建立好以后,我们首先要做的不是考虑要在这个支持数据库的服务器运行哪些受MySQL提携的程序,而是当数据库遭到破坏
当数据库服务器建立好以后,我们首先要做的不是考虑要在这个支持数据库的服务器运行哪些受MySQL提携的程序,而是当数据库遭到破坏后,怎样安然恢复到最后一次正常的状态,使得数据的损失达到最小。
或者说,仅仅是数据库服务器的建立,只能说明它能做些什么,并不代表它能稳定的做些什么。灾难恢复的效率及全面性,也是系统的稳定性的一个准因素,尤其对于一个服务器系统。
这一节,介绍数据库自动备份以及数据库被破坏后的恢复的方法。在这里,我们使用mysqlhotcopy,并且定义一段Shell脚本来实现数据库的自动备份,并且,让整个数据自动备份与数据恢复过程都基于Shell。
建立数据库备份所需条件
[1] 建立自动备份脚本
在这里,为了使数据库备份和恢复的符合我们的实际要求,用一段符合要求的Shell脚本来实现整个备份过程的自动化。
[root@sample ~]# vi mysql-backup.sh ← 建立数据库自动备份脚本,,如下:
#声明SHELL 和执行路径
#!/bin/bash
PATH=/usr/local/sbin:/usr/bin:/bin
# 设定备份的目录
BACKDIR=/backup/mysql
# 设置ROOT的密码
ROOTPASS=******** ← 将星号替换成MySQL的root密码
# 重做备份目录
rm -rf $BACKDIR
mkdir -p $BACKDIR
# 获得数据库名字
DBLIST=`ls -p /var/lib/mysql | grep / | tr -d /` #tr - translate or delete characters[ -d, --delete]
# 备份数据库
for dbname in $DBLIST
do
mysqlhotcopy $dbname -u root -p $ROOTPASS $BACKDIR | logger -t mysqlhotcopy
done
[2] 运行数据库自动备份脚本
[root@sample ~]# chmod 700 mysql-backup.sh ← 改变脚本属性,让其只能让root用户执行
[root@sample ~]# ./mysql-backup.sh ← 运行脚本
[root@sample ~]# ls -l /backup/mysql/ ← 确认一下是否备份成功
total 8
drwxr-x--- 2 mysql mysql 4096 Sep 1 16:54 mysql ← 已成功备份到/backup/mysql目录中
[3] 让数据库备份脚本每天自动运行
[root@sample ~]# crontab -e ← 编辑自动运行规则(然后会出现编辑窗口,操作同vi)
00 03 * * * /root/mysql-backup.sh ← 添加这一行到文件中,让数据库备份每天凌晨3点进行
测试自动备份正常运转与否(备份恢复的方法)这里,以通过实际操作的过程来介绍问题出现后的恢复方法。
[1] 当数据库被删除后的恢复方法
首先建立一个测试用的数据库。
[root@sample ~]# mysql -u root -p ← 用root登录到MySQL服务器
Enter password: ← 输入MySQL的root用户密码
#进入下面的BANNER
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
#BANNER结束
mysql> create database test; ← 建立一个测试用的数据库test
Query OK, 1 row affected (0.00 sec)
mysql> use test ← 连接到这个数据库
Database changed
mysql> create table test(num int, name varchar(50)); ← 在数据库中建立一个表
Query OK, 0 rows affected (0.07 sec)
mysql> insert into test values(1,'Hello,BSD'); ← 插入一个值到这个表(这里以“Hello,BSD”为例)
Query OK, 1 row affected (0.02 sec)
mysql> select * from test; ← 查看数据库中的内容
+------+-------------+
| num | name |
+------+-------------+
|1 | Hello,BSD | ← 确认刚刚插入到表中的值的存在
+------+-------------+
1 row in set (0.01 sec)
mysql> exit ← 退出MySQL服务器
Bye

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
