How to Fix MySQL Error 1228 (ER_LOCAL_VARIABLE)
You trigger mysql error 1228 when you try to update a session-only variable across the whole server. To fix it, change the variable for your current session only instead of using a global command. MySQL maps this failure to symbol ER_LOCAL_VARIABLE and SQLSTATE HY000. The server returns a message stating that the variable is a session variable and cannot be used with a global scope. Understanding how MySQL separates server-wide settings from session settings helps your team avoid this issue.
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 trigger mysql error 1228 when you try to update a session-only variable across the whole server. To fix it, change the variable for your current session only instead of using a global command.
MySQL maps this failure to symbol ER_LOCAL_VARIABLE and SQLSTATE HY000. The server returns a message stating that the variable is a session variable and cannot be used with a global scope. Understanding how MySQL separates server-wide settings from session settings helps your team avoid this issue.
What causes mysql error 1228 in your database
MySQL divides system variables into two main scopes: global and session. Global variables control how the entire database server runs for all users. Session variables control how the database acts for one single client connection.
Many variables exist in both scopes. You can change the global default, and new sessions will copy that value. Still, some variables only make sense inside one active session. These session-only variables track state for a single user, transaction, or temporary query setting.
When an engineer or an app tries to set a session-only variable at the global level, the engine blocks the request. It throws mysql error 1228 right away. The database does this because applying a personal connection state to every user on the server would break active work.
This problem often shows up during database migrations or when teams write startup scripts. A developer might assume a setting applies to the whole server and add a global scope keyword by mistake. It also happens when an app pool runs setup queries with the wrong scope.
How to find and fix the variable scope issue
Start by reading the exact error text in your app log or client output. MySQL puts the offending variable name right inside the error message. Once you know the variable name, check the official MySQL manual to confirm its allowed scope.
Next, find the script, migration file, or app setup code that sent the bad query. Rewrite that command so it targets the session scope instead of the global scope. When you set the variable at the session level, the change applies right away to your current connection.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If your app needs this setting for every query, do not try to force a global update. Instead, update your app connection pool settings. Tell the pool to run the session update command each time it opens a fresh connection to the database.
Follow safe operational practices when you apply this fix in production. You do not need to grant high-level admin rights to fix a session variable error. Standard app roles can change most session variables without extra privileges, which keeps your database permissions tight and safe.
How to prevent scope errors in production
Scope errors often happen when engineers run ad-hoc admin statements during an incident. Someone copies a command, adds a global modifier to make it stick, and hits an error. Or worse, they change a real global variable and break other apps sharing the server.
You can stop these mistakes by reviewing database scripts before they run against live data. Test all schema and variable changes in a staging setup first. Keep a clear list of which settings belong in server startup files and which belong in app connection hooks.
You should also limit who can run server-wide commands in production. Give developers and apps only the rights they need for daily work. When teams use scoped roles and clear review workflows, they catch bad variable commands before those queries ever reach the database engine.
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 does ER_LOCAL_VARIABLE mean in MySQL?
ER_LOCAL_VARIABLE is the official MySQL symbol for error code 1228 under SQLSTATE HY000. It means your query tried to set a global value for a variable that only works inside a single session.
Do I need admin rights to fix mysql error 1228?
No, you do not need admin rights to fix this error. Because the target variable only applies to your own session, a standard user role can update it once you switch to the session scope.
How do I apply a session variable to every new app connection?
Since you cannot set a session-only variable globally, configure your app connection pool to run the session command when each connection starts. This gives every new session the right setting without triggering error 1228.
Related guides
In this post