mysql中mysqlhotcopy备份数据库总结
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组件

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

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.
