美文网首页
MySQL 备份

MySQL 备份

作者: 你笑的那么美丶 | 来源:发表于2019-03-09 11:28 被阅读0次

普通文件方式

简单数据的导入和导出

编辑配置文件my.cnf添加如下配置项

[mysqld]
secure_file_priv = 保存到本地目录
例如
vim /etc/my.cnf
secure_file_priv = /tmp

配置完成后需要重启服务

本地目录需要给mysql用户授予读写权限

登录到服务器,可以验证配置项是否生效

SELECT @@GLOBAL.secure_file_priv;
或者
SHOW VARIABLES LIKE "secure_file_priv";
image.png

执行验证命令

mysql> select @@global.secure_file_priv;
+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
| /tmp/                     |
+---------------------------+
1 row in set (0.00 sec)

如果注释掉此配置项,查询到的值为NULL

image.png
mysql> select @@global.secure_file_priv;
+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
| NULL                      |
+---------------------------+
1 row in set (0.00 sec)

每次更新配置文件都要重启服务配置文件才会生效

导出

mysql> select * from test.class into outfile "/tmp/class.db";
Query OK, 3 rows affected (0.00 sec)
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls
class.db

导入

导入到数据文件中的列必须和表中的一 一对应。包括id列的值。

导入语句

注意导入语句之前要保证要导入的语句和表格中的主键号不能重复,否则会报错

1,高三1班
2,高三2班
3,高三3班
mysql> load data infile '/tmp/class.db' into table class fields terminated by ',';
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'   #键“primary”的重复项“1”
修改一下语句的主键项
4,高三1班
5,高三2班
6,高三3班
mysql> load data infile '/tmp/class.db' into table class terminated by ',';
Query OK, 3 rows affected (0.01 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0
mysql> select * from class
    -> ;
+----+------------+
| id | name       |
+----+------------+
|  1 | 高三1班    |
|  2 | 高三2班    |
|  3 | 高三3班    |
|  4 | 高三1班    |
|  5 | 高三2班    |
|  6 | 高三3班    |
+----+------------+
6 rows in set (0.00 sec)

载入外部“形式整齐”的数据(csv 格式的文件,没有域之家用逗号隔开): 
load data infile  '文件完整名(含路径)' into table 表名
fields terminated by '域分隔符'   optionally    enclosed  by '"'
ignore 1 lines;

示例文件
vim class.db

1 高三1班
2 高三2班
3 高三3班

因为示例文件中1和高三1班之间是以空格进行分割所以fields terminated by '域分隔符' #域分隔符为空格

mysql> load data infile '/tmp/class.db' into table class fields terminated by ' ';
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

例如域分隔符为“,”

mysql> delete from class;
Query OK, 3 rows affected (0.04 sec)

mysql> load data infile '/tmp/class.db' into table class fields terminated by ',';
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0
1 "高三,1班"
2 "高三,2班"
3 "高三,3班"
mysql> load data infile '/tmp/class.db' into table class fields terminated by ' ' optionally enclosed by "";
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

相关文章

网友评论

      本文标题:MySQL 备份

      本文链接:https://www.haomeiwen.com/subject/tezjpqtx.html