Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySQL says execute command denied to user for routine

ErrorExecute command denied to user ''@'localhost' for routine

Short answer

The account running CALL or the function has no EXECUTE privilege on that routine. When the error names ''@'localhost', your named account was not the one matched: the server used an anonymous user row. Run SELECT CURRENT_USER() to see, then GRANT EXECUTE on the routine, or grant it to the routine's definer.

  • Applies to MySQL 8.0, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the server who you really are, and what that account may do. The user in the error message is the account that was matched, which may not be the name you typed.

    SELECT CURRENT_USER();
    SHOW GRANTS;
  2. 2Match what you see, then jump to its fix

    1. 1The user in the error is the account you meant to useThe account has no EXECUTE privilege on the routineFix
    2. 2The error names ''@'localhost' (empty user) although you logged in with a nameThe connection matched an anonymous user rowFix
    3. 3The call itself is allowed, but the error appears when the routine body runsThe routine runs as its definer, and the definer lacks privilegesFix

What does the error look like?

  • Execute command denied to user ''@'localhost' for routine

Cause 1 of 3

The account has no EXECUTE privilege on the routine

1Check

SHOW GRANTS lists no EXECUTE for the routine, its database, or globally. EXECUTE can be granted globally, for a database, or for one routine.

SHOW GRANTS FOR 'app_user'@'localhost';

2Fix

Grant EXECUTE at the narrowest level that works, a single routine or all routines in the database.

GRANT EXECUTE ON PROCEDURE mydb.myproc TO 'app_user'@'localhost';
-- or every routine in one database:
GRANT EXECUTE ON mydb.* TO 'app_user'@'localhost';

3Verify

Reconnect as that user and call the routine.

CALL mydb.myproc();

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

The connection matched an anonymous user row

1Check

A blank User in mysql.user matches any name. Rows are sorted by host specificity first, so an anonymous row for a more specific host can win over your named account for %. CURRENT_USER() then shows something like @localhost.

SELECT CURRENT_USER();
SELECT Host, User FROM mysql.user ORDER BY User, Host;

2Fix

Either create the named account for the exact host the connection comes from, or remove the anonymous row if you do not use it. Check the order of rows: a more specific Host sorts first.

-- make sure 'app_user' exists for the same Host value as the anonymous row,
-- or delete the anonymous account

3Verify

Reconnect and check which account is matched.

SELECT CURRENT_USER();

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

The routine runs as its definer, and the definer lacks privileges

1Check

Routines created with SQL SECURITY DEFINER, the default, run with the privileges of the DEFINER account. That account needs the privileges the body uses. With SQL SECURITY INVOKER the caller needs them.

SHOW CREATE PROCEDURE mydb.myproc\G

2Fix

Grant the missing privileges to the DEFINER account, or recreate the routine with the DEFINER and SQL SECURITY you intend. Setting another account as DEFINER needs the SET_ANY_DEFINER privilege.

GRANT EXECUTE ON PROCEDURE mydb.other_proc TO 'definer_user'@'localhost';

3Verify

Call the routine again.

CALL mydb.myproc();

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Grant EXECUTE on specific routines or one database and not globally.
  • Remove anonymous accounts on servers that do not need them.
  • Name DEFINER and SQL SECURITY explicitly in the routine definition.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.