MySQL 创建新用户以及导入数据

新建用户

先登录进root用户,然后再利用root用户的权限进行创建用户操作

mysql> create user 'mrc'@'localhost' identified by '123456';

刷新授权:mysql> flush privileges;

为新用户分配权限:mysql> grant all privileges on car.* to mrc@localhost;

建表导入数据(txt)

  建立pet表:

  CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

  • 在电脑中准备一个pet.txt文件,里面包含想要导入到pet表的数据。(文件中一行代表一条数据,一条数据中的属性用Tab键隔开)
  • 向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

猜你喜欢

转载自www.cnblogs.com/dashuaiB/p/10725728.html