MySQL runs lib_mysqludf_sys implement calls to external programs and system commands

MySQL Query environment

Mysql query plug-in directory path

show variables like "plugin_dir";

Here Insert Picture Description
View the number of bits MySQL

show variables like '%version_%';

Here Insert Picture Description

lib_mysqludf_sys installation

https://github.com/mysqludf/lib_mysqludf_sys

A UDF library with functions to interact with the operating system. These functions allow you to interact with the execution environment in which MySQL runs.

Directory structure is as follows:
Here Insert Picture Description

Compile source code

Modify the Makefile, according to MySQL itself on the system's environment

Original Makefile

LIBDIR=/usr/lib

install:
    gcc -Wall -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o $(LIBDIR)/lib_mysqludf_sys.so

Modified Makefile

LIBDIR=/usr/lib/mysql/plugin

install:
    gcc -fPIC -Wall -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o $(LIBDIR)/lib_mysqludf_sys.so

Execute make, generate new lib_mysqludf_sys.so

Note: the source 32 comes to lib_mysqludf_sys.so

Is there lib_mysqludf_sys.so under / usr / lib64 / mysql / plugin directory, if mysql is docker environment, use the following command:

docker cp lib_mysqludf_sys.so a6e96adf79e2:/usr/lib/mysql/plugin/

Creating function

DROP FUNCTION IF EXISTS lib_mysqludf_sys_info;
DROP FUNCTION IF EXISTS sys_get;
DROP FUNCTION IF EXISTS sys_set;
DROP FUNCTION IF EXISTS sys_exec;
DROP FUNCTION IF EXISTS sys_eval;

CREATE FUNCTION lib_mysqludf_sys_info RETURNS string SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_get RETURNS string SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_set RETURNS int SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_exec RETURNS int SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_eval RETURNS string SONAME 'lib_mysqludf_sys.so';

Here Insert Picture Description

error

ERROR 1126 (HY000): Can’t open shared library ‘lib_mysqludf_sys.so’ (errno: 2 /usr/lib/mysql/plugin/lib_mysqludf_sys.so: wrong ELF class: ELFCLASS32)

View comes lib_mysqludf_sys.so file

file lib_mysqludf_sys.so 

It is compiled in 32-bit systems lib_mysqludf_sys.so, and our MySQL median X86_64
Here Insert Picture Description
view make file generated lib_mysqludf_sys.so

file lib_mysqludf_sys.so 

Here Insert Picture Description

Execution lib_mysqludf_sys

SELECT sys_eval('pwd'); 

Here Insert Picture Description

select * from mysql.func;

Here Insert Picture Description

Published 72 original articles · won praise 66 · Views 150,000 +

Guess you like

Origin blog.csdn.net/miaodichiyou/article/details/102566958