MySQL --init-file 参数可以让 memory 表的固定数据永不丢失

众所周知, memory 存储引擎的表,数据只保留在内存,重启 MySQL 服务或主机后,表数据会丢失。

但可以在启动  MySQL 服务时,通过  --init-file 参数,指定需要执行插入数据到 memory 表的 sql 文件或在该sql文件中引用 load data infile 通过直接装载文件的方式,将固定格式的文本数据加载进 MySQL表,如下:

1、在 insert_tmem.sql 文件中加入要插入到  memory 表 t_mem 的sql语句,如:

insert into mytest.t_mem  select * from t1;


或在 insert_tmem.sql 文件中加入 load data infile 方式装载文本文件的数据,如下:

[root@dg-st tmp]# cat insert_tmem.sql
load data infile '/root/tmp/t_mem.txt' replace into table mytest.t_mem
character set utf8 fields terminated by ',' enclosed by ''
lines terminated by '\n' (id,names,addr) set upd_time=current_timestamp;


2、其中,/root/tmp/t_mem.txt 要装载的源数据文本文件数据格式如下:

[root@dg-st tmp]# cat t_mem.txt
1,chen,mm
2,hong,gz
3,quan,cn
4,samdy,us


3、启动 mysql 服务时,通过指定 --init-file=/root/tmp/insert_tmem.sql 参数,启动 mysql 后,会自动装载数据到 memory 表 t_mem,如下:

[root@dg-st tmp]# mysqld_safe --user=mysql --init-file=/root/tmp/insert_tmem.sql &


4、启动 mysql ,发现数据已经成功装载进来了:

mysql> select * from t_mem;
+------+-------+------+---------------------+
| id   | names | addr | upd_time            |
+------+-------+------+---------------------+
|    1 | chen  | mm   | 2017-09-19 08:59:48 |
|    2 | hong  | gz   | 2017-09-19 08:59:48 |
|    3 | quan  | cn   | 2017-09-19 08:59:48 |
|    4 | samdy | us   | 2017-09-19 08:59:48 |
+------+-------+------+---------------------+
4 rows in set (0.00 sec)



5、t_mem  的存储引擎类型是 memory:

mysql> select table_name,engine from information_schema.tables where table_name='t_mem'; 
+------------+--------+
| table_name | engine |
+------------+--------+
| t_mem      | MEMORY |
+------------+--------+

mysql> show create table t_mem \G   
*************************** 1. row ***************************
       Table: t_mem
Create Table: CREATE TABLE `t_mem` (
  `id` int(11) DEFAULT NULL,
  `names` varchar(100) COLLATE utf8_bin DEFAULT NULL,
  `addr` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  `upd_time` datetime DEFAULT NULL,
  KEY `idx_tmem_id` (`id`)

) ENGINE=MEMORY DEFAULT CHARSET=utf8 COLLATE=utf8_bin


(完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值