init-file 是在MySQL启动的时候加载的脚本。
有两个要注意的。
1. 确保你的mysqld 编译的时候没有加 --disable-grant-options 开关。
2. 确保init-file指定的脚本每行一个具体的语句。
使用方法很简单,直接添加到配置文件,比如my.cnf.
添加:
[server] 或者 [mysqld] 或者 [mysqld_safe]
init-file="Your file location"
重启mysqld 就可以看到效果了。
我来做个简单的例子。利用init-file来生成一个CACHE表的数据。
环境: Vbox 虚拟机 Ubuntu 32Bit, MySQL 5.1.30, 启动脚本/home/david/script/control_db
[email=root@david-desktop:/var/log/mysql]root@david-desktop:/var/log/mysql[/email]# /home/david/scripts/control_db enter
Enter password:
...mysql> use ytt;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table ytt_test ( id int unsigned not null, suffix tinyint unsigned not null, primary key(id,suffix));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into ytt_test (id,suffix) values (1,10),(1,20),(1,50),(1,52),(2,55),(2,1),(2,30),
->(2,80),(3,100),(3,22),(3,4),(4,50),(4,20),(4,2),(5,10),(5,90);
Query OK, 16 rows affected (0.00 sec)
Records: 16 Duplicates: 0 Warnings: 0
简单的CACHE表。
mysql> create table ytt_test_heap engine memory select * from ytt_test where 0;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
任务就是取到每个分组的最大值。
mysql> select a.* from ytt_test as a where suffix = (select max(suffix) from ytt_test where id = a.id);
+----+--------+
| id | suffix |
+----+--------+
| 1 | 52 |
| 2 | 80 |
| 3 | 100 |
| 4 | 50 |
| 5 | 90 |
+----+--------+
5 rows in set (0.02 sec)
然后放到CACHE表中。关于SQL语句优化方面我另外会写的。现在时间多啊。
现在搞init-file
加如下部分到my.cnf
[server]
init-file=/home/david/i.e/init.file
文件/home/david/i.e/init.file 内容很简单,如下:
use ytt;
insert into ytt_test_heap select a.* from ytt_test as a where suffix = (select max(suffix) from ytt_test where id = a.id);
给相应的权限。
[email=root@david-desktop:/var/log/mysql]root@david-desktop:/var/log/mysql[/email]# chown -R mysql.mysql /home/david/i.e/init.file
[email=root@david-desktop:/var/log/mysql]root@david-desktop:/var/log/mysql[/email]# chmod 0660 /home/david/i.e/init.file
然后重启 mysqld.
[email=root@david-desktop:/var/log/mysql]root@david-desktop:/var/log/mysql[/email]# /home/david/scripts/control_db stop
[email=root@david-desktop:/var/log/mysql]root@david-desktop:/var/log/mysql[/email]# /home/david/scripts/control_db start
[email=root@david-desktop:/var/log/mysql]root@david-desktop:/var/log/mysql[/email]# /home/david/scripts/control_db enter
Enter password:
...
mysql> select * from ytt_test_heap;
+----+--------+
| id | suffix |
+----+--------+
| 1 | 52 |
| 2 | 80 |
| 3 | 100 |
| 4 | 50 |
| 5 | 90 |
+----+--------+
5 rows in set (0.00 sec)
看到效果了吧。
转自:
http://blog.chinaunix.net/u/29134/showart_1761742.html
作者: coolstr
发布时间: 2009-03-03