iTranslated by AI
Self-hosting libsql-server
I investigated self-hosting libsql-server because I wanted to use libSQL for SQLite file replication purposes, similar to Litestream.
What is libsql-server?
- A server implementation of the libSQL project, which is a fork of SQLite.
- The foundational technology of Turso, which has been open-sourced.
- It allows you to create client/server-style SQLite databases.
Server Startup Command
A Docker image is provided.
docker run --name some-sqld -p 8080:8080 -ti \
-e SQLD_NODE=primary \
--platform linux/amd64 \
ghcr.io/tursodatabase/libsql-server:latest
_ _
___ __ _| | __| |
/ __|/ _` | |/ _` |
\__ \ (_| | | (_| |
|___/\__, |_|\__,_|
|_|
Welcome to sqld!
version: 0.24.30
commit SHA: 58b016ab72eea7fc0118009fb03195449f202edc
build date: 2024-12-17
This software is in BETA version.
If you encounter any bug, please open an issue at https://github.com/tursodatabase/libsql/issues
config:
- mode: primary (0.0.0.0:5001)
- database path: iku.db
- extensions path: <disabled>
- listening for HTTP requests on: 0.0.0.0:8080
- grpc_tls: no
2024-12-18T10:28:43.642626Z INFO sqld: listening for incoming user HTTP connection on 0.0.0.0:8080
2024-12-18T10:28:43.644701Z INFO sqld: listening for incoming gRPC connection on 0.0.0.0:5001
2024-12-18T10:28:43.703993Z INFO restore: libsql_server::namespace::meta_store: restoring meta store
2024-12-18T10:28:43.705025Z INFO restore: libsql_server::namespace::meta_store: meta store restore completed
2024-12-18T10:28:43.708396Z INFO libsql_server: Server sending heartbeat to URL <not supplied> every 30s
2024-12-18T10:28:43.725623Z INFO libsql_server::rpc: serving internal rpc server without tls
You can connect using a libSQL-compatible client. A list of GUI tools is available below:
Client Connection
You can connect from Node.js using libsql-client, which is available on npm.
Connect by specifying url: "http://localhost:8080".
The following is a sample code that retrieves a random name and saves it to the database upon execution.
import { createClient } from "@libsql/client";
const client = createClient({
url: "http://localhost:8080",
});
async function getRandomName() {
const response = await fetch('https://randomuser.me/api/');
const data = await response.json();
return `${data.results[0].name.first} ${data.results[0].name.last}`;
}
await client.batch(
[
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)",
{
sql: "INSERT INTO users(name) VALUES (?)",
args: [await getRandomName()],
},
],
"write",
);
const users = await client.execute("SELECT * FROM users");
console.log(users);
Read Replica
You can create a read replica by setting the url to a local file path and specifying a syncUrl in the client creation options.
const client = createClient({
- url: "http://localhost:8080",
+ url: "file:local.db",
+ syncUrl: "http://localhost:8080/",
});
A local.db file will be created, and the database contents will be synchronized via WAL.
It can be used as a read-only database.
Summary
By using libsql-server, you can operate an SQLite database in a client/server model.
Next steps:
- Taking database backups with S3 bottomless replication
- Creating applications using libSQL-compatible ORMs (Prisma, Drizzle)
- Setting up authentication if you expose the endpoint
Discussion