How to Configure LlamaIndex MySQL Guardrails

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up llamaindex mysql guardrails by restricting database roles and naming allowed tables. Then you pair those limits with a read-only database replica or a sandbox. Teams often start by handing an agent a standard database user login. When that login has broad permissions, one bad prompt can drop or change rows. Even read rights on the full schema let the model read sensitive user records. How native llamaindex mysql guardrails work LlamaIndex connects to relational databases through a

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 llamaindex mysql guardrails by restricting database roles and naming allowed tables. Then you pair those limits with a read-only database replica or a sandbox. Teams often start by handing an agent a standard database user login. When that login has broad permissions, one bad prompt can drop or change rows. Even read rights on the full schema let the model read sensitive user records.

How native llamaindex mysql guardrails work

LlamaIndex connects to relational databases through a light wrapper around SQLAlchemy. Its natural language table query engine turns user questions into SQL statements. It then runs those generated statements straight against your MySQL database.

The first native control sits inside the query engine setup. You pass a specific list of table names to the engine. When you name those tables, the engine only reads their schema context. If you leave that list blank, the engine pulls the whole schema context. Pulling the whole schema wastes prompt tokens and exposes hidden tables to the model.

The second native control sits inside MySQL itself. Official LlamaIndex docs notes that running arbitrary SQL queries is a security risk. The docs recommends restricted roles, read-only databases, and sandboxing as basic precautions. In MySQL, teams build a dedicated user role for the AI app. They grant that role select permissions on the safe tables only. They also point the SQLAlchemy wrapper at a read-only replica instead of the primary database. This setup keeps a bad query from deleting live production data.

What native controls solve and what they miss

Native database roles and table lists solve two big problems. First, they stop write, update, and delete statements at the database level. Second, they stop the query engine from loading every table into the prompt context. These steps give your team a solid starting point for basic safety.

Still, database logins only check who logs in and which tables they can read. They do not inspect what individual SQL statements do in flight. A read-only user can still run a massive join that locks up MySQL CPU. That same user can also select every row from a permitted customer table by default.

Table permissions are also all or nothing for the columns you allow. Often an agent needs a customer table to count orders by city or state. If that table also holds emails, phone numbers, or card numbers, the model gets them. Native MySQL roles cannot mask those fields in the query results on the fly. You would have to build and maintain separate views for every single agent task.

MySQL Access Governance + AI Guardrails: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Finally, shared app credentials make auditing hard for security teams. Every query from the LlamaIndex app shows up in MySQL under the same role. When a weird query runs, logs do not show which user prompt caused it. Teams also lack a way to pause one risky query for human review.

Steps to harden your text-to-SQL workflow

Start by listing the exact business questions your LlamaIndex agent must answer. Map those questions to the smallest set of MySQL tables possible. Pass that exact table list into your natural language query engine setup. Never let the query engine scan the full database schema by default.

Next, create a dedicated MySQL user just for this specific AI agent. Grant that user read access only on the tables you listed earlier. Route the agent traffic to a read replica with strict query timeouts. Then evaluate how you will mask sensitive columns and log queries per user.

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

What happens if I do not specify tables in the LlamaIndex SQL query engine?

If you do not list specific tables, the query engine pulls all schema context. This exposes every table in your MySQL database to the language model prompt.

Does LlamaIndex block destructive SQL statements by default?

No, LlamaIndex runs the generated SQL queries through its SQLAlchemy wrapper. Its docs warn that running arbitrary SQL is a risk and advise using read-only roles.

Can a read-only MySQL role stop sensitive data leaks in text-to-SQL?

A read-only role stops data changes, but it still returns raw rows from allowed tables. To hide emails, phone numbers, or card numbers, you need result masking.

The query runs. The emails stay hidden.