数据的导出导入:
数据导入与导出默认检索路径
查看
show variables like "%file%";
show variables like "secure_file_priv";
system ls /var/lib/
导入的文件需要拷贝到这个路径,导出的时候,文件也需要存放到这个路径
这个路径在安装服务时已经自动创建
修改搜索路径
操作步骤:
vim /etc/my.cnf
[mysqld]
secure_file_priv="/myload"
mkdir /myload
chown mysql /myload
systemctl restart mysqld
mysql -uroot -p
show variables like "secure_file_priv";
数据导入
数据导入介绍
把系统文件内容存储到数据库服务器的表里
数据导入步骤
- 默认只有root用户有数据导入权限
- 建表(表结构根据文件内容创建)
- 导入数据
命令格式
mysql>load data infile “目录名/文件名”
into table 库名.表名
fields terminated by “分隔符” #怎么区分文件中的列
lines terminated by “\n” ; #怎么区分文件中的行
例子
把系统用户信息/etc/passwd 存储到db3库下的user 表里
create database db3;
create table db3.user(
name char(20),
password char(1),
uid int,
gid int,
comment varchar(150),
homedir char(150),
shell char(50)
);
desc db3.user;
system cp /etc/passwd /myload
system ls /myload
load data local infile "/myload/passwd"
into table db3.user
fields terminated by ":"
lines terminated by"\n" ;
select * from db3.user;
优化:给表加一个编号
alter table db3.user add id int primary key auto_increment first;
select * from db3.user;
select id from db3.user;
数据导出
数据导出介绍
把表记录保存到系统文件中
命令格式
格式1
select命令 into outfile “目录名/文件名” ;
格式2
select into outfile “目录名/文件名”
fields terminated by " 分隔符" ;
格式3
select into outfile “目录名/文件名”
fields terminated by " 分隔符"
lines terminated by “\n” ;
例子
把某个查询结果输出到某个文件
select * from db3.user where id=1 into outfile "/myload/user1.txt";
system cat /myload/user1.txt
select name,homedir,shell from db3.user where id <=2 into outfile "/myload/user2.txt" fields terminated by "##" lines terminated by "[@@@]";
system cat /myload/user2.txt
|