How to Configure LlamaIndex MySQL Audit Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a llamaindex mysql audit log by turning on database logging for a dedicated agent user. You also turn on callback handlers in your app to record generated queries. Teams often start by giving the agent a shared database login. If that role is too broad, the model can read private tables or run bad queries. Good logging and strict roles help teams track every action the agent takes. How a llamaindex mysql audit log works in practice LlamaIndex connects to relational databases throug

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 a llamaindex mysql audit log by turning on database logging for a dedicated agent user. You also turn on callback handlers in your app to record generated queries. Teams often start by giving the agent a shared database login. If that role is too broad, the model can read private tables or run bad queries. Good logging and strict roles help teams track every action the agent takes.

How a llamaindex mysql audit log 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 over your tables. If you do not specify which tables to use, the engine pulls all schema context. The framework then runs those generated SQL queries straight against your database.

Official docs notes that running arbitrary SQL queries is a security risk. The creators urge teams to use restricted roles, read-only databases, and sandboxing. To build a llamaindex mysql audit log, you first make a dedicated MySQL user. You grant this user select permissions on only the safe tables. Next, you turn on the native audit plugin or general query log inside MySQL. You filter the database log so it tracks queries from that specific agent user.

On the app side, you also set up built-in trace callbacks in LlamaIndex. These callbacks capture the user prompt and the SQL string the model builds. Your team can then ship both log streams to a central log store. There, you match app traces with database queries using timestamps.

Where native database logging falls short for AI agents

Native database roles and logs solve the first problem of basic access control. They decide who can log in and which tables that account can see. The MySQL log proves that a specific database account ran a query at a certain time. This gives security teams a basic record for past checks.

Still, native setup leaves a big gap during live query execution. All requests from the AI app share the exact same database user login. The MySQL log sees the app account, not the human user who asked the question. Matching a bad SQL query back to a specific person takes slow manual work.

Also, database logs only record the event after the query finishes running. They do not inspect what individual statements do in flight or block risky joins. A read-only role stops data changes, but it cannot stop a broad select query. If a table holds emails, phone numbers, card numbers, or national IDs, the query returns them. Native logging records that the query ran, but it does not mask sensitive fields in results.

Audit Log Integrity + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Best practices for safer text-to-SQL operations

Start by naming the exact tables the query engine can use in your code. Never let the engine pull the whole database schema by default. Keep sensitive user tables out of the schema context from the start.

Next, set strict resource limits on the MySQL role your agent uses. Cap the time a query can run and limit concurrent sessions. This stops a bad prompt from locking up your database server with heavy joins.

Finally, pair your database logs with real-time controls that check queries before they run. Data masking rewrites matched fields like emails or card numbers in results before the app reads them. This keeps raw sensitive text out of model prompts and app logs.

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 pass a table list, the query engine pulls all schema context. It does this so the language model knows what tables exist to answer questions. You should always pass an explicit list of safe tables to limit this scope.

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

No, a read-only role only stops the agent from changing or deleting data. The agent can still run heavy queries that slow down the server. It can also read sensitive columns like phone numbers or national IDs if they sit in allowed tables.

Native MySQL logs only show the shared database user that the app uses to log in. To see the human user, you must pass user identity metadata along with the session. Otherwise, you have to match app trace logs with database timestamps by hand.

The query runs. The emails stay hidden.