设为首页 收藏本站
查看: 585|回复: 0

[经验分享] mysql备份工具之mysqlhotcopy

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-1-6 08:21:35 | 显示全部楼层 |阅读模式
mysqlhotcopy使用lock tables、flush tables和cp或scp来快速备份数据库.它是备份数据库或单个表最快的途径,完全属于物理备份,但只能用于备份MyISAM存储引擎和运行在数据库目录所在的机器上.与mysqldump备份不同,mysqldump属于逻辑备份,备份时是执行的sql语句.使用mysqlhotcopy命令前需要要安装相应的软件依赖包.
1.安装mysqlhotcopy所依赖的软件包(perl-DBD,DBD-mysql)
[iyunv@tong2 ~]# yum install perl-DBD* -y
[iyunv@tong2 ~]# wget https://cpan.metacpan.org/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.029.tar.gz
[iyunv@tong2 ~]# tar xvf DBD-mysql-4.029.tar.gz
[iyunv@tong2 ~]# cd DBD-mysql-4.029
[iyunv@tong2 DBD-mysql-4.029]# perl Makefile.PL
[iyunv@tong2 DBD-mysql-4.029]# make
[iyunv@tong2 DBD-mysql-4.029]# make install
[iyunv@tong2 DBD-mysql-4.029]# echo $?
0
[iyunv@tong2 DBD-mysql-4.029]# cd
[iyunv@tong2 ~]#

2.查看mysqlhotcopy的帮助信息
[iyunv@tong2 ~]# vim /usr/my.cnf     --在配置文件中添加如下参数
[mysqlhotcopy]
interactive-timeout
host=localhost
user=root
password=system
port=3306
[iyunv@tong2 ~]# /etc/init.d/mysql restart      --重启服务
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[iyunv@tong2 ~]# mysqlhotcopy  --help
Warning: /usr/bin/mysqlhotcopy is deprecated and will be removed in a future version.
/usr/bin/mysqlhotcopy Ver 1.23
Usage: /usr/bin/mysqlhotcopy db_name[./table_regex/] [new_db_name | directory]
  -?, --help           display this help-screen and exit
  -u, --user=#         user for database login if not current user
  -p, --password=#     password to use when connecting to server (if not set
                       in my.cnf, which is recommended)
  -h, --host=#         hostname for local server when connecting over TCP/IP
  -P, --port=#         port to use when connecting to local server with TCP/IP
  -S, --socket=#       socket to use when connecting to local server
      --old_server     connect to old MySQL-server (before v5.5) which
                       doesn't have FLUSH TABLES WITH READ LOCK fully implemented.
  --allowold           don't abort if target dir already exists (rename it _old)     --不覆盖以前备份的文件
  --addtodest          don't rename target dir if it exists, just add files to it      --属于增量备份
  --keepold            don't delete previous (now renamed) target when done
  --noindices          don't include full index files in copy          --不备份索引文件
  --method=#           method for copy (only "cp" currently supported)
  -q, --quiet          be silent except for errors
  --debug              enable debug                                          --启用调试输出
  -n, --dryrun         report actions without doing them
  --regexp=#           copy all databases with names matching regexp   --使用正规表达式
  --suffix=#           suffix for names of copied databases
  --checkpoint=#       insert checkpoint entry into specified db.table    --插入检查点条目
  --flushlog           flush logs once all tables are locked                    --所有表锁定后刷新日志
  --resetmaster        reset the binlog once all tables are locked         --一旦锁表重置binlog文件
  --resetslave         reset the master.info once all tables are locked   --一旦锁表重置master.info文件   
  --tmpdir=#        temporary directory (instead of /tmp)
  --record_log_pos=#   record slave and master status in specified db.table
  --chroot=#           base directory of chroot jail in which mysqld operates
  Try 'perldoc /usr/bin/mysqlhotcopy' for more complete documentation
[iyunv@tong2 ~]#

3.备份一个数据库到一个目录中
[iyunv@tong2 ~]# mysqlhotcopy -u root -p system tong /opt/
[iyunv@tong2 ~]# ll /opt/tong/
total 112
-rw-rw----. 1 mysql mysql    15 Jan  5 14:35 q.isl
-rw-rw----. 1 mysql mysql  8554 Jan  4 18:03 t.frm
-rw-rw----. 1 mysql mysql 98304 Jan  4 18:03 t.ibd
[iyunv@tong2 ~]# ll /var/lib/mysql/tong
total 112
-rw-rw----. 1 mysql mysql    15 Jan  5 14:35 q.isl
-rw-rw----. 1 mysql mysql  8554 Jan  4 18:03 t.frm
-rw-rw----. 1 mysql mysql 98304 Jan  4 18:03 t.ibd
[iyunv@tong2 ~]#

4.备份多个数据库到一个目录中
[iyunv@tong2 ~]# mysqlhotcopy -u root -p system tong mysql /opt/tong
[iyunv@tong2 ~]# ll /opt/
total 8
drwxr-x---. 2 mysql mysql 4096 Jan  5 15:29 mysql
drwxr-x---. 2 mysql mysql 4096 Jan  5 15:29 tong
[iyunv@tong2 ~]# ll /var/lib/mysql/{mysql,tong} -d
drwxr-xr-x. 2 mysql mysql 4096 Jan  5 15:29 /var/lib/mysql/mysql
drwxr-xr-x. 2 mysql mysql 4096 Jan  5 15:29 /var/lib/mysql/tong
[iyunv@tong2 ~]#

5.备份数据库中某一个表
[iyunv@tong2 ~]# mysqlhotcopy -u root -p system mysql./user*/ /opt/
[iyunv@tong2 ~]# ll /opt/mysql/
total 20
-rw-r--r--. 1 mysql mysql 10684 Jan  4 16:49 user.frm
-rw-r--r--. 1 mysql mysql   784 Jan  4 16:49 user.MYD
-rw-r--r--. 1 mysql mysql  2048 Jan  4 16:49 user.MYI
[iyunv@tong2 ~]# ll /var/lib/mysql/mysql/user.*
-rw-r--r--. 1 mysql mysql 10684 Jan  4 16:49 /var/lib/mysql/mysql/user.frm
-rw-r--r--. 1 mysql mysql   784 Jan  4 16:49 /var/lib/mysql/mysql/user.MYD
-rw-r--r--. 1 mysql mysql  2048 Jan  4 16:49 /var/lib/mysql/mysql/user.MYI
[iyunv@tong2 ~]#

6.恢复数据
[iyunv@tong2 ~]# rm -rf /var/lib/mysql/tong
[iyunv@tong2 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.6.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
[iyunv@tong2 ~]# cp -arp /opt/tong /var/lib/mysql/         --将备份的数据移到mysql数据根目录
[iyunv@tong2 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.6.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \u tong
Database changed
mysql> show tables;
+----------------+
| Tables_in_tong |
+----------------+
| t              |
+----------------+
2 rows in set (0.00 sec)
mysql> exit
Bye
[iyunv@tong2 ~]#


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-40250-1-1.html 上篇帖子: PHP+MySQL网站打开速度慢的几个因素 下篇帖子: 将excel表的内容导入mysql数据库的方法 mysql
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表