MySQL
Use MySQLStore to persist Mastra application data, including messages, workflows, traces, scores, and other storage domains, in a MySQL database.
InstallationDirect link to Installation
Storage providers must be installed as separate packages:
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/mysql@latest
pnpm add @mastra/mysql@latest
yarn add @mastra/mysql@latest
bun add @mastra/mysql@latest
UsageDirect link to Usage
Add MySQLStore to your Mastra configuration with a MySQL connection string:
import { Mastra } from '@mastra/core'
import { MySQLStore } from '@mastra/mysql'
export const mastra = new Mastra({
storage: new MySQLStore({
id: 'mysql-storage',
connectionString: process.env.MYSQL_URL!,
}),
})
For example, MYSQL_URL can use the format mysql://user:password@host:3306/database.
Host-based configurationDirect link to Host-based configuration
You can provide individual connection fields instead of a connection string:
import { Mastra } from '@mastra/core'
import { MySQLStore } from '@mastra/mysql'
export const mastra = new Mastra({
storage: new MySQLStore({
id: 'mysql-storage',
host: process.env.MYSQL_HOST!,
port: Number(process.env.MYSQL_PORT ?? 3306),
user: process.env.MYSQL_USER!,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE!,
}),
})
OptionsDirect link to Options
Connection stringDirect link to Connection string
connectionString:
database is provided separately.database?:
max?:
ssl?:
Host-based connectionDirect link to Host-based connection
host:
port?:
user:
password?:
database:
ssl?:
max?:
waitForConnections?:
queueLimit?:
0 for no limit.Storage and index optionsDirect link to Storage and index options
id?:
disableInit?:
storage.init() manually before using the store.skipDefaultIndexes?:
indexes?:
InitializationDirect link to Initialization
When you register storage with Mastra, init() is called automatically to create the core schema:
const storage = new MySQLStore({
id: 'mysql-storage',
connectionString: process.env.MYSQL_URL!,
})
export const mastra = new Mastra({
storage, // init() is called automatically
})
When using storage directly without Mastra, call init() before accessing domain stores:
const storage = new MySQLStore({
id: 'mysql-storage',
connectionString: process.env.MYSQL_URL!,
})
await storage.init()
const memoryStore = await storage.getStore('memory')
ProductionDirect link to Production
Use a durable MySQL database that your Mastra deployment can reach. Store credentials in environment variables, and enable SSL when your database provider requires it.