本文来自:https://www.cnblogs.com/lettuce-u/p/10715795.html(自己收藏看)

  • 在localhost中准备好了一个test数据库和一个pet表:在电脑中准备一个pet.txt文件,里面包含想要导入到pet表的数据。(文件中一行代表一条数据,一条数据中的属性用Tab键隔开)
    1. 复制代码
      mysql> SHOW DATABASES;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | test               |
      +--------------------+
      2 rows in set (0.01 sec)
      
      mysql> USE test
      Database changed
      mysql> SHOW TABLES;
      +----------------+
      | Tables_in_test |
      +----------------+
      | pet            |
      +----------------+
      1 row in set (0.01 sec)
      复制代码
  • 向pet表载入pet.txt中的数据:
    1. mysql> LOAD DATA LOCAL INFILE 'E:\Desktop\pet.txt' INTO TABLE pet;
      ERROR 1148 (42000): The used command is not allowed with this MySQL version

      ERROR原因:服务器端,local_infile默认开启,客户端local_infile默认关闭,因此用的时候需要打开它

    2. 复制代码
      mysql> SHOW VARIABLES like 'local_infile';
      +---------------+-------+
      | Variable_name | Value |
      +---------------+-------+
      | local_infile  | OFF   |
      +---------------+-------+
      1 row in set, 1 warning (0.04 sec)
      复制代码

      开启local_infile:(开启后,再次执行SHOW VARIABLES like 'local_infile'会看到Value为ON)

    3. mysql> SET GLOBAL local_infile=ON;
      Query OK, 0 rows affected (0.00 sec)

      重新载入:

    4. mysql> LOAD DATA LOCAL INFILE 'E:\Desktop\pet.txt' INTO TABLE pet;
      ERROR 2 (HY000): File 'E:Desktoppet.txt' not found (OS errno 2 - No such file or directory)

      ERROR原因:路径应使用'/'   , 重新载入:

    5. mysql> LOAD DATA LOCAL INFILE 'E:/Desktop/pet.txt' INTO TABLE pet;
      Query OK, 8 rows affected, 7 warnings (0.02 sec)
      Records: 8  Deleted: 0  Skipped: 0  Warnings: 7  

      载入完毕,查看载入成功后的pet表: ψ(`∇´)ψ

    6. 复制代码
      mysql> select * from pet;
      +----------+--------+---------+------+------------+------------+
      | name     | owner  | species | sex  | birth      | death      |
      +----------+--------+---------+------+------------+------------+
      | Fluffy   | Harold | cat     | f    | 1993-02-04 | 0000-00-00 |
      | Claws    | Gwen   | cat     | m    | 1994-03-17 | 0000-00-00 |
      | Buffy    | Harold | dog     | f    | 1989-05-13 | 0000-00-00 |
      | Fang     | Benny  | dog     | m    | 1990-08-27 | 0000-00-00 |
      | Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
      | Chirpy   | Gwen   | bird    | f    | 1998-09-11 | 0000-00-00 |
      | Whistler | Gwen   | bird    |      | 1997-12-09 | 0000-00-00 |
      | Slim     | Benny  | snake   | m    | 1996-04-29 | 0000-00-00 |
      +----------+--------+---------+------+------------+------------+
      8 rows in set (0.00 sec
      复制代码