How to Configure Mastra MySQL Read Only Access

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up mastra mysql read only access by making a restricted database user. Then you pass those login keys to your agent tools. Many teams start by sharing a main admin database account during testing. This broad role lets an AI agent change or drop tables by mistake. A dedicated read role stops write commands at the database engine level. How mastra mysql read only access works Mastra is a framework for building AI agents and workflows. Agents use custom tools to fetch data from outside s

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up mastra mysql read only access by making a restricted database user. Then you pass those login keys to your agent tools. Many teams start by sharing a main admin database account during testing. This broad role lets an AI agent change or drop tables by mistake. A dedicated read role stops write commands at the database engine level.

How mastra mysql read only access works

Mastra is a framework for building AI agents and workflows. Agents use custom tools to fetch data from outside systems like MySQL. You give the agent a database connection string through local secret files. The agent reads this string and opens a session to the database.

Inside MySQL, an admin makes a new user account just for the agent. The admin grants only the select privilege on chosen schemas or tables. The admin leaves out insert, update, delete, and drop rights. When the agent tool runs a query, MySQL checks the user account rules.

If the agent sends a select query, the database returns the matching rows. If the agent tries to change a row, the database blocks the query. You can also bind the user account to a single network host. This stops outside machines from logging in with that same database key.

Why native database roles are only the first step

A native read role solves the problem of accidental data writes. Your AI agent cannot delete rows or change schema structures. It also separates agent traffic from your main backend app traffic. This makes it easier to revoke the agent key if a leak happens.

Still, database roles only check who logs in at the table level. They do not inspect what individual queries do in flight. A read role lets the agent pull every column in an allowed table by default. If a customer table holds emails or phone numbers, the agent gets them.

Card numbers and national IDs in those tables also flow into agent memory. The language model processes those raw values and may put them in logs. Masking rewrites matched fields in results before the agent processes the rows. Native MySQL roles do not rewrite sensitive text on the fly.

Auditor Read-Only Access + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Managing query load and shared agent keys

Read access also does not stop bad queries from hurting database speed. An AI agent might write a slow query with many table joins. It might scan millions of rows without a row limit. This heavy load can slow down the database for real users.

Database admins can set basic connection limits on the MySQL user account. Yet those limits apply to the whole shared account, not single agent runs. Every workflow in your app often shares the same static database key. When a bad query runs, the database log only shows that shared user.

Your team cannot tell which user prompt or workflow triggered the heavy query. Rotating that static key also takes manual work across your host servers. If you change the password in MySQL, you must update your app secrets. Until you restart the app, agent workflows will fail to log in.

Check MySQL queries and results on the wire

MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL. It runs next to MySQL, RDS, or Aurora and reads the MySQL wire protocol.

Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails. When a rule refuses a query, MySQL returns a native error packet with your message. On the response path, hoop.dev data masking rewrites string values in returned rows that match your rules. Every query and decision is logged per resource. See the MIT-licensed project on GitHub.

Frequently asked questions

Can a MySQL read user still read sensitive columns?

Yes, table read grants return all columns unless you set up column grants. Maintaining column grants across many tables takes heavy manual work. Masking rewrites matched fields like emails or card numbers in query results instead.

Should I connect my AI agent to a read replica?

Yes, pointing agent tools at a read replica protects your main database. Heavy select queries from the agent will not block live user writes. You still need a read role on the replica to block accidental changes.

How do I track which agent run sent a MySQL query?

Native MySQL logs only record the shared database user account name. To track single runs, you must attach caller identity to each session. This links every SQL statement back to the exact workflow or user.

The query runs. The emails stay hidden.