简单实现一个需求,每5分钟往特定表中插入2条数据。经过分析还是采用crontab方式靠谱,另外执行php脚本的方式,不用担心链接超时等问题。
1、准备工作,创建1个数据表“person”,
?
| 1 2 3 4 5 6 7 8 9 10 11 | create table `person` ( `firstname` varchar (100) not null , `lastname` varchar (100) default null , `age` varchar (100) default null , primary key (`firstname`) ) engine=innodb default charset=latin1; |
2、创建1个php脚本“test.php”,实现往数据库表中插入的操作,这个脚本可以放到任何位置。ps:建议不放到wwwroot根目录下,因为这样的话,用户可以通过url就可以进行访问了,这样的安全不高,但是有一些方便之处,我们使用自带的一些php框架,例如ci、tp等,也可以使用自己封装的一些业务通用类!综合考虑实际情况进行决定。
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php $con = mysql_connect( "localhost" , "root" , "idodopass01!" ); if (mysql_select_db( "test" , $con )) { mysql_query( "insert into person (firstname, lastname, age) values ('peter','griffin','35')" ); mysql_query( "insert into person (firstname, lastname, age) values ('glenn','quagmire','33')" ); echo "chenggong" ; } mysql_close( $con ); echo "string" ; ?> |
3、创建crontab任务
?
| 1 2 | crontab -e * /5 * * * * /usr/local/php/bin/php /home/wwwroot/default/test .php |
关于crontab命令参考链接
4、其他
查看运行日志的路径:/var/log
搬瓦工中文网





