Send an http request in mysql trigger after insertion in a table(插入表后在mysql触发器中发送http请求)
问题描述
我在 PHP 中工作.我必须创建一个 mysql 触发器,它会在插入表后触发 http 请求.下面是代码.
I am working in PHP.I have to created a mysql trigger which fires an http request after insertion on table.Below is the code.
DELIMITER @@
CREATE TRIGGER Test_Trigger
AFTER INSERT ON insertsms
FOR EACH ROW
BEGIN
SET @tt_json = (SELECT json_object(id,addtime,title)
FROM insertsms WHERE id = NEW.id LIMIT 1);
SET @tt_resu = (SELECT http_put(CONCAT('--url localhost--')));
END;
@@
DELIMITER ;
但我收到类似的错误
消息:SQLSTATE[42000]:语法错误或访问冲突:1305 FUNCTION emg.json_object 不存在
Message: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION emg.json_object does not exist
消息:SQLSTATE[42000]:语法错误或访问冲突:1305 FUNCTION emg.http_put 不存在
Message: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION emg.http_put does not exist
如何消除这个错误?我无法下载包含这些功能的支持文件.我已经在 localhost 中进行了测试.还有其他方法可以满足我的要求吗?请任何人帮助我..
How to remove this error? I was not able to download the supporting files containing these functions.I have tested in localhost.Is there any other way to achieve my requirement? Please anyone help me..
推荐答案
虽然技术上可行,但出于以下几个原因,我强烈建议您不要走这条路:
Although it's technically possible I'd strongly discourage you from going this route for several reasons:
使用 UDF 本身就是一种安全风险.UDF 可供所有数据库用户使用 - 您不能为其授予 EXECUTE 权限.
Using UDFs is a security risk on its own. UDFs are available to all database users - you cannot grant EXECUTE privileges for them.
在触发器中执行任何非事务性操作完全是错误的.DML 语句所做的数据更改(在您的情况下是更新)可以并且将在现实世界中回滚.您将无法撤消 http 调用.
Doing any non-transactional operations in a trigger is simply wrong. Data changes made by DML statement (in your case it's an update) can and will be rolled back in a real world scenario. You won't be able to undo your http calls.
您延长了插入事务的时间,可能会导致其他更新/插入操作的锁定等待超时.
You're prolonging the time for insert transaction possibly causing lock-wait-timeouts for other update/insert operations.
强烈推荐阅读:
- 触发器的问题
现在很可能你需要的是一个工作队列,例如beanstalked.使用这种专门的中间件比用数据库组织队列要好得多.
Now most likely what you need is a work queue e.g. beanstalked. Using such specialized middleware is much better than organizing queues with database.
这篇关于插入表后在mysql触发器中发送http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:插入表后在mysql触发器中发送http请求
基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01