mysqlhotcopy是mysql数据库中一个备份工具,相对于mysqldump是一个快速文件意义上的COPY,mysqlhotcopy是一个数据库端的SQL语句集合。
mysqlhotcopy用法
使用mysqlhotcopy之前需要安装perl-DBI和DBD-mysql:
1、执行yum install perl-DBI安装perl-DBI。
2、安装DBD-mysql请参考底部资料
一个常用的备份例子:
/usr/local/mysql/bin/mysqlhotcopy -u root -p Zmh537817 --addtodest 数据库名1 数据库名2 备份目录
–addtodest的意思是当备份存在时,不中断备份,只添加新的文件进去。
更多的选项可以执行/usr/local/mysql/bin/mysqlhotcopy –help查看。
如何还原?
1、停止mysql服务器。
2、复制备份的数据库目录到mysql数据目录下。
3、更改目录所有者为mysql服务器运行的用户(如mysql)。
4、启动mysql服务器
实例
mysqlhotcopy只用于备份MyISAM。它运行在Unix和NetWare中
使用方法见下面的脚本.加入crotab中吧.
代码如下 | 复制代码 |
#!/bin/sh # Name:mysqlbackup.sh # PS:MySQL DataBase Backup,Use mysqlhotcopy script. # Last Modify:2008-06-12 # 定义变量,请根据具体情况修改 # 定义脚本所在目录 scriptsDir=`pwd` # 数据库的数据目录 dataDir=/var/lib/mysql # 数据MySQL备份目录 tmpBackupDir=/tmp/mysqlblackup backupDir=/backup/mysql # 用来备份数据库的用户名和密码 mysqlUser=root mysqlPWD='you password' # 如果临时备份目录存在,清空它,如果不存在则创建它 if [[ -e $tmpBackupDir ]]; then rm -rf $tmpBackupDir/* else mkdir $tmpBackupDir fi # 如果备份目录不存在则创建它 if [[ ! -e $backupDir ]];then mkdir $backupDir fi # 得到数据库备份列表,在此可以过滤不想备份的数据库 for databases in `find $dataDir -type d | sed -e "s//var/lib/mysql///" | sed -e "s/test//"`; do if [[ $databases == "" ]]; then continue else # MySQL备份数据库 /usr/bin/mysqlhotcopy --user=$mysqlUser --password=$mysqlPWD -q "$databases" $tmpBackupDir dateTime=`date "+%Y.%m.%d %H:%M:%S"` echo "$dateTime Database:$databases backup success!" >>MySQLBackup.log fi done # 压缩备份文件 date=`date -I` cd $tmpBackupDir tar czf $backupDir/mysql-$date.tar.gz ./ #End完成 |
加入到crontab中设置每周5运行
代码如下 | 复制代码 |
0 0 * * 5 /backup/blackup.sh |
注意:恢复数据库到备份时的状态
mysqlhotcopy 备份出来的是整个数据库目录,使用时可以直接拷贝到 mysqld 指定的 datadir (在这里是 /var/lib/mysql/)目录下即可,同时要注意权限的问题,如下例:
代码如下 | 复制代码 |
shell> cp -rf db_name /var/lib/mysql/ shell> chown -R mysql:mysql /var/lib/mysql/ (将 db_name 目录的属主改成 mysqld 运行用户) |
本套备份策略只能恢复数据库到最后一次备份时的状态,要想在崩溃时丢失的数据尽量少应该更频繁的进行MySQL备份,要想恢复数据到崩溃时的状态请使用主从复制机制(replication)。
mysqlhotcopy备份报错
错误信息:
代码如下 | 复制代码 |
/usr/local/mysql/bin/mysqlhotcopy -u root -p --addtodest --noindices --flushlog -q qeedoo /tmp/bakinstall_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at (eval 7) line 3. Perhaps the DBD::mysql perl module hasn't been fully installed, or perhaps the capitalisation of 'mysql' isn't right. Available drivers: DBM, ExampleP, File, Proxy, Sponge. |
解决办法:
代码如下 | 复制代码 |
# http://search.cpan.org/~capttofu/DBD-mysql-3.0008/ wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.020.tar.gz tar zxvf DBD-mysql-4.020.tar.gz cd /tmp/DBD-mysql-4.020 perl Makefile.PL --mysql_config=/usr/local/webserver/mysql/bin/mysql_configmakemake install |
最后编译安装完之后 发现还是报错:
代码如下 | 复制代码 |
?12345 install_driver(mysql) failed: Can't load '/usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/auto/DBD/mysql/mysql.so' for module DBD::mysql: libmysqlclient.so.16: cannot open shared object file: No such file or directory at /usr/lib/perl5/5.8.8/i386-linux-thread-multi/DynaLoader.pm line 230. at (eval 7) line 3 Compilation failed in require at (eval 7) line 3. Perhaps a required shared library or dll isn't installed where expected at /usr/bin/mysqlhotcopy line 177 |
是因为tar安装mysql后的路径配置的有问题
vi /etc/ld.so.conf
把mysql的这个库文件路径添加进去
代码如下 | 复制代码 |
echo "/usr/local/webserver/mysql/lib/mysql" >> /etc/ld.so.conf ldconfig |
再执行mysqlhotcopy的时候,一切正常。
有一台服务出现如下问题:
以下问题待遇查看资料,先记下另外这个问题:
代码如下 | 复制代码 |
Can't locate DBI.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /usr/local/mysql/bin/mysqlhotcopy line 8. BEGIN failed--compilation aborted at /usr/local/mysql/bin/mysqlhotcopy line 8. |
原因:系统没有按安装DBI组件

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools
