MySQL
Use MySQLStore to persist Mastra application data, including messages, workflows, traces, scores, and other storage domains, in a MySQL database.
InstallationInstallation的直接链接
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
UsageUsage的直接链接
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 configurationHost-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!,
}),
})
OptionsOptions的直接链接
Connection stringConnection string的直接链接
connectionString:
database is provided separately.database?:
max?:
ssl?:
Host-based connectionHost-based connection的直接链接
host:
port?:
user:
password?:
database:
ssl?:
max?:
waitForConnections?:
queueLimit?:
0 for no limit.Storage and index optionsStorage and index options的直接链接
id?:
disableInit?:
storage.init() manually before using the store.skipDefaultIndexes?:
indexes?:
InitializationInitialization的直接链接
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')
ProductionProduction的直接链接
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.