使用文件向表中导入导出记录

  1. 修改 my.ini

    • @@global 是用于引用 Mysql 系统全局变量的关键字,连接到 Mysql 服务器的所有客户端都会引用系统全局变量的值;
      secure_file_priv 用于指定允许导入、导出文件的路径。
      查看系统全局变量 secure_file_priv 的值,可以使用下面的命令:
      
          select @@global.secure_file_priv;
                                                     
      
      mysql> select @@global.secure_file_priv;
      +---------------------------+
      | @@global.secure_file_priv |
      +---------------------------+
      | NULL                      |
      +---------------------------+
      1 row in set (0.023 sec)
      
      mysql>
                                                     
      • secure_file_priv 的值是 NULL:不能进行导入或导出。
      • secure_file_priv 的值是空字符串 "" 即不显示任何内容: 任何路径都可以导入或导出。

    • Windows 上建立初始化配置文件 my.ini : 新建一个 my.txt 文件,然后另存为 my.ini 。 文件内容如下:
      
         [mysqld]
       # 允许任何路径都可以导入或导出
         secure_file_priv=""
                                                     
      • 把 my.ini 文件放到和 bin 文件相同的文件中。
      • 停止服务,然后重启服务,系统会读取 my.ini 文件的内容。
      重新查看系统全局变量 secure_file_priv 的值:
      mysql> select @@global.secure_file_priv;
      +---------------------------+
      | @@global.secure_file_priv |
      +---------------------------+
      |                           |
      +---------------------------+
      1 row in set (0.005 sec)
      
      mysql>
                                                     
  2. 使用文件向表中导入记录

    • 新创建表 wechat:
      create table wechat ( id varchar(20), nickname varchar(20));
      假设 D:/doc/it( Windows 是 D:\doc\it ) 下有下面的文本文件 a.txt
      abcdef  小清新
      k8899s  龙校长
                                      
      下面的命令,使得 a.txt 中的两条记录导入到表 wechat:
      
        load data infile 'd:/doc/it/a.txt' into table wechat  fields terminated by '  ';
                                                     
      mysql>  load data infile 'd:/doc/it/a.txt' into table wechat  fields terminated by '  ';
      Query OK, 2 rows affected (0.594 sec)
      Records: 2  Deleted: 0  Skipped: 0  Warnings: 0
      
      mysql>
                                                     
      • fields terminated by ' ' 指定两个空格为分隔符,文本文件中字段间也必需两个空格。
      • a.txt 的字符编码需要和服务器端的字符编码一致。
      查看表中记录的变化:
      mysql> select * from wechat ;
      +--------+----------+
      | id     | nickname |
      +--------+----------+
      | bcdef  | 小清新
      | k8899s | 龙校长
      +--------+----------+
      2 rows in set (0.006 sec)
      
      mysql>
                                                     
  3. 把表中记录导出到文件

    • 执行下面的命令:
      
        select * into outfile 'd:/doc/it/b.txt' fields terminated by '  ' from wechat;
                                                     
      mysql> select * into outfile 'd:/doc/it/b.txt' fields terminated by '  ' from wechat;
      Query OK, 2 rows affected (0.244 sec)
      
      mysql>
                                                     
      • 上面的命令将在 D:/doc/it( Windows 是 D:\doc\it ) 下新建文件 b.txt 。 注意如果已经存在 b.txt 会提示错误。
      • 把表 wechat 中的记录导出到文件 b.txt 。
      打开文件 b.txt 会看到下面的内容:
      abcdef  小清新
      k8899s  龙校长