How to Configure LlamaIndex MySQL Least Privilege

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up llamaindex mysql least privilege, create a read-only database user for your agent. Then, pass a fixed list of safe tables into the query engine. Many teams start by giving their AI app a root or admin database key. This broad role lets the language model read every table or change live data. When a user prompt tricks the model, broad permissions turn a bad query into an outage. How llamaindex mysql least privilege works in practice LlamaIndex connects to relational databases through

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 llamaindex mysql least privilege, create a read-only database user for your agent. Then, pass a fixed list of safe tables into the query engine. Many teams start by giving their AI app a root or admin database key. This broad role lets the language model read every table or change live data. When a user prompt tricks the model, broad permissions turn a bad query into an outage.

How llamaindex mysql least privilege works in practice

LlamaIndex connects to relational databases through a light wrapper around SQLAlchemy. Its natural language query engine turns user questions into SQL statements and runs them right away. Because the framework runs generated SQL directly, the official docs flag this as a security risk. They advise teams to use restricted roles, read-only databases, and sandboxing.

Setting up llamaindex mysql least privilege requires changes in both the database and the app. First, your database admin creates a dedicated MySQL account just for the AI agent. This account should only get select permissions on the specific tables the agent needs. Do not grant permissions to drop tables, change schemas, or write new rows. If you have a read replica, point the agent there instead of the primary instance.

Next, you restrict what the framework sees inside the Python app. When you set up the query engine, you can name the exact tables to include. If you skip this list, the query engine pulls all schema context from the database. Pulling every table wastes prompt tokens and shows sensitive schema details to the model. Naming only the needed tables keeps the prompt small and limits where the model looks.

Why native roles leave a runtime gap

Database grants and table lists are necessary first steps for any text-to-SQL app. They stop an agent from deleting rows or reading restricted system tables. Still, native MySQL permissions only check who logs in and which tables they touch. They do not inspect what individual queries do in flight.

Least Privilege Principle + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

A read-only MySQL role still allows queries that cause serious trouble. For example, a permitted customer table often holds both public order totals and private data. If the MySQL user can read that table, the model can pull every column by default. Dynamic data masking can help here by rewriting matched fields like emails, phone numbers, or card numbers in query results.

Native roles also share a single database identity across every user of your app. In the MySQL logs, every generated query comes from the same service account. You cannot tell which human user or agent task triggered a specific database read. Also, a read-only role cannot stop a bad join that locks up CPU resources. Securing agent queries requires controls that check each statement and log the real caller.

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?

When you do not pass a table list, the query engine reads the full schema. It does this so the language model knows what tables exist to answer questions. You should always specify allowed tables to save tokens and hide private schemas.

Is a read-only MySQL user enough to secure text-to-SQL agents?

A read-only user stops data changes and table drops, which is a vital start. Still, it cannot mask sensitive columns like card numbers or national IDs in results. It also cannot tie shared service account queries back to individual app users.

How do we stop an AI agent from running slow MySQL queries?

Start by pointing the agent to a read replica so primary traffic stays safe. You can also set statement timeouts and resource limits on the MySQL user account. This kills runaway joins before they exhaust database memory and CPU.

The query runs. The emails stay hidden.