简体   繁体   English

MySQL在启动时截断表

[英]mysql truncate table on boot

How to truncate MySQL table on boot,
but want make sure script execute after mysqld started

mysql -h localhost -u root -pPASSWORD DBNAME -e "truncate table tables"

thank you. 谢谢。

Create a script startup.sql, store it in your data directory, make it readable to the server. 创建一个脚本startup.sql,将其存储在您的数据目录中,使其对服务器可读。 The startup.sql script contains the statements you need, for example "TRUNCATE TABLE tables;". startup.sql脚本包含您需要的语句,例如“ TRUNCATE TABLE表;”。

Add the following line to the [mysqld] section of your my.cnf file: 将以下行添加到my.cnf文件的[mysqld]部分:

init-file=startup.sql 初始化文件= startup.sql

Stop and restart the server to test. 停止并重新启动服务器以进行测试。

The relevant documentation can be found here: http://dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_init-file 相关文档可以在这里找到: http : //dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_init-file

depends on why you need it truncating... 取决于您为什么需要截断...

perhaps consider using a memory table which would be 'truncated' on server restart anyway as it is only held in memory... 也许考虑使用一个在服务器重启时将被“截断”的内存表,因为它仅保存在内存中...

otherwise yoou coud add a line to truncate each table you need truncating to the set-up script and ensure that the script is called on startup. 否则,您会添加一行以截断需要截断的每个表到设置脚本,并确保在启动时调用该脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM