How to Fix MySQL Error 4049 (ER_OPERATION_NOT_ALLOWED_ON_GR_SECONDARY)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mysql error 4049 when you run a primary-only action on a secondary node. To fix it, find the current group primary and run your command there. The official symbol for this failure is ER_OPERATION_NOT_ALLOWED_ON_GR_SECONDARY with SQLSTATE HY000. MySQL returns a plain message when this rule triggers. It says this operation cannot be performed on a Group Replication secondary member. It also notes that the work must be done on the group primary. This rule protects your cluster from split-b

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 4049 when you run a primary-only action on a secondary node. To fix it, find the current group primary and run your command there.

The official symbol for this failure is ER_OPERATION_NOT_ALLOWED_ON_GR_SECONDARY with SQLSTATE HY000. MySQL returns a plain message when this rule triggers. It says this operation cannot be performed on a Group Replication secondary member. It also notes that the work must be done on the group primary. This rule protects your cluster from split-brain states and broken data.

What causes mysql error 4049 in group replication

Group Replication lets a set of database servers work together as one unit. In single-primary mode, only one node takes writes and cluster changes. All other nodes in the group act as secondary members. These secondary nodes take read traffic and apply updates from the primary.

The database engine blocks many actions on secondary nodes to keep data safe. If you try to change group settings on a secondary, the engine stops you. It throws code 4049 right away. This often happens after a cluster failover changes the node roles. An old primary becomes a secondary after it rejoins the group. If an engineer or script still connects to that old host, the command fails.

Load balancers and routers can also cause this problem. A router might send a client to a read port by mistake. If the app or admin tries a restricted command on that port, it fails. Even read-only checks do not stop every admin action from reaching the wrong node. That is why the engine uses code 4049 as a hard stop.

How to find the primary and fix the failure

Do not turn off read-only flags on the secondary node to bypass this error. Forcing changes on a secondary node can break replication and corrupt your data. You should never grant extra super privileges to work around group rules either. Instead, you must check the cluster state and move your session to the primary.

First, query the performance schema tables on any member of the group. Check the member list to see the current role of each server. Look for the node marked as the primary member with an online state. Note the host name and port for that specific server.

Single Sign-On (SSO) + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Next, close your current database session on the secondary node. Open a new session that points straight to the primary host. If you use a router, make sure you use the read-write port. Once you log in to the primary, check its role one more time. Now you can run your command safely, and the group will sync the change.

How to prevent scripts from hitting secondary nodes

Automated jobs and admin scripts should never use fixed host names for cluster work. When a primary node fails, another node takes over the primary role. Fixed host names will point to a secondary node after the switch completes.

Point your scripts and admin tools to a cluster-aware router or DNS name. The router tracks the group state and sends traffic to the active primary. You can also add a quick role check at the start of admin scripts. If the script sees a secondary role, it can stop before throwing an error. For data safety during admin checks, teams can also use data masking. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs 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 does ER_OPERATION_NOT_ALLOWED_ON_GR_SECONDARY mean?

This symbol means you ran a command that only the group primary can handle. You must connect to the primary node in your Group Replication cluster to run it.

Can I fix mysql error 4049 by turning off read-only mode?

No, you should never turn off read-only mode on a active secondary member. Doing so will not allow primary-only group commands and can break cluster replication.

Why did my working script start throwing error 4049 today?

Your cluster likely went through a primary failover or a planned role switch. The host your script uses is now a secondary node instead of the primary.

The query runs. The emails stay hidden.