luasql报错笔记

luasql 编译安装

查看mysql配置,注意 lmysqlclient 路径

[root@hmy luasql-master]# yum install mysql-devel gcc* -y
[root@hmy luasql-master]# mysql_config 
Usage: /usr/bin/mysql_config [OPTIONS]
Options:
        --cflags         [-I/usr/include/mysql]
        --include        [-I/usr/include/mysql]
        --libs           [-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl]
        --libs_r         [-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl]
        --plugindir      [/usr/lib64/mysql/plugin]
        --socket         [/var/lib/mysql/mysql.sock]
        --port           [0]
        --version        [5.5.56]
        --libmysqld-libs [-L/usr/lib64/mysql -lmysqld]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/include/mysql]
                pkglibdir     [/usr/lib64/mysql]
                plugindir     [/usr/lib64/mysql/plugin]

修改luasql-master安装包里的config

PREFIX ?= /usr/local/apisix/deps
LUA_SYS_VER ?= 5.1
LUA_LIBDIR ?= $(PREFIX)/lib/lua/$(LUA_SYS_VER)
LUA_DIR ?= $(PREFIX)/share/lua/$(LUA_SYS_VER)
LUA_INC ?= $(PREFIX)/include
...
...
DRIVER_LIBS_mysql ?= -L/usr/lib64/mysql -lmysqlclient -lz
DRIVER_INCS_mysql ?= -I/usr/include/mysql
...
...

安装luasql-mysql

[root@hmy luasql-master]# make mysql 
gcc -O2 -std=gnu99 -Wall -Wmissing-prototypes -Wmissing-declarations -pedantic -fPIC  -I/usr/local/apisix/deps/include -DLUASQL_VERSION_NUMBER='"2.6.0"'  -c src/luasql.c -o src/luasql.o
gcc -O2 -std=gnu99 -Wall -Wmissing-prototypes -Wmissing-declarations -pedantic -fPIC  -I/usr/local/apisix/deps/include -DLUASQL_VERSION_NUMBER='"2.6.0"'  src/ls_mysql.c -o src/mysql.so -shared src/luasql.o -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -lz
[root@hmy luasql-master]# make install
mkdir -p /usr/local/apisix/deps/lib/lua/5.1/luasql
cp src/*.so /usr/local/apisix/deps/lib/lua/5.1/luasql

安装完成后,执行apisix reload,报错内容如下:

2022/09/19 00:22:50 [error] 119935#119935: *11035303 [lua] plugin.lua:103: load_plugin(): failed to load plugin [sfimplat-auth] err: attempt to index a nil value, context: init_worker_by_lua*
2022/09/19 00:22:50 [error] 119933#119933: *11035302 [lua] plugin.lua:103: load_plugin(): failed to load plugin [sfimplat-auth] err: attempt to index a nil value, context: init_worker_by_lua*

一开始我以为是安装问题,反复检查重复安装很久才发现,是局部变量、全局变量问题
例如:

[root@hmy luasql]# lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require "luasql.mysql"
> local luasql = require("luasql.mysql")
> local env = assert(luasql.mysql())
stdin:1: attempt to index global 'luasql' (a nil value)
stack traceback:
        stdin:1: in main chunk
        [C]: ?

但如果这样写就没问题了

local luasql = require("luasql.mysql")

env = assert(luasql.mysql())
--连接数据库
conn = env:connect("数据库名","用户名","密码","IP地址",端口)

--设置数据库的编码格式
conn:execute"SET NAMES UTF8"

--执行数据库操作
cur = conn:execute("select * from tb_users where username='test'")

row = cur:fetch({}, "a")
core.log.error(core.json.encode(row))

或者 写在局部方法里,这样就可以用局部变量了

local function mysql_query(roles)
    local env = assert(luasql.mysql())
    local conn = env:connect("数据库名","用户名","密码","IP地址",端口)

    local per_dict = {}
    local cmd = string.format("select * from tb_roles where code like '%test%'")
    local cursor, err = conn:execute(cmd)
    local row = cursor:fetch({}, 'a')
    table.insert(per_dict, row.url)
    while row do
        -- reusing the table of results
        row = cursor:fetch(row, "a")
        if row then
            table.insert(per_dict, row.url)
        end
    end
    conn:close()  --关闭数据库连接
    env:close()   --关闭数据库环境
    return per_dict
end
``
posted @ 2022-09-19 00:43  醒日是归时  阅读(144)  评论(0编辑  收藏  举报