本节记录数据的导入导出和备份恢复相关操作
数据导出
- SELECT ... FROM table INTO OUTFILE file
1
2
3
4
5
6(root@localhost) [book]> select * from student
-> into outfile '~/mysqlBackup/studentbk.txt'
-> fields terminated by ',' 字段间隔符
-> optionally enclosed by '"' 字符串包裹符
-> lines terminated by '?'; 行间隔符
Query OK, 3 rows affected (0.01 sec)
数据导入
- LOAD DATA INFILE file INTO TABLE table
1
2
3
4
5
6
7(root@localhost) [book]> load data infile '~/mysqlBackup/studentbk.txt'
-> into table student
-> fields terminated by ','
-> optionally enclosed by '"'
-> lines terminated by '?';
Query OK, 3 rows affected (0.01 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
数据备份
mysqldump -u user -p possword --all-database > file
1
$ mysqldump -u root --all-databases>./alldata.sql
mysqldump -u user -p possword --tab=directory database
1
2
3
4
5
6
7$ mysqldump -u root --tab=. book
$ ls
aaa.sql book_cp.txt borrow_copy.txt student.sql t.txt
aaa.txt book_view.sql g.sql student.txt test_copy.sql
book.sql borrow.sql g.txt student_copy.sql test_copy.txt
book.txt borrow.txt o.sql student_copy.txt test_create.sql
book_cp.sql borrow_copy.sql o.txt t.sql test_create.txt
数据恢复
mysql -u user -p password database < file
1
$ mysql -u root book < ./alldata.sql
mysqlimport -u user -p password database file file必须写全路径,用来恢复数据,不能表结构
1
2$ mysqlimport -u root book ~/mysqlBackup/book_cp.txt
book.book_cp: Records: 4 Deleted: 0 Skipped: 0 Warnings: 0