首页 » Linux » mysql load data and into outfile using

mysql load data and into outfile using

 

基本语法如下:

Syntax:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number {LINES | ROWS}]
    [(col_name_or_user_var
        [, col_name_or_user_var] ...)]
    [SET col_name={expr | DEFAULT},
        [, col_name={expr | DEFAULT}] ...]

 

导出时,需要导出到指定的目录,不然会报错,默认的目录是:/var/lib/mysql-files/下面。

导出命令:select 。。。from table where .. into outfile ”;

mysql> select * from test into outfile '/tmp/test.txt';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
mysql> show variables like 'secure-file-priv';
Empty set (0.02 sec)

mysql> show variables like '%secur%';
+--------------------------+-----------------------+
| Variable_name            | Value                 |
+--------------------------+-----------------------+
| require_secure_transport | OFF                   |
| secure_auth              | ON                    |
| secure_file_priv         | /var/lib/mysql-files/ |
+--------------------------+-----------------------+
3 rows in set (0.01 sec)

mysql> select * from test into outfile '/var/lib/mysql-files/test.txt';
Query OK, 3 rows affected (0.00 sec)

 

2、导入命令分几种情况,以下说明是对mysql 5.7的:

1)、load data  [local] infile, local加不加的问题,如果数据库和文件在同一台,不用加local,但文件必须要放到mysql指定的安全目录,默认是:/var/lib/mysql-files/

2)、登录数据库不在同一台的话,需要加上local,导入的文件位置随便放就可以了。

3、 导入时有重复数据处理情况:

1)、在into table 前面加上replace,表示替换掉已经存在表中的数据

2)、在into table前面加上ignore,表示忽略掉文件中一样的数据。

3)、命令中加了local,load data local infile ” …  ,意思和加上ignore一样。

 

 

原文链接:mysql load data and into outfile using,转载请注明来源!

0