How to Fix MySQL Error 1792 (ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION)
You see mysql error 1792 when a query tries to change data inside a read-only transaction. To fix it, start a read-write transaction or route write queries to a primary database node. MySQL maps this issue to symbol ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION and SQLSTATE 25006. The server stops the query and returns the message that it cannot run the statement. This rule protects your data when an app or user session expects zero data changes. What causes mysql error 1792 in your database This
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 see mysql error 1792 when a query tries to change data inside a read-only transaction. To fix it, start a read-write transaction or route write queries to a primary database node.
MySQL maps this issue to symbol ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION and SQLSTATE 25006. The server stops the query and returns the message that it cannot run the statement. This rule protects your data when an app or user session expects zero data changes.
What causes mysql error 1792 in your database
This error happens when a session tries to write, update, or delete rows while locked in read-only mode. Several layers in your stack can set this mode without you noticing.
First, the app or driver might start the current transaction in read-only mode on purpose. Object-relational mappers and framework annotations often mark service methods as read-only to save database resources. If a developer adds a write query inside that method later, the database blocks it right away.
Second, connection pools can pass old session state from one app request to the next. A worker thread might set a session to read-only and return the connection to the pool. If the pool does not reset the state, the next request inherits that read-only rule and fails.
Third, your database cluster might shift roles during a failover or maintenance event. If your app sends write queries to a read replica, or if a primary node steps down, writes fail. In other cases, a DBA sets the session default to read-only to stop accidental edits during an incident.
How to diagnose and fix the failure in production
Start by checking where the failing query comes from in your app logs. Look at the transaction settings wrapped around the code method that sent the write query. If the method needs to write data, update the code annotation so it starts a read-write transaction.
Read-Only Root Filesystem + Just-in-Time Access: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Sometimes a read-only method fails because it tries to write an audit log or update a cache table. Move those helper writes into a separate transaction block, or keep that method open for writes. Never fix this by giving the app user admin rights, which bypasses read-only guards on the server.
Next, check your connection pool settings if the error shows up at random times. Make sure the pool resets session state each time it hands a connection to a worker thread. You can also split read and write traffic into two distinct pools with clear rules.
Finally, check the target database node to confirm it is the active primary instance. Verify your DNS records or load balancer targets so write pools always point to the primary writer. If an engineer needs to run a manual fix, open a fresh read-write session for that single task.
Preventing read-only transaction conflicts across teams
Teams often run into this error when humans and apps share database roles without clear rules. When an engineer logs in to check a bug, they should use a strict read-only session. This stops a stray update query from changing live rows during a tense outage.
If that engineer tries to run a write query by mistake, error 1792 acts as a helpful safety net. To make a real change, the engineer should switch to an approved write role with full audit logging. You can also add data masking to read sessions so queries rewrite emails, phone numbers, and card numbers in results.
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, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
What is the exact SQLSTATE for mysql error 1792?
MySQL assigns code 1792 to SQLSTATE 25006 and the symbol ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION. It returns the message that it cannot execute the statement in a read-only transaction.
Can temporary tables trigger error 1792 in MySQL?
In older setups or specific storage engines, creating or changing tables inside a read-only transaction can fail. Check that your session uses temporary table keywords, or run the step outside the read-only block.
Why should we avoid fixing error 1792 with admin roles?
Admin roles can bypass read-only safety checks and allow writes on nodes meant to stay untouched. Giving an app admin rights risks corrupting replica data and removes a vital safety boundary in production.
Related guides
In this post