关于MySQL的init-file选项的用法实例
2008-12-30 14:27
260 查看
这段时间失业中,刚好在家有大把的时间学习ORACLE和MySQL.顺便也把PHP捡起来。逛逛论坛。
今天论坛上有人问起,刚好写个例子,贴上来。^_^
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
root@david-desktop:/var/log/mysql# /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);
给相应的权限。
root@david-desktop:/var/log/mysql# chown -R mysql.mysql /home/david/i.e/init.file
root@david-desktop:/var/log/mysql# chmod 0660 /home/david/i.e/init.file
然后重启 mysqld.
root@david-desktop:/var/log/mysql# /home/david/scripts/control_db stop
root@david-desktop:/var/log/mysql# /home/david/scripts/control_db start
root@david-desktop:/var/log/mysql# /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://yueliangdao0608.blog.51cto.com/397025/123538
今天论坛上有人问起,刚好写个例子,贴上来。^_^
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
root@david-desktop:/var/log/mysql# /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);
给相应的权限。
root@david-desktop:/var/log/mysql# chown -R mysql.mysql /home/david/i.e/init.file
root@david-desktop:/var/log/mysql# chmod 0660 /home/david/i.e/init.file
然后重启 mysqld.
root@david-desktop:/var/log/mysql# /home/david/scripts/control_db stop
root@david-desktop:/var/log/mysql# /home/david/scripts/control_db start
root@david-desktop:/var/log/mysql# /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://yueliangdao0608.blog.51cto.com/397025/123538
相关文章推荐
- 关于MySQL的init-file选项的用法实例
- 关于数据库里cascade的用法,在MySQL和Hibernate是不同的
- Mysql存储过程中游标的用法实例
- 关于改变innodb_log_file_size后无法启动mysql的问题
- 关于MySQL的Error1290 --secure-file-priv
- mysql的左右内连接用法实例
- Mysql 关于last_insert_id() 函数的用法
- Mysql inner join on的用法实例(必看)
- PHP中auto_prepend_file与auto_append_file用法实例分析
- PHP中file_get_contents高級用法实例
- 详解mysql的limit经典用法及优化实例
- 【转载】关于数据库里cascade的用法,在MySQL和Hibernate是不同的
- python中关于os.path.file的详细用法
- mysql的C++ API之mysql_use_result()的用法实例
- 关于微信等app请求服务器,file_get_contents()函数和CURL用法
- MySQL启动参数(一) —— init_file
- 关于MySQL里的found_row()和row_count()解释及用法(很好很强大!!!)
- 关于HTML5的FileReader这个高级货的用法
- php.init中关于文件上传的相关选项
- 关于数据库里cascade的用法,在MySQL和Hibernate是不同的