How to Fix MySQL Error 1621 (ER_VARIABLE_IS_READONLY)
You get mysql error 1621 when a query tries to change a system variable using the wrong scope. You fix this error by switching the statement to use the scope suggested in the error message. Database servers keep strict rules around system settings. Some settings apply to the whole server, while others apply to one session. When an app or engineer mixes up these scopes, the database blocks the change right away. It returns the symbol ER_VARIABLE_IS_READONLY and stops the command from running.
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 get mysql error 1621 when a query tries to change a system variable using the wrong scope. You fix this error by switching the statement to use the scope suggested in the error message.
Database servers keep strict rules around system settings. Some settings apply to the whole server, while others apply to one session. When an app or engineer mixes up these scopes, the database blocks the change right away. It returns the symbol ER_VARIABLE_IS_READONLY and stops the command from running.
What triggers mysql error 1621 in the database engine
Every system variable in the database has a set scope. A setting can be global, session, or both. Global settings control how the whole server runs for all users. Session settings only change how the current client connection behaves.
Under the official spec, this failure uses SQLSTATE HY000 and code 1621. The server message says that the target variable is read-only in the chosen scope. It then tells you which SET scope you must use to assign the value.
This issue often happens when a user tries to set a session-only rule at the global level. It also happens when a script tries to change a global-only setting inside a local session. Driver startup scripts and migration tools cause this problem when they run bad setup commands. Pool managers can also trigger it when they reset connection states with the wrong scope.
How to diagnose and fix the failure in production
Start by reading the full text of the error message in your app logs. The database tells you the exact variable name and the required scope in the error output. Check your app code or client settings to find where that variable gets set.
Once you find the bad command, update it to use the right scope. If the message asks for a session scope, remove the global keyword from your statement. If it asks for a global scope, make sure this change will not break other apps first. Global changes affect every user on the server at once.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Never grant admin roles to an app user just to bypass a variable error. App accounts should only change their own session state when needed. If a setting truly needs a global change, an admin should apply it during a maintenance window. You should also put global changes in the main server file so they survive a restart.
Preventing scope errors across your database fleet
Teams often run into this error during manual debug work on live servers. An engineer logs in to fix a slow query and types the wrong variable scope by mistake. While the engine blocks read-only changes, a valid global change can still hurt production traffic.
You can stop these mistakes by limiting who can run variable changes in production. Give developers read-only roles for daily debug work so they cannot alter server state. When your team queries live tables, data masking can rewrite emails, phone numbers, card numbers, and national IDs in results. This keeps sensitive data safe while engineers check system variables and query plans.
Test all startup scripts and app connection strings in a staging setup before shipping them. A quick test run will catch scope mistakes before they reach live users. Keep a clear list of approved session settings for your apps so teams use the right syntax.
Inspect every MySQL query and response in flight
Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev sits inline in the MySQL protocol as a sidecar for your MySQL. For every plain query, prepared statement, or cursor an agent or human runs in production, hoop.dev decides to pass, mask, block, or hold it for review.
First, hoop.dev guardrails block banned actions before MySQL runs them, and an AI Analyzer you configure can score intent or escalate for approval. Next, hoop.dev data masking rewrites sensitive string columns in results in memory, and every verdict goes to the audit trail. Find the open-source binary on GitHub.
Frequently asked questions
Does mysql error 1621 mean the entire database is in read-only mode?
No, this error only applies to the single system variable in your command. Your tables can still take normal writes while this variable error happens. Whole-server read-only mode uses a different error code when it blocks data writes.
Can I fix ER_VARIABLE_IS_READONLY by restarting the database server?
No, a server restart will not change the built-in scope rules for system variables. You must fix the command syntax in your script or app to use the right scope. If a variable only changes at startup, you must set it in the server file before restarting.
Why does my app throw error 1621 right when it connects?
Many database drivers and pool tools run a list of setup commands right after they log in. If one of those startup commands uses the wrong variable scope, the connection fails right away. Check your app connection string and driver settings to remove or fix the bad startup rule.
Related guides
In this post