MySQL 备份

普通文件方式

简单数据的导入和导出

编辑配置文件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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 开启二进制日志 在 /etc/my.cnf 的 [mysqld] 配置域中添加如下配置内容 /var/log/my...
    运维开发_西瓜甜阅读 7,020评论 1 42
  • 备份重要性 重要性备份是DBA的救命稻草没有备份,就没有复活的机会备份也可以让数据回档到某一时刻 误区备份占用太多...
    显卡hg阅读 483评论 0 0
  • 4种备份恢复的方式 mysqldump mysqlbackup mysqlhotcopy xtrabackup/i...
    JaeGwen阅读 4,272评论 1 2
  • reactive():包装一个正常表达式来创建一个反应式表达式。从概念上讲,反应式表达式是一种表达式,其结果将随时...
    D_MarsD阅读 711评论 0 1
  • 出生时,快乐是一个温暖的怀抱 那样我可以得到父母的呵护 幼年时,快乐是一张小小的钱币 那样我可以买到很多好吃的 童...
    江潇然阅读 106评论 0 0