How to Configure LlamaIndex MySQL Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up a llamaindex mysql sandbox, create a restricted database user and replica first. Then point the framework's SQLAlchemy wrapper to that isolated instance and name specific tables. Engineering teams often connect language models straight to primary databases using shared admin credentials. When permissions are too broad, a bad prompt can alter rows or pull private fields. How a llamaindex mysql sandbox works in practice LlamaIndex connects to relational databases through a light wrapp

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.

To set up a llamaindex mysql sandbox, create a restricted database user and replica first. Then point the framework's SQLAlchemy wrapper to that isolated instance and name specific tables. Engineering teams often connect language models straight to primary databases using shared admin credentials. When permissions are too broad, a bad prompt can alter rows or pull private fields.

How a llamaindex mysql sandbox works in practice

LlamaIndex connects to relational databases through a light wrapper built around SQLAlchemy. Its natural language query engine turns user questions into SQL queries over your data. The framework then runs those generated statements straight against your connected MySQL database. Official docs warns that running arbitrary generated SQL queries is a real security risk. The authors recommend using restricted roles, read-only databases, and sandboxing to lower this risk.

Building this sandbox starts inside your MySQL server before you write any app code. You create a separate database user that only holds select rights on safe tables. Many teams also place this user on a read replica or an isolated snapshot. This step keeps heavy analytical queries from slowing down the main production database. Next, you pass this restricted database login to the SQLAlchemy wrapper in your app.

You must also limit what schema context the query engine can read at runtime. When you set up the table query engine, you should pass an explicit table list. Without a table list, the query engine will pull all the schema context. Pulling every table wastes prompt tokens and exposes internal system tables to the model. Naming only the needed tables keeps the prompt small and focuses the agent.

Why native database roles are only half the fix

A native MySQL role and an isolated replica solve important problems for your team. They stop the AI agent from dropping tables, deleting rows, or changing production records. They also limit which tables the database user can read during a session. If a prompt injection tries to run a write command, MySQL blocks it fast.

Still, static database roles only check who logs in and which tables they touch. They cannot inspect what individual statements do in flight or filter specific column values. A read-only user with access to a customer table can still select every row by default. If that table holds emails, phone numbers, or card numbers, the model gets them. The query engine reads those raw results and sends them straight to the LLM.

MySQL Access Governance + AI Sandbox Environments: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Native roles also lack fine controls over how each query runs in real time. Every agent prompt uses the exact same database user in the app log. Your team cannot tell which person or prompt triggered a bad query in MySQL. A static sandbox cannot rewrite matched fields in results or require human approval for big queries.

Adding runtime controls to your database workflow

To close these gaps, teams pair their read-only MySQL setup with runtime query controls. First, keep your restricted MySQL user and explicit table list inside the LlamaIndex setup. Second, make sure that user cannot read internal system tables or sensitive auth tables. Third, look at how query results flow back to the language model during a run.

When a table holds emails, phone numbers, or national IDs, table grants are not enough. You need a way to inspect the query result before the agent reads it. Dynamic data masking rewrites matched fields in results while keeping the query format valid. This lets the agent count rows or group data without sending raw personal details outward. Combining strict MySQL grants with in-flight checks gives your team a safer text-to-SQL setup.

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

Why does LlamaIndex pull the whole MySQL schema by default?

If you do not name specific tables, the query engine pulls all schema context. Always pass a clear list of allowed tables when you set up the query engine.

Can a read-only MySQL user stop all text-to-SQL risks?

A read-only role stops data changes, table drops, and row deletes on the server. It does not stop the agent from reading sensitive columns like emails or card numbers.

Should I run text-to-SQL agents against a primary MySQL database?

No, LlamaIndex docs advise using read-only databases, restricted roles, and sandboxing for text-to-SQL apps. Running generated queries on a replica or sandbox protects your main database from bad queries.

The query runs. The emails stay hidden.