欢迎关注MySQL 8.0必知必会系列课程。

    MySQL8.0必知必会-自动化部署            https://edu.51cto.com/course/16368.html
    MySQL8.0必知必会之参数标准化配置        https://edu.51cto.com/course/16358.html



1 背景介绍

把OS的文件导入到数据库中

2、语法

load data infile "文件名"

into table 表名

fields terminated by "分隔符"

lines terminated by "\n"

3、练习

把/etc/passwd文件中的内容导入到的userinfo表中

root:x:0:0:root:/root:/bin/bash

用户名:密码:UID:GID:用户描述:主目录:登录权限



2 操作步骤

在数据中创建对应的表

将要导入的文件拷贝到数据库的默认搜索路径中

将系统文件导入到创建的表中

  • 创建表

create table userinfo(

username char(20),

password char(1),

uid int,

gid int,

comment varchar(50),

homedir varchar(50),

shell varchar(50)

);

  • 将要导入的文件拷贝到数据库的默认搜索路径中

如何查看数据库的默认搜索路径

show variables like "secure_file_priv";

sudo cp /etc/passwd /var/lib/mysql-files/

3、执行数据导入语句

load data infile "/var/lib/mysql-files/passwd"

into table userinfo

fields terminated by ":"

lines terminated by "\n"

4、在userinfo表中第一列添加一个id字段,类型为int,设置为主键带自增长属性

alter table userinfo add id int primary key auto_increment first


来源于:https://www.cnblogs.com/taoke2016/p/9002616.html