MySQL UDF sys_exec() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27587333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:14:01  来源:igfitidea点击:

MySQL UDF sys_exec() doesn't work

mysqlstored-proceduresuser-defined-functions

提问by tea-addict

I have a trigger which calls a stored procedure with parameters which calls SET result = sys_exec(cmd);. But it gives the error "function sys_exec does not exist".

我有一个触发器,它调用一个带有参数的存储过程,它调用SET result = sys_exec(cmd);. 但它给出了错误“函数 sys_exec 不存在”。

I don't know what to do, on Tuesday I have presentation and because of this code line my project won't work. The codes which I try to work.

我不知道该怎么办,周二我有演示文稿,但由于此代码行,我的项目将无法运行。我尝试工作的代码。

DELIMITER $$
CREATE PROCEDURE push_message
(p1   int,
 p2   int,
 p3 varchar(20))
BEGIN
 DECLARE cmd CHAR(255);
 DECLARE result CHAR(255);
 SET cmd = CONCAT('curl https://pubsub.pubnub.com/publish/demo/demo/0/mysql_triggers/0/%22',p1, ',' ,p2, ',' ,p3,'%22');
 SET result = sys_exec(cmd);
END$$; 


CREATE TRIGGER push_message_trigger AFTER INSERT ON your_table_name_here
FOR EACH ROW
CALL push_message(NEW.id, NEW.num, NEW.name);

回答by wchiquito

Try:

尝试:

mysql> SELECT VERSION();
+-----------------+
| VERSION()       |
+-----------------+
| 5.5.35-1ubuntu1 |
+-----------------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'plugin_dir';
+---------------+------------------------+
| Variable_name | Value                  |
+---------------+------------------------+
| plugin_dir    | /usr/lib/mysql/plugin/ | -- copy 'lib_mysqludf_sys.so' here
+---------------+------------------------+
1 row in set (0.01 sec)

mysql> DROP FUNCTION IF EXISTS lib_mysqludf_sys_info;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP FUNCTION IF EXISTS sys_get;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP FUNCTION IF EXISTS sys_set;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP FUNCTION IF EXISTS sys_exec;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP FUNCTION IF EXISTS sys_eval;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE FUNCTION lib_mysqludf_sys_info RETURNS string SONAME 'lib_mysqludf_sys.so';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE FUNCTION sys_get RETURNS string SONAME 'lib_mysqludf_sys.so';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE FUNCTION sys_set RETURNS int SONAME 'lib_mysqludf_sys.so';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE FUNCTION sys_exec RETURNS int SONAME 'lib_mysqludf_sys.so';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE FUNCTION sys_eval RETURNS string SONAME 'lib_mysqludf_sys.so';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT sys_exec('curl http://stackoverflow.com/');
+--------------------------------------------+
| sys_exec('curl http://stackoverflow.com/') |
+--------------------------------------------+
|                                          0 |
+--------------------------------------------+
1 row in set (0.12 sec)

For more details, visit: 24.3.2.5 Compiling and Installing User-Defined Functions.

更多详细信息,请访问:24.3.2.5 编译安装用户自定义函数

回答by Aslam Shaik

Try Once.

尝试一次。

Steps to be followed:

应遵循的步骤:

  • Get the files from https://github.com/mysqludf/lib_mysqludf_sys

  • Go to the folder path in terminal.

  • Execute:

    gcc -DMYSQL_DYNAMIC_PLUGIN -fPIC -Wall -m64 -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o lib_mysqludf_sys.so
    

    NOTE: If 32-bit OS replace -m64with -m32in the above command.

  • Execute the following in mysql shell:

    SHOW VARIABLES LIKE 'plugin_dir';
    

    This is an example of successful output:

    +---------------+------------------------+
    | Variable_name | Value                  |
    +---------------+------------------------+
    | plugin_dir    | /usr/lib/mysql/plugin/ |
    +---------------+------------------------+
    
  • Copy the lib_mysqludf_sys.sofile to the above path.

  • Execute the following in mysql shell

    CREATE FUNCTION sys_exec RETURNS INT SONAME 'lib_mysqludf_sys.so';
    
  • https://github.com/mysqludf/lib_mysqludf_sys获取文件

  • 转到终端中的文件夹路径。

  • 执行:

    gcc -DMYSQL_DYNAMIC_PLUGIN -fPIC -Wall -m64 -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o lib_mysqludf_sys.so
    

    注意:如果32位操作系统替换-m64-m32在上面的命令。

  • 在 mysql shell 中执行以下命令:

    SHOW VARIABLES LIKE 'plugin_dir';
    

    这是一个成功输出的例子:

    +---------------+------------------------+
    | Variable_name | Value                  |
    +---------------+------------------------+
    | plugin_dir    | /usr/lib/mysql/plugin/ |
    +---------------+------------------------+
    
  • lib_mysqludf_sys.so文件复制到上述路径。

  • 在mysql shell中执行以下命令

    CREATE FUNCTION sys_exec RETURNS INT SONAME 'lib_mysqludf_sys.so';