What it does
MySQL and MariaDB adapters store permissions and roles in your database using the mysql2 driver. They work the same way, so you can use either one with permly.
When to use it
Use MySQL or MariaDB when:
- Your app already uses MySQL or MariaDB
- You want permissions stored in your main database
- You're deploying to a platform with MySQL support (AWS RDS, DigitalOcean, etc.)
How it works
- Install the driver: permly uses
mysql2/promisefor async queries - Create a pool: connect to your database with connection pooling
- Create adapter: pass the pool to
mysqlAdapter() - Create tables: run
npx permly migrateto create the fiveperm_*tables - Use permly: all queries happen through the adapter automatically
Names (role names, permission names, user IDs) are stored as case-sensitive strings using the utf8mb4_bin collation. A typo in a name is a different entry.
Transactions and locks keep permissions safe under concurrent changes. The adapter uses named locks (a MySQL feature) to ensure only one process can update a user's roles at a time, preventing race conditions.
Examples
// @typecheck-only: needs mysql2 driver and real MySQL database
import mysql from "mysql2/promise";
import { createPermissions } from "permly";
import { mysqlAdapter } from "permly/mysql";
const pool = mysql.createPool(process.env.DATABASE_URL || "");
const perms = createPermissions({
adapter: mysqlAdapter(pool),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
await perms.sync();
await perms.user(1).assignRole("editor");
const allowed = await perms.user(1).can("posts.edit");
console.log(allowed); // true
// @typecheck-only: needs mysql2 driver and real MySQL database
import mysql from "mysql2/promise";
import { createPermissions } from "permly";
import { mysqlAdapter } from "permly/mysql";
const url = process.env.DATABASE_URL;
if (!url) throw new Error("Set DATABASE_URL");
const pool = mysql.createPool(url);
const perms = createPermissions({
adapter: mysqlAdapter(pool),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
await perms.sync();
const allowed: boolean = await perms.user(1).can("posts.edit");
Connection options
You can customize the table prefix and other behavior. Pass an options object as the second argument:
// @typecheck-only: needs mysql2 driver and real MySQL connection pool
import mysql from "mysql2/promise";
import { mysqlAdapter } from "permly/mysql";
const pool = mysql.createPool(process.env.DATABASE_URL || "");
const adapter = mysqlAdapter(pool, { prefix: "auth_" });
The default prefix is perm_. Change it if you already have tables with those names.
Migration and setup
Create the database schema with:
DATABASE_URL=mysql://user:password@localhost/mydb npx permly migrate
Or use the generated SQL file directly with your migration tool. The migrate command is idempotent: running it multiple times is safe and won't drop or recreate existing tables.
Galera cluster limitation
Galera clusters (multi-master MySQL replication) don't support named locks, which permly uses to safely sync roles under concurrency. Everything else works fine—just concurrent calls to syncRoles() on the same user may rarely timeout. Most apps are unaffected.
Common mistakes
Forgetting to run migrate — The tables won't exist until you run npx permly migrate. Do this once in your deployment pipeline.
Using callback API — mysql2 has two APIs: callback and promise. Use the promise version: import mysql from "mysql2/promise" and mysql.createPool().
Hardcoding the database URL — Use environment variables or your deployment platform's config system, never hardcode credentials.
Assuming collation — Names are case-sensitive. "admin" and "Admin" are different roles. This is correct behavior (permissions need exact names).
Related pages
- Installation — How to set up permly
- Quick Start — Your first permissions check
- PostgreSQL — Use Postgres instead
- MongoDB — Use MongoDB instead