亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關于MySQL的init-file選項的用法實例

標簽:
MySQL


这段时间失业中,刚好在家有大把的时间学习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)

看到效果了吧。

©著作权归作者所有:来自51CTO博客作者david_yeung的原创作品,如需转载,请注明出处,否则将追究法律责任

MySQL数据库休闲MySQL性能优化


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消