Search code examples
mysqldatabaselua

Luasql error: "LuaSQL: error connecting to database"


I am working on mysql. I am trying to acess the mysql database using the Luasql.I have installed Luasql using yum. Then i tried the following code:

mysql = require "luasql.mysql"

env = assert(mysql.mysql())

con = assert(env:connect ( "db_name", "username", "password", "localhost"))

for no, name in rows (con, "select * from t1") do

print (string.format ("%s", name))

end

While executing the above code i am getting the following error :

lua: check.lua:3: LuaSQL: error connecting to database. MySQL: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
stack traceback:
    [C]: in function 'assert'
    check.lua:3: in main chunk
    [C]: ?

How to overcome this error.Can anyone help me for the proper execution of the code? Thanks !!!


Solution

  • This code executes well..thank u both @ EgorSkriptunoff &jaylzhang for ur responses

    mysql = require "luasql.mysql"
    env = assert(mysql.mysql())
    con = assert(env:connect ( "db_name", "username", "password", "hostname", "3306"))
    local cur = con:execute("select * from t1")
    local row = cur:fetch({}, 'a')
    for k, v in pairs(row) do
        print(k, v)
    end