文章目录
- lua安装luasql扩展模块
- 安装lua
- 安装luarocks
- 安装luasql
- 安装luasql-mysql
- 安装luasql-postgres
- 使用lua-sql
- 安装lua日志模块
- 安装LuaLogging
- 结束语
lua安装luasql扩展模块
使用lua编程过程中不可避免的会用到mysql等数据库,进行数据库操作lua有luasql,各个数据库操作的接口统一,用起来比较方便,但是这货安装起来有时候遇到问题会比较麻烦,下面介绍如何安装luasql:
1.安装lua
2.安装luarocks
3.安装luasql
4.使用luasql
安装过程中还会使用到gcc、 wget、git等工具,自行安装,不喂饭了
yum install gcc wget git unzip zip
安装lua
- 有时候系统自带了lua,但是没有lua的头文件(比如lua.h),会导致luarocks安装不了,这时候仅需要下载和系统自带lua相同版本的lua代码就可以了,安装luarocks的时候指定lua代码路径即可找到需要的文件
- 如果系统没有安装lua,那么根据以下步骤操作
lua安装步骤:
- 我用的是lua 5.1.4版本做示例,大家根据各自要求选择即可
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz - 解压缩
tar -zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
chmod -R 755 ./ - 安装
make linux
make install
基本上就是安装三部曲,安装默认路径是/usr/local,如果需要改路径可以修改Makefile文件如下配置:
#Where to install. The installation starts in the src and doc directories,
#so take care if INSTALL_TOP is not an absolute path.
INSTALL_TOP= /usr/local
INSTALL_BIN= $(INSTALL_TOP)/bin
INSTALL_INC= $(INSTALL_TOP)/include
INSTALL_LIB= $(INSTALL_TOP)/lib
INSTALL_MAN= $(INSTALL_TOP)/man/man1
如果使用lua的时候需要加载动态库,那么make的时候必须选择make linux
加载动态库运行会出现这个报错:dynamic libraries not enabled; check your Lua installation
make linux的时候需要readline库,使用如下命令安装即可
yum install readline-devel
redhat 系列下这个软件包叫 readline-devel
ubuntu 下叫readline-dev 细分又分为libreadline5-dev 和 libreadline6-dev
安装luarocks
- Luarocks是一个Lua包管理器,基于Lua语言开发,提供一个命令行的方式来管理Lua包依赖、安装第三方Lua包等,社区比较流行的包管理器之一,另还有一个LuaDist,Luarocks的包数量比LuaDist多
luarocks安装步骤:
- 去这里选择一个版本的luarocks下载下来,我用的是2.1.0版本做示例
wget http://luarocks.org/releases/luarocks-2.1.0.tar.gz - 解压
tar -zxvf luarocks-2.1.0.tar.gz
cd luarocks-2.1.0 - 安装
./configure --rocks-tree=/usr/local/luarocks --prefix=/usr/local/luarocks --with-lua=/usr/local --with-lua-include=/root/lua-5.1.4/src
make bootstrap
安装完成后执行文件路径:/usr/local/luarocks/bin/luarocks
说明一下configure的参数
- rocks-tree配置的是所要安装的 Lua 扩展模块的安装目录(也就是luasql安装目录),luarocks会自动在这个目录后面加上/lib/lua/5.1/拼装成完整路径(/usr/local/luarocks/lib/lua/5.1/)
- prefix配置的是luarocks安装目录,默认安装到/usr/local下面
- with-lua配置的是lua执行文件所在的目录(貌似不需要写bin,我的在/usr/loca/bin下)
- with-lua-include配置的是lua源码路径(用来找头文件)
如果没有指明rocks-tree,默认路径是/usr/local(/usr/local/lib/lua/5.1/),可以查看/usr/local/etc/luarocks//config-5.1.lua 配置文件获取扩展库安装路径
[root@loacalost ~]#more /usr/local/luarocks/etc/luarocks/config-5.1.lua
rocks_trees = {
home…[[/.luarocks]],
[[/usr/local/luarocks]]
}
安装luasql
安装完上面的工具都终于可以按照luasql了,介绍2个比较常用的数据库
安装luasql-mysql
- 下载mysql开发包
wget https://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-devel-5.6.42-2.el7.x86_64.rpm - 安装mysql开发包
rpm -ivh mysql-community-devel-5.6.42-2.el7.x86_64.rpm - 安装luasql-mysql
luarocks install luasql-mysql MYSQL_DIR=/var/lib/mysql MYSQL_INCDIR=/usr/include/mysql
MYSQL_DIR配置的是myqsl的安装目录
MYSQL_INCDIR配置的是mysql头文件所在目录
遇到的问题
以下问题不一定会遇到,大家可以先安装,遇到问题再解决
- luarocks安装myqsl的时候会去/var/lib/mysql/lib找mysql的库,但是有时候安装mysql的时候不会放那,我的解决办法是建立一个软连接
ln -s /usr/lib64/mysql/ /var/lib/mysql/lib
安装luasql-postgres
- 安装好postgreSQL数据库,这个就不说了(毕竟我没安装过>.<)
- 安装luasql-postgres
/usr/local/bin/luarocks install luasql-postgres
遇到的问题
以下问题不一定会遇到,大家可以先安装,遇到问题再解决
- luarocks安装post-gre的时候会提示要指明POST-GRE数据库的安装路径,这时候需要指定PGSQL_DIR参数,PGSQL_INCDIR指定的是pgsql头文件路径,那么可以这么做:
先安装pgsql开发包:yum install postgresql-devel
再用luarocks安装:/usr/local/bin/luarocks install luasql-postgres PGSQL_DIR=/var/lib/pgsql/11/data PGSQL_INCDIR=/usr/include/pgsql/server
使用lua-sql
lua文件开头要引用上面安装luasql,比如:
package.cpath = package.cpath … ‘;/usr/local/luarocks/lib/lua/5.1/?.so’; --数据库路径
local luasql = require(“luasql.mysql”);
示例
package.cpath = package.cpath .. ';/usr/local/luarocks/lib/lua/5.1/?.so'; --数据库路径
local luasql = require("luasql.postgres");
--获取数据库句柄
local postgre_handle = luasql.postgres();
if (nil == postgre_handle) then
print("get postgreSQL handle failed\n");
return;
end
--连接数据库
local postgre_con = postgre_handle:connect("db", "user", "passwd", "ip", port);
if (nil == postgre_con) then
postgre_handle:close();
print("connect postgreSQL failed\n");
return;
end
--获取数据
local cur = postgre_con:execute([[select name, sex from student;]]);
if (nil ~= cur) then
local row = cur:fetch({},"a");
while row do
print(row["name"], row["sex"]);
row = cur:fetch({},"a");
end
end
--释放资源
postgre_con:close();
postgre_handle:close();
安装lua日志模块
由于项目上日志量比较大,最好能自动按天分割日志,又不想用系统命令分割,好不容易找到一个合适的lua日志库LuaLogging,这里是使用说明,比较简单
安装LuaLogging
/usr/local/luarocks/bin/luarocks install lualogging
安装后都是lua文件,使用起来也很方便
结束语
最近用lua做项目,安装扩展库感觉也不怎么方便,搞lua环境都得搞个一天,做项目的时候要用mysql,post-gre,redis,ini,xml,json等等库,但是有些是直接用lua文件,有些是通过luarocks安装的so文件,感觉不太统一,用的略不爽