Search code examples
luamysql-connect

Lua programming ,error in establishing Database connection


After writing the code to connect to database,lua returns an error as

Error in establishing connection to MySQL ,Can't connect to /var/lib/mysql/mysql.sock

I'm unable to locate the /var/lib/mysql/mysql.sock file (which I haven't created )

My Sock is /tmp/mysql.sock

Below is my code to connect to MySQL database.

mysql = require "luasql.mysql"

local env  = mysql.mysql()
local conn = env:connect('test','root','')
print(env,conn)

status,errorString = conn:execute([[CREATE TABLE sample2 (id INTEGER, name TEXT);]])
print(status,errorString )

Solution

  • as seen there: Luasql error: "LuaSQL: error connecting to database", the problem is because luasql defaults to using a default socket path when connecting to localhost without a port. so just set the mysql host and port in env:connect or specify the socket path.

    EDIT: I believe your code should be:

    env:connect('test','root','','localhost',3306) -- for tcp
    -- or
    env:connect('test','root','',':/tmp/mysql.sock') -- for socket