备份MySQL数据库

发布于 2024-08-02 02:52:17 字数 306 浏览 4 评论 0原文

我有一个大约 1.7GB 的 MySQL 数据库。 我通常使用 mysqldump 进行备份,这大约需要 2 分钟。 但是,我想知道以下问题的答案:

  1. mysqldump 是否会阻止对数据库的读和/或写操作? 因为在实际场景中,我不想在备份数据库时阻止用户使用数据库。

  2. 对我来说,理想的情况是一周只备份一次整个数据库,但在中间的日子里只需要备份一个表,因为其他表不会改变。 有没有办法实现这一点?

  3. 对于这些目的,mysqlhotcopy 是更好的选择吗?

I have a MySQL Database of about 1.7GB. I usually back it up using mysqldump and this takes about 2 minutes. However, I would like to know the answers to the following questions:

  1. Does mysqldump block read and/or write operations to the database? Because in a live scenario, I would not want to block users from using the database while it is being backed up.

  2. It would be ideal for me to only backup the WHOLE database once in, say, a week, but in the intermediate days only one table needs to be backed up as the others won't change. Is there a way to achieve this?

  3. Is mysqlhotcopy a better alternative for these purposes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

仅一夜美梦 2024-08-09 02:52:17

mysqlhotcopy 在读锁丢失的某些情况下不起作用,
并且不适用于 INNODB 表。

mysqldump用得比较多,因为它可以备份各种表。

来自 MySQL 文档

mysqlhotcopy 是一个 Perl 脚本,最初由 Tim Bunce 编写和贡献。 它使用 LOCK TABLES、FLUSH TABLES 和 cp 或 scp 来快速进行数据库备份。 这是备份数据库或单个表的最快方法,但它只能在数据库目录所在的同一台计算机上运行。 mysqlhotcopy 仅适用于备份 MyISAM 和 ARCHIVE 表。 它运行在 Unix 和 NetWare 上

mysqldump 客户端是一个备份程序,最初由 Igor Romanenko 编写。 它可用于转储数据库或数据库集合以进行备份或传输到另一个 SQL 服务器(不一定是 MySQL 服务器)。 转储通常包含用于创建表、填充表或两者的 SQL 语句。 但是,mysqldump 也可用于生成 CSV、其他分隔文本或 XML 格式的文件。

再见。

mysqlhotcopy does not work in certain cases where the readlock is lost,
and does not work with INNODB tables.

mysqldump is more used because it can back up all kinds of tables.

From MySQL documentation

mysqlhotcopy is a Perl script that was originally written and contributed by Tim Bunce. It uses LOCK TABLES, FLUSH TABLES, and cp or scp to make a database backup quickly. It is the fastest way to make a backup of the database or single tables, but it can be run only on the same machine where the database directories are located. mysqlhotcopy works only for backing up MyISAM and ARCHIVE tables. It runs on Unix and NetWare

The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format.

Bye.

街角迷惘 2024-08-09 02:52:17

1) mysqldump 仅在您要求时阻塞(--lock-tables、--lock-all-tables、--single-transaction 之一)。 但如果您希望备份保持一致,则 mysqldump 应该 阻止(使用 --single-transaction 或 --lock-all-tables),否则您可能会得到不一致的数据库快照。 注意:--single-transaction 仅适用于 InnoDB。

2)当然,只需在数据库名称后枚举您想要备份的表:

mysqldump OPTIONS DATABASE TABLE1 TABLE2 ...

或者您可以排除您不想要的表:

mysqldump ... --ignore-table=TABLE1 --ignore-table=TABLE2 .. DATABASE

因此您可以每周执行一次整个数据库转储,并且每周仅备份一次更改的表天。

3)mysqlhotcopy仅适用于MyISAM表,在大多数应用程序中,您最好使用InnoDB。 有商业工具(相当昂贵)用于 innodb 表的热备份。 最近还有用于此目的的新开源工具 - Xtrabackup

另外,要自动化该过程,您可以使用 astrails-safe。 它支持使用 mysqldump 进行数据库备份,使用 tar 进行文件系统备份。 +加密+上传到S3,+许多其他好东西。 目前还没有 xtrabackup 支持,但如果您需要的话,应该很容易添加。

1) mysqldump only blocks when you ask it to (one of the --lock-tables, --lock-all-tables, --single-transaction). but if you want your backup to be consistent then mysqldump should block (using --single-transaction or --lock-all-tables) or you might get an inconsistent database snapshot. Note: --single-transaction works only for InnoDB.

2) sure, just enumerate the tables you want to be backed up after the database name:

mysqldump OPTIONS DATABASE TABLE1 TABLE2 ...

Alternatively you can exclude the tables you don't want:

