MySQL Read-Only User for an AI Agent: Setup and the Gaps to Close
A dedicated SELECT-only MySQL account is the right start for an AI agent, and it is not enough: MySQL has no native row-level security, and anything you grant can still be read.
Claix · November 2026. Related guides: read-only Postgres role for AI agents, how to let an agent query Postgres safely, and safe Supabase access. Product: Claix Retrieve.
Why the agent needs its own account
An AI agent that writes SQL will run whatever the connection allows. If it shares the application's credentials, it inherits the application's write access. A dedicated account lets you give the agent only what it needs, revoke it without touching anything else, and see in your logs exactly what it did.
In MySQL an account is a user name plus a host pattern, such as 'claix_agent'@'10.0.%', and privileges can be granted at four scopes: global, schema, table and column. Use the narrowest scope that works.
Step by step: create the account
The examples below use MySQL 8 syntax and assume a shop database with customers and orders tables.
1. Create the account with a restricted host, TLS and resource limits
CREATE USER 'claix_agent'@'10.0.%' IDENTIFIED BY 'use-a-long-random-secret'
REQUIRE SSL
WITH MAX_USER_CONNECTIONS 5 MAX_QUERIES_PER_HOUR 20000;2. Grant SELECT only where needed
Either grant specific columns:
GRANT SELECT (id, full_name, plan, created_at) ON shop.customers TO 'claix_agent'@'10.0.%';
GRANT SELECT ON shop.orders TO 'claix_agent'@'10.0.%';or, usually friendlier for an agent, expose views (next section) and grant only those.
3. Check what you granted
SHOW GRANTS FOR 'claix_agent'@'10.0.%';You should see USAGE on *.* and nothing else except the objects you chose. If a *.* grant appears, remove it.
Use views to control columns and rows
A view lets you decide exactly what the agent can see, and it avoids a common agent error: with column-level grants, SELECT * fails because it includes columns the account cannot read.
CREATE VIEW shop.agent_customers AS
SELECT id, full_name, plan, created_at FROM shop.customers;
CREATE VIEW shop.agent_orders AS
SELECT id, customer_id, status, total, placed_at FROM shop.orders;
GRANT SELECT ON shop.agent_customers TO 'claix_agent'@'10.0.%';
GRANT SELECT ON shop.agent_orders TO 'claix_agent'@'10.0.%';By default a MySQL view runs with the privileges of its definer, so the agent does not need any privilege on the base tables. That is what makes this pattern work, and it is also why you should review who defines the view.
Row-level security in MySQL: there is none
PostgreSQL can restrict which rows a role sees with row-level security policies. MySQL does not support row-level security natively. Workarounds exist, and each has a cost:
| Workaround | How it works | Limit |
|---|---|---|
| One view per tenant or customer | Each view filters on a tenant column | Does not scale to thousands of customers |
| One database account per tenant, with a mapping table and CURRENT_USER() in the view | The view filters on the connected account | Many accounts to create and manage |
| Stored procedures that look up the caller's tenant | Grant EXECUTE on the procedure, not SELECT on the table | Only covers the questions you wrote procedures for |
| Enforce the filter in an access layer in front of the database | The layer adds the tenant filter to every query | You must build and test it |
If your agent serves many customers from shared tables, the filter has to live somewhere other than the model's SQL. A prompt that says always filter by customer is a request, not a restriction.
The gaps a read-only user leaves open
1. Read-only session settings are not a lock
SET SESSION TRANSACTION READ ONLY and START TRANSACTION READ ONLY apply to a session, and a session can change them. The actual barrier is the set of privileges granted to the account. Server-wide read_only is useful on a replica, but do not rely on a setting the agent's own connection can switch off.
2. No native row-level security
See above. Without a workaround, any row in a table the agent can read is visible to it.
3. Views expose whatever you put in them
A view with no filter exposes every row of its base table. A view over a sensitive table can become a quiet leak if someone adds a column later. Review views when the schema changes.
4. Stored routines can do more than you expect
To call a routine, an account needs the EXECUTE privilege, and it does not have it by default. The risk appears when you grant EXECUTE broadly (for example on a whole schema). A routine declared SQL SECURITY DEFINER runs with its owner's privileges, so if it writes data, the agent can write through it. Grant EXECUTE one routine at a time and read what each one does.
5. Global privileges are dangerous for an agent
Never give the agent FILE (which enables SELECT ... INTO OUTFILE and LOAD_FILE), PROCESS, SHOW DATABASES, SUPER or any administrative privilege. None of them is needed to read business data.
6. Expensive queries can slow production
A read-only account can still run a query that scans a huge table. Set a server-wide max_execution_time (in milliseconds, it applies to read-only SELECT statements) because a session can override its own value, and use account limits such as MAX_QUERIES_PER_HOUR and MAX_USER_CONNECTIONS. Better still, point the agent at a read replica and accept some replication lag. On MariaDB, the equivalent setting is max_statement_time.
7. A wide-open host and no TLS
An account defined as 'agent'@'%' can connect from anywhere. Restrict the host pattern, require TLS with REQUIRE SSL, and add a network allowlist.
8. Everything you grant can be read
A read-only role protects what it cannot reach. If the agent can read the email column, a prompt injected through a document it processes can ask it to reveal every email. Grant the minimum columns and tables, and keep personal and secret data out of the agent's reach.
Test the account before you trust it
Connect as the agent and confirm that each of these fails or returns nothing useful:
INSERT INTO shop.agent_orders (status) VALUES ('x'); -- denied: no INSERT privilege
SELECT email FROM shop.customers; -- denied: no access to the base table
SELECT * FROM mysql.user; -- denied
SELECT LOAD_FILE('/etc/passwd'); -- returns NULL without the FILE privilegeThen confirm that the queries the agent should be able to run actually work, and repeat the test after every schema change.
A hardened setup in one block
-- 1. Restricted account
CREATE USER 'claix_agent'@'10.0.%' IDENTIFIED BY 'use-a-long-random-secret'
REQUIRE SSL
WITH MAX_USER_CONNECTIONS 5 MAX_QUERIES_PER_HOUR 20000;
-- 2. Views with only the columns the agent may see
CREATE VIEW shop.agent_customers AS
SELECT id, full_name, plan, created_at FROM shop.customers;
CREATE VIEW shop.agent_orders AS
SELECT id, customer_id, status, total, placed_at FROM shop.orders;
-- 3. Grant the views only
GRANT SELECT ON shop.agent_customers TO 'claix_agent'@'10.0.%';
GRANT SELECT ON shop.agent_orders TO 'claix_agent'@'10.0.%';
-- 4. Server-wide read timeout in milliseconds (applies to SELECT)
SET GLOBAL max_execution_time = 5000;For writes, do not reuse this account. A separate path, with a deterministic check before every INSERT, UPDATE or DELETE, is safer. See how to stop DELETE or UPDATE without a real WHERE.
What a read-only account does not give you
| Need | Read-only account | Controlled access layer |
|---|---|---|
| Stop writes | Yes | Yes |
| Limit columns | Yes, with grants or views | Yes |
| Limit rows per customer | No native support | Yes, if the layer enforces the filter |
| Typed, predictable answers | No, raw rows | Yes |
| A record of every question and query | Only through logs you configure | Built in |
| Stop the agent from writing bad SQL | No | The layer can restrict what is allowed |
Claix Retrieve connects to your MySQL or MariaDB database through a read-only account like the one above and adds route-level limits on which tables and columns each agent can use, typed answers and a trace of every query. It is a layer on top of the account, not a replacement for it.
Frequently asked questions
- How do I create a read-only MySQL user for an AI agent?
- Create a dedicated account restricted by host with REQUIRE SSL, grant SELECT only on the specific tables, columns or views the agent needs, give it resource limits, and verify the result with SHOW GRANTS. Never grant *.* or global privileges.
- Is a SELECT-only user enough for an AI agent?
- It stops writes, which is essential, but it does not limit rows per customer, does not stop expensive queries and does not stop the agent from reading and revealing anything you granted.
- Does MySQL support row-level security?
- Not natively. You can approximate it with views, one account per tenant, stored procedures or an access layer in front of the database.
- Can I limit an AI agent to certain columns in MySQL?
- Yes. Use column-level grants, or better, create views that expose only the allowed columns and grant SELECT on the views.
- Should an AI agent use a read replica?
- Often yes: it keeps expensive or runaway queries away from production writes. Expect some replication lag and make sure the replica is set to read-only.
- How do I stop an AI agent from running very slow queries?
- Set a server-wide max_execution_time for SELECT statements, use account limits such as MAX_QUERIES_PER_HOUR and MAX_USER_CONNECTIONS, and use a read replica.
- Does this work with MariaDB?
- Mostly. Privileges, views and column grants work in a similar way. Some settings differ, for example max_statement_time instead of max_execution_time, so check the MariaDB documentation for the exact syntax.
- Can prompt injection still leak data with a read-only user?
- Yes. A read-only user cannot stop an agent from reading and repeating data it is allowed to see. Reduce what the account can reach and limit which rows each agent can read.
- Can the agent write with this account?
- No, and it should not. Handle writes through a separate path where every proposed INSERT, UPDATE or DELETE is checked before your own code runs it.
Conclusion
A dedicated, SELECT-only MySQL account on specific views, restricted by host and TLS and limited in resources, is the right base for an AI agent. Its main weaknesses are the absence of row-level security, the data you do choose to expose and expensive queries. Close them with views or an access layer, a read replica and timeouts, and keep writes on a separate, checked path. Claix Retrieve builds on exactly that kind of read-only account, with per-agent limits on tables and columns, typed answers and traces. Try Claix Retrieve free: 30 successful calls per month.
You might also like…
Security · Postgres
Read-only Postgres role for AI agents: step-by-step setup
Security · Agents
How to let an AI agent query your Postgres database safely
Guides · Supabase
How to Give an AI Agent Safe Access to Your Supabase Database with Claix
Security · Guardrails
How to Stop an AI Agent From Running DELETE or UPDATE Without a WHERE Clause