mysql数据库起动自动执行某个命令(init-file使用)

来源:互联网 发布:淘宝打折软件开发接口 编辑:程序博客网 时间:2024/06/08 13:05
关于MySQL的init-file选项的用法实例

    init-file 是在MySQL启动的时候加载的脚本。

    有两个要注意的。

    1. 确保你的mysqld 编译的时候没有加 --disable-grant-options 开关。

    2. 确保init-file指定的脚本每行一个具体的语句。

    使用方法如下,直接添加到配置文件,比如my.cnf.

    添加:

    [server] 或者 [mysqld] 或者 [mysqld_safe]

    init-file="Your file location"

    # The following options will be passed to all MySQL clients

    [server]

    init-file=/usr/local/mysql567/init.file

    [root@ambow-school-system-hylm bin]# ll /usr/local/mysql567/init.file

    -rw-rw---- 1 mysql mysql 92 Dec 15:43 /usr/local/mysql567/init.file

    [root@hylm bin]cat /usr/local/mysql567/init.file

    use test;

    insert chen select substr(passid,1,7) a, max(passid) as aa from user group by a;

    [root@hylm bin]#

    mysql> show create table chen\G;

    *************************** 1. row ***************************

     Table: chen

    Create Table: CREATE TABLE `chen` (

     `pkey` char(8) default NULL,

     `value` char(25) default NULL

    ) ENGINE=MEMORY DEFAULT CHARSET=utf8

    1 row in set (0.00 sec)

    ERROR:

    No query specified

    mysql> show create table USER\G;

    *************************** 1. row ***************************

     Table: USER

    Create Table: CREATE TABLE `user` (

     `passid` char(25) default NULL

    ) ENGINE=MyISAM DEFAULT CHARSET=utf8

    1 row in set (0.00 sec)

    ERROR:

    No query specified

    mysql> select * from user;

    +-----------------+

    | passid |

    +-----------------+

    | apt1212100000 |

    | apt1212100002 |

    | apt1212100003 |

    | cisco1212100001 |

    | cisco1212100002 |

    | cisco1212100003 |

    | tmp1212100001 |

    | tmp1212100002 |

    | tmp1212100003 |

    +-----------------+

    9 rows in set (0.00 sec)

    mysql>

    重起后就可以看到如下:

    mysql> select * from chen;

    +---------+-----------------+

    | pkey | value |

    +---------+-----------------+

    | apt1212 | apt1212100003 |

    | cisco12 | cisco1212100003 |

    | tmp1212 | tmp1212100003 |

    +---------+-----------------+

    

    


原创粉丝点击