mysqldump ... --ignore-table=TABLE1 --ignore-table=TABLE2 .. DATABASE

So you can do a whole database dump once a week and backup only the changing tables once a day.

3) mysqlhotcopy inly works on MyISAM tables and in most applications you are better off with InnoDB. There are commercial tools (quite expensive) for hotbackup of innodb tables. Lately there is also the new opensource one for this purpose - Xtrabackup

Also, to automate the process you can use astrails-safe. It supports database backup with mysqldump and filesystem with tar. +encryption +upload to S3, +many other goodies. There is no xtrabackup support yet, but it should be easy to add if this is what you need.

聊慰 2024-08-09 02:52:17

在您的设置中添加 mysql 从站将允许您在不锁定生产数据库的情况下进行一致的备份。

添加从站还可以为您提供更改的二进制日志。 转储是您进行转储时数据库的快照。 二进制日志包含修改数据的所有语句以及时间戳。

如果您在中午发生故障并且每天只进行一次备份,那么您就损失了半天的工作量。 使用二进制日志和 mysqldump,您可以从前一天恢复并将日志“播放”到故障点。

http://dev.mysql.com/doc/refman/ 5.0/en/binary-log.html

如果您在具有 LVM 磁盘的 Linux 服务器或具有 VSS 的 Windows 服务器上运行 MySQL,您应该查看 Zamanda
它需要磁盘上数据的二进制差异,这比数据库的文本转储的读取和恢复速度要快得多。

Adding a mysql slave to your setup would allow you to take consistant backups without locking the production database.

Adding a slave also gives you a binary log of changes. A dump is a snapshot of the database at the time you took the dump. The binary log contains all statements that modified the data along with a timestamp.

If you have a failure in the middle of the day and your only taking backups once a day, you've lost a half a days worth of work. With binary logs and mysqldump, you could restore from the previous day and 'play' the logs forward to the point of failure.

http://dev.mysql.com/doc/refman/5.0/en/binary-log.html

If your running MySQL on a linux server with LVM disks or a windows server with VSS, you should check out Zamanda.
It takes binary diffs of the data on disk, which is much faster to read and restore than a text dump of the database.

治碍 2024-08-09 02:52:17
  1. 不,您可以使用 --lock-tables 指定要锁定的表,但默认情况下它们不是锁定的
  2. 如果您不指定任何表,则备份整个数据库,或者您可以指定表列表:

    mysqldump [options] db_name [tables]

  3. 抱歉,没有使用它,但是我运行了许多 MySQL 数据库,有些更大,有些小于 1.7gb,我使用 mysqldump 进行所有备份。

  1. No, you can specify tables to be locked using --lock-tables but they aren't by default
  2. If you don't specify any tables then the whole DB is backed up, or you can specify a list of tables :

    mysqldump [options] db_name [tables]

  3. Not used it sorry, however I run a number of MySQL DBs, some bigger some smaller than 1.7gb and I use mysqldump for all my backups.

溺渁∝ 2024-08-09 02:52:17

对于 mysql 和 PHP 试试这个
这也会在 n 天后删除文件

$dbhost = 'localhost';
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$dbname = 'database1';  
$folder = 'backups/';  // Name of folder you want to place the file
$filename = $dbname . date("Y-m-d-H-i-s") . ".sql";
$remove_days = 7; // Number of days that the file will stay on the server



$command="mysqldump --host=$dbhost --user=$dbuser --password=$dbpass $dbname > $folder$filename";
system($command);

$files = (glob("$folder"."*.sql"));

    foreach($files as $file) {
        if(is_file($file)
        && time() - filemtime($file) >= $remove_days*24*60*60) { // 2 days =  2*24*60*60
            unlink($file);
            echo "$file removed \n";
        } else { echo "$file was last modified: " . date ("F d Y H:i:s.", filemtime($file)) . "\n"; }
    }

For mysql and PHP try this
This will also remove files after n days

$dbhost = 'localhost';
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$dbname = 'database1';  
$folder = 'backups/';  // Name of folder you want to place the file
$filename = $dbname . date("Y-m-d-H-i-s") . ".sql";
$remove_days = 7; // Number of days that the file will stay on the server



$command="mysqldump --host=$dbhost --user=$dbuser --password=$dbpass $dbname > $folder$filename";
system($command);

$files = (glob("$folder"."*.sql"));

    foreach($files as $file) {
        if(is_file($file)
        && time() - filemtime($file) >= $remove_days*24*60*60) { // 2 days =  2*24*60*60
            unlink($file);
            echo "$file removed \n";
        } else { echo "$file was last modified: " . date ("F d Y H:i:s.", filemtime($file)) . "\n"; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文