How to Configure LlamaIndex MySQL Read Only Access

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up llamaindex mysql read only access by pairing a restricted MySQL user with explicit table limits. The framework then runs generated SQL statements using only those limited database permissions. Many engineering teams start by connecting their AI agents with standard app credentials. Those default roles often allow data changes or full schema reads across the database. When a language model turns natural language into SQL, broad permissions create real risks. A bad prompt or model mista

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 read only access by pairing a restricted MySQL user with explicit table limits. The framework then runs generated SQL statements using only those limited database permissions. Many engineering teams start by connecting their AI agents with standard app credentials. Those default roles often allow data changes or full schema reads across the database. When a language model turns natural language into SQL, broad permissions create real risks. A bad prompt or model mistake can alter rows or expose private tables.

How llamaindex mysql read only access works in practice

LlamaIndex connects to relational databases through its SQL database wrapper around SQLAlchemy. Its natural language table query engine translates user questions into executable SQL statements. The official docs notes that running arbitrary SQL queries is a security risk. The authors advise teams to use restricted roles, read-only databases, and sandboxing.

Setting up this protection starts inside your MySQL server. You create a dedicated database user for the agent. You grant this user select privileges only on the specific tables it needs. You leave out insert, update, delete, and drop permissions.

Next, you pass those read-only credentials to the SQLAlchemy engine inside your app. You also define an explicit table list when you set up the query engine. If you skip this table list, the query engine pulls all schema context. Pulling every table wastes prompt tokens and shows the model restricted database structures. Naming the exact tables keeps the prompt small and limits what the model sees.

What native database roles solve and what they miss

A read-only MySQL role stops the most obvious failure modes. The database engine blocks any attempt to delete rows, drop tables, or change data. Even if a user injects a destructive prompt, MySQL rejects the write command. Using a read replica adds another layer of safety for production workloads. If the agent generates a slow join, the heavy read load hits the replica.

Still, native database grants only check who logs in at the table level. They do not inspect what individual statements do in flight or what rows return. A read-only user can still read every column inside an allowed customer table by default. If that table holds emails, phone numbers, card numbers, or national IDs, the query returns them. Those sensitive values then flow straight into the language model context and third-party logs.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Static credentials also create operational work for engineering teams. The app needs a database password stored in its local settings. When multiple agents or developers share that credential, database logs show one generic user. You cannot tie a specific SQL statement back to the person or service that triggered it. You also cannot block an expensive full-table scan before MySQL runs it.

Reducing schema exposure and query risk

Teams can tighten their setup further by creating dedicated MySQL views for the agent. A view lets you expose only safe columns from a wider table. You then grant the read-only role access to the view instead of the base table. You also point the LlamaIndex table list solely at that view.

This approach hides sensitive columns, though it adds schema maintenance over time. Every time the app needs a new field, an engineer must update the SQL view. For larger systems, teams look for ways to inspect queries and mask fields in flight.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev runs beside the database as a sidecar for your MySQL. It keeps your login setup and drivers as they are while checking queries on the wire.

With hoop.dev MySQL support, guardrails refuse bad queries before they run and return a readable MySQL error. Data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

Why does LlamaIndex pull the whole MySQL schema by default?

When you set up the table query engine without a table list, it reads every table. It does this so the language model knows the database structure. Passing a specific list of table names stops it from loading the entire schema.

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

No, a read-only role only stops data changes and schema edits. The language model can still run slow queries that lock up CPU resources. It can also read sensitive columns like emails or card numbers from allowed tables.

How does data masking help a text-to-SQL agent?

Data masking rewrites matched fields like phone numbers or national IDs in query results. The query still runs, and the agent gets the structure it needs to answer questions. Raw sensitive values do not enter the model prompt or external logs.

The query runs. The emails stay hidden.