MySQL database backup and recovery (1)-use Select Into Outfile and Load Data Infile commands

MySQL database backup and recovery (1)-use Select Into Outfile and Load Data Infile commands

1. Use the Select Into Outfile command to export data

The command format is as follows:

SELECT ... 
INTO OUTFILE 'file_name'
fields terminated by 'char';                                              

Note:
(1) The path where the file specified by the OUTFILE parameter is located must have mysql access permission, otherwise an error will be reported.
(2) The data of each record is separated by Tab by default, or you can use the fields terminated parameter to specify the separator.
(3) To execute the Select into outfile and Load data infile commands, you need to enable the setting of the secure_file_priv parameter in the my.cnf parameter file. The settings of this parameter are as follows:
——NULL: MySQL service will prohibit import and export operations;
——Directory name: MySQL service only allows file import and export operations to be performed in this directory. The directory must exist, the MySQL service will not create it;
——Empty string (''): the file can be in any location.

1. Check the value of the secure_file_priv parameter

mysql> show variables like '%secure_file_priv%';
+------------------+-----------------------+
| Variable_name    | Value                 |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.01 sec)

2. Modify the value of the secure_file_priv parameter

[root@Mysql11 ~]# vim /etc/my.cnf  ##编辑MySQL配置文件
#######################################################################
[mysqld]
..........
secure_file_priv=''
..........
#######################################################################

Restart the MySQL service and check the value of the secure_file_priv parameter

mysql> show variables like '%secure_file_priv%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| secure_file_priv |       |
+------------------+-------+
1 row in set (0.00 sec)

3. Examples

(1) Use select to export data-do not specify a separator

mysql> select * from stu into outfile '/tmp/stu.txt';
Query OK, 5 rows affected (0.00 sec)

View the content of the stu.txt file, and use the tab key to separate the data.

[root@Mysql11 ~]# cat /tmp/stu.txt
1	zhangsan	20	Xinxiang	15578941258
2	tom	20	Xinxiang	13778942222
3	jack	20	Zhengzhou	13675871454
4	john	21	Zhengzhou	13937681111
5	mark	22	Aanyang	13055882233

(2) Use select to export data-specify the separator

mysql> select * from stu into outfile '/tmp/stu2.txt' fields terminated by ',';
Query OK, 5 rows affected (0.00 sec)

View the content of the stu.txt file, and use a comma (,) to separate the data.

[root@Mysql11 ~]# cat /tmp/stu2.txt;
1,zhangsan,20,Xinxiang,15578941258
2,tom,20,Xinxiang,13778942222
3,jack,20,Zhengzhou,13675871454
4,john,21,Zhengzhou,13937681111
5,mark,22,Aanyang,13055882233

Two, use the Load Data Infile command to import data

The command format is as follows:

LOAD DATA [LOCAL] INFILE 'file_name'
INTO TABLE tbl_name
[TERMINATED BY 'string';

Description:
(1) Specify the corresponding separator according to the file format;
(2) To execute Load data on the non-server side, local is required. For example, to log in to mysqld on A from machine B, you need to use local.

1. Use the stu.txt file to import data

(1) Clear the data in the stu table

mysql> truncate stu;
Query OK, 0 rows affected (0.04 sec)

mysql> select * from stu;
Empty set (0.00 sec)

(2) Import data

mysql> load data infile '/tmp/stu.txt' into table stu;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from stu;
+----+----------+------+-----------+-------------+
| id | name     | age  | address   | phone       |
+----+----------+------+-----------+-------------+
|  1 | zhangsan |   20 | Xinxiang  | 15578941258 |
|  2 | tom      |   20 | Xinxiang  | 13778942222 |
|  3 | jack     |   20 | Zhengzhou | 13675871454 |
|  4 | john     |   21 | Zhengzhou | 13937681111 |
|  5 | mark     |   22 | Aanyang   | 13055882233 |
+----+----------+------+-----------+-------------+
5 rows in set (0.00 sec)

2. Use the stu2.txt file to import data

(1) Clear the data in the stu table

mysql> truncate stu;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from stu;
Empty set (0.00 sec)

(2) Import data

If you do not specify the separator, the following error will occur:

mysql> load data infile '/tmp/stu2.txt' into table stu;
ERROR 1265 (01000): Data truncated for column 'id' at row 1

Specify the separator, the import is successful:

mysql> load data infile '/tmp/stu2.txt' into table stu fields terminated by ',';
Query OK, 5 rows affected (0.00 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from stu;
+----+----------+------+-----------+-------------+
| id | name     | age  | address   | phone       |
+----+----------+------+-----------+-------------+
|  1 | zhangsan |   20 | Xinxiang  | 15578941258 |
|  2 | tom      |   20 | Xinxiang  | 13778942222 |
|  3 | jack     |   20 | Zhengzhou | 13675871454 |
|  4 | john     |   21 | Zhengzhou | 13937681111 |
|  5 | mark     |   22 | Aanyang   | 13055882233 |
+----+----------+------+-----------+-------------+
5 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/107072574