How to Configure OpenAI Agents SDK MySQL Read Only Access
You set up openai agents sdk mysql read only access by creating a restricted database user. Then you pass those select-only credentials to the tool function your agent runs. Many teams start by giving an agent the same database key the main app uses. That shortcut lets an agent alter rows or drop tables if a prompt goes wrong. A dedicated read-only user stops write commands at the database engine level. How openai agents sdk mysql read only access works in practice The OpenAI Agents SDK uses
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 openai agents sdk mysql read only access by creating a restricted database user. Then you pass those select-only credentials to the tool function your agent runs. Many teams start by giving an agent the same database key the main app uses. That shortcut lets an agent alter rows or drop tables if a prompt goes wrong. A dedicated read-only user stops write commands at the database engine level.
How openai agents sdk mysql read only access works in practice
The OpenAI Agents SDK uses Python functions as tools that the model can call. The SDK itself does not include a built-in database client or SQL engine. Instead, developers write a custom tool function that opens a database session using a standard driver. The model decides when to call that function and passes the query text as an argument.
To enforce read-only rules, you start inside the MySQL server. A database admin creates a new user account just for the agent. The admin grants only the select privilege on the specific tables the agent needs. You can also set the session transaction mode to read only inside the Python tool function. Next, you store the new user password in a secret manager or local key file. The tool function reads this secret when it starts up and connects to MySQL.
When a user asks the agent a question, the model writes a SQL statement. The SDK passes that statement into your Python function. Your function sends the query to MySQL over the restricted session. If the model tries to run an update, delete, or drop statement, MySQL blocks it. The database returns a permission error, and your production data stays intact.
What native database roles solve and what they leave open
A MySQL read-only role solves the problem of accidental or malicious writes. It ensures that an agent cannot change records, delete tables, or alter schemas. Database admins can also limit which schemas and tables the agent account can read. This setup is a strong first step for any team building SQL tools for agents.
Still, database privileges only check who logs in and which table they touch. They do not inspect what individual queries do in flight or what rows come back. An agent with select access to a customer table can still read every column in it. If a table holds emails, phone numbers, card numbers, or national IDs, the query returns them. The raw values go straight into the model context and out to an external API. Data masking rewrites matched fields in results so raw sensitive strings do not reach the model.
Auditor Read-Only Access + MySQL Access Governance: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Native roles also struggle with audit trails when many agents share one database user. Every query in the MySQL log shows the same shared agent account name. You cannot tell which user session or agent run triggered a specific slow query. Also, a bad prompt can make an agent run a massive join across millions of rows. Read-only permissions will allow that query to run and lock up database memory.
Steps to harden your agent database tool functions
You can add extra checks inside your tool design to reduce risk even further. First, do not let the model write raw SQL if a fixed query template will work. A tool function that takes a customer ID and runs a preset query is much safer. It stops the model from guessing table names or joining unintended tables.
Second, always set a strict query timeout and a maximum row limit in your database driver. If the model writes a slow query, the database will cancel it before the server stalls. Third, build database views that expose only the safe columns the agent needs to do its job. Grant the agent user select rights on those views instead of the base tables.
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
Can an OpenAI agent bypass MySQL read-only permissions?
No, if you enforce read-only grants on the MySQL user, the database blocks all writes. The agent enforces policy on server privileges unless it finds a way to use another credential.
Should I give my agent access to the primary database or a read replica?
You should point the agent tool at a read replica whenever one is available. A replica isolates heavy agent queries so they do not slow down your main app traffic.
How do I keep sensitive columns out of the model prompt?
You can grant access only to restricted MySQL views that leave out private columns. You can also use data masking to rewrite emails, phone numbers, and card numbers in results.
Related guides
In this post