用mysql执行shell命令,从MySQL执行Shell命令

I know what I'm looking for is probably a security hole, but since I managed to do it in Oracle and SQL Server, I'll give it a shot:

I'm looking for a way to execute a shell command from a SQL script on MySQL. It is possible to create and use a new stored procedure if necessary.

Notice: I'm not looking for the SYSTEM command which the mysql command line tool offers. Instead I'm looking for something like this:

BEGIN IF

COND1...

EXEC_OS cmd1; ELSE

EXEC_OS cmd2; END;

where EXEC_OS is the method to invocate my code.

解决方案

You might want to consider writing your scripts in a more featureful scripting language, like Perl, Python, PHP, or Ruby. All of these languages have libraries to run SQL queries.

There is no built-in method in the stored procedure language for running shell commands. This is considered a bad idea, not only because it's a security hole, but because any effects of shell commands do not obey transaction isolation or rollback, as do the effects of any SQL operations you do in the stored procedure:

START TRANSACTION;

CALL MyProcedure();

ROLLBACK;

If MyProcedure did anything like create or edit a file, or send an email, etc., those operations would not roll back.

I would recommend doing your SQL work in the stored procedure, and do other work in the application that calls the stored procedure.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值