mysql数据备份和恢复

来源:互联网 发布:逆波兰表达式算法 视频 编辑:程序博客网 时间:2024/05/17 03:44


mysqlhotcopy

yum install perl-DBD-MySQL perl-DBI perl perl-DBD-MySQL

[root@localhost database]# wget http://www.fcbu.com/upimages/soft/DBD-mysql-4.020.tar.gz

[root@localhost softs]# tar xf DBD-mysql-4.020.tar.gz -C /usr/src/
[root@localhost softs]# cd /usr/src/DBD-mysql-4.020/
[root@localhost DBD-mysql-4.020]# perl Makefile.PL --mysql_config=/usr/local/mysql/bin/mysql_config

[root@localhost DBD-mysql-4.020]# make

[root@localhost DBD-mysql-4.020]# make install

当然如果你是编译安装的,直接到自己编译安装的目录里

我想我数据库中的iwebmall这个库备份到/tmp目录下

[root@localhost DBD-mysql-4.020]# /usr/local/mysql/bin/mysqlhotcopy -u root -p 123 --socket=/var/run/mysqld/mysql5.socket iwebmall /tmp/

[root@localhost DBD-mysql-4.020]# /usr/local/mysql/bin/mysqlhotcopy -u root -p 123 --socket=/var/run/mysqld/mysql5.socket iwebmall /tmp/
Flushed 68 tables with read lock (`iwebmall`.`imall_admin_group`, `iwebmall`.`imall_admin_log`, `iwebmall`.`imall_admin_user`, `iwebmall`.`imall_areas`, `iwebmall`.`imall_article`, `iwebmall`.`imall_article_cat`, `iwebmall`.`imall_asd_content`, `iwebmall`.`imall_asd_position`, `iwebmall`.`imall_attribute`, `iwebmall`.`imall_brand`, `iwebmall`.`imall_brand_category`, `iwebmall`.`imall_cart`, `iwebmall`.`imall_category`, `iwebmall`.`imall_category_copy`, `iwebmall`.`imall_complaint_type`, `iwebmall`.`imall_complaints`, `iwebmall`.`imall_credit`, `iwebmall`.`imall_crons`, `iwebmall`.`imall_flink`, `iwebmall`.`imall_goods`, `iwebmall`.`imall_goods_attr`, `iwebmall`.`imall_goods_comment`, `iwebmall`.`imall_goods_gallery`, `iwebmall`.`imall_goods_shipping`, `iwebmall`.`imall_goods_transport`, `iwebmall`.`imall_goods_types`, `iwebmall`.`imall_groupbuy`, `iwebmall`.`imall_groupbuy_log`, `iwebmall`.`imall_img_size`, `iwebmall`.`imall_index_images`, `iwebmall`.`imall_integral`, `iwebmall`.`imall_keywords_count`, `iwebmall`.`imall_mailtpl`, `iwebmall`.`imall_nav`, `iwebmall`.`imall_order_goods`, `iwebmall`.`imall_order_info`, `iwebmall`.`imall_payment`, `iwebmall`.`imall_plugin_url`, `iwebmall`.`imall_plugins`, `iwebmall`.`imall_privilege`, `iwebmall`.`imall_protect_rights`, `iwebmall`.`imall_receiv_list`, `iwebmall`.`imall_refund_list`, `iwebmall`.`imall_remind`, `iwebmall`.`imall_remind_info`, `iwebmall`.`imall_remind_user`, `iwebmall`.`imall_settings`, `iwebmall`.`imall_shipping`, `iwebmall`.`imall_shipping_list`, `iwebmall`.`imall_shop_article`, `iwebmall`.`imall_shop_categories`, `iwebmall`.`imall_shop_category`, `iwebmall`.`imall_shop_guestbook`, `iwebmall`.`imall_shop_honor`, `iwebmall`.`imall_shop_info`, `iwebmall`.`imall_shop_inquiry`, `iwebmall`.`imall_shop_payment`, `iwebmall`.`imall_shop_request`, `iwebmall`.`imall_tag`, `iwebmall`.`imall_tag_user`, `iwebmall`.`imall_tmp_img`, `iwebmall`.`imall_transport`, `iwebmall`.`imall_user_address`, `iwebmall`.`imall_user_favorite`, `iwebmall`.`imall_user_info`, `iwebmall`.`imall_user_rank`, `iwebmall`.`imall_users`, `iwebmall`.`imall_word`) in 1 seconds.
Locked 0 views () in 0 seconds.
Copying 199 files...
Copying indices for 0 files...
Unlocked tables.
mysqlhotcopy copied 68 tables (199 files) in 0 seconds (3 seconds overall).

mysqlhotcopy 备份出来的是整个数据库目录,使用时可以直接拷贝到mysqld指定的datadir目录下就行了,同时要注意权限的问题。

[root@localhost tmp]# cp -fr ./iwebmall  /database/ -r

[root@localhost tmp]# chown mysql.mysql -R /database


######################################################################################################

mysqldump

mysqldump备份数据后,可以将数据转移到另一个数据库服务器(不一定是mysql)。如果在本地备份myisam数据库,建议使用mysqlhotcopy,因为这个可以更快。

mysqldump最常用的就是用于备份一个完整的数据库

将数据导出成sql的格式,然后在拷贝到其他地方,使用source导入即可,不过在导入之前需要授权,这种方法不必担心授权的问题。

[root@localhost tmp]# /usr/local/mysql/bin/mysqldump -u root -p --database iwebmall > iwebmall.sql

[root@localhost tmp]# ll iwebmall.sql 
-rw-r--r-- 1 root root 247641 Oct 14 11:23 iwebmall.sql

[root@localhost tmp]# chown mysql.mysql iwebmall.sql 
[root@localhost tmp]# source iwebmall.sql 

[root@localhost tmp]# mysql -u root -p123 < iwebmall.sql 

##################################################################################

mysqlimport

mysqlimport是一个数据导入程序,客户端提供了load data infiles 语句的一个命令行接口,mysqlimport 的大多数选项直接对应load data infile子句。

[root@localhost tmp]# cat test.txt 
1 qw
2 er
3 ty
4 ui
5 op
[root@localhost tmp]# mysql -u root -p123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13

mysql> use iwebmall
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table test(id int, name varchar(11));
Query OK, 0 rows affected (0.08 sec)
mysql> \q       
Bye

[root@localhost tmp]# /usr/local/mysql/bin/mysqlimport -u root -p123 --socket=/var/run/mysqld/mysql5.socket --local iwebmall test.txt 
Warning: Using a password on the command line interface can be insecure.
iwebmall.test: Records: 5  Deleted: 0  Skipped: 0  Warnings: 0
[root@localhost tmp]# mysql -u root -p123

mysql> use iwebmall
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | qw   | 
|    2 | er   | 
|    3 | ty   | 
|    4 | ui   | 
|    5 | op   | 
+------+------+
5 rows in set (0.00 sec)
###############################################################

backup table 

backup table tabl1ename [,table2name...] to '/path/to/backup/directory'

backup table 用于刷新了所有对磁盘的缓冲变更后,把恢复表所需要的最少数目的表文件拷贝到备份目录中。只适用于myisam,它可以拷贝.frm定义文件和.MYD数据文件。.MYI索引文件可以从这两个文件中重建。本目录应被指定为一个完整地路径名。

backup table语法跟mysqlhotcopy基本类似,原理也类似,都是锁表,然后草被数据文件,它能实现在线备份,但是效果不理想。它只拷贝表结构文件和数据文件,不拷贝索引文件,因此恢复比较慢。

select  into outfile

select into outfile 将一个表的数据导出到一个文本文件中去。把数据导出来成为一个普通的文本文件,可以自定义字段间隔的方式。

mysql> select * into outfile 'tt.txt' from iwebmall.test;
Query OK, 5 rows affected (0.03 sec)

[root@localhost tmp]# cd /database/     -----------------如果不指定目录,默认会导出到datadir中

[root@localhost database]# ls tt.txt 
tt.txt

##############################################################

load data local infile 

该语句用于高速从一个文本文件中读取行,并装入一个表。

mysql> load data local infile 'tt.txt' into table iwebmall.test;
Query OK, 5 rows affected (0.03 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0


mysql> 

mysql> select * from iwebmall.test;
+------+------+
| id   | name |
+------+------+
|    1 | qw   | 
|    2 | er   | 
|    3 | ty   | 
|    4 | ui   | 
|    5 | op   | 
+------+------+
5 rows in set (0.00 sec)


原创粉丝点击