Incident Response

MySQL ERROR 1449: The user specified as a definer does not exist

A view, procedure, trigger or event still points at an account that is gone. It usually shows up right after a restore on a new server or a user cleanup. Here is how to find every orphaned object, which fix fits which object type, and how to stop dumps from carrying the problem along.

· 13 min read · AG Data Team

ERROR 1449 (HY000): The user specified as a definer ('...'@'...') does not exist means you used a view, stored routine, trigger or event whose DEFINER names a MySQL account that is not on this server. The object is now an orphan. In most cases nothing is wrong with your query: the object was created by a user who has since been dropped, or the schema was restored from a dump taken on another server.

mysql> SELECT * FROM shop.order_summary;
ERROR 1449 (HY000): The user specified as a definer ('deploy'@'10.0.%') does not exist

In the server error reference this is ER_NO_SUCH_USER, number 1449, SQLSTATE HY000. The account in the message is exactly the one you need to look for, host part included.

Why a missing account breaks the object

Every view and stored program has a DEFINER. Views, procedures and functions also have SQL SECURITY, which defaults to DEFINER: the object runs with the privileges of the definer account, not of the person calling it. Triggers and events have no such option and always run in definer context. According to the stored object access control documentation:

Watch the host part. MySQL accounts are user plus host, so 'deploy'@'%' existing does not help an object defined by 'deploy'@'10.0.%'. The same rule causes many "access denied for user" errors.

How objects become orphans

Find every orphan at once

Do not fix them one error at a time. The definer of each object is in INFORMATION_SCHEMA, in user@host form, so one query compares them all with mysql.user:

-- every stored object whose definer account no longer exists
SELECT * FROM (
  SELECT 'VIEW' AS type, TABLE_SCHEMA AS db, TABLE_NAME AS name, DEFINER
    FROM information_schema.VIEWS
  UNION ALL
  SELECT ROUTINE_TYPE, ROUTINE_SCHEMA, ROUTINE_NAME, DEFINER
    FROM information_schema.ROUTINES
  UNION ALL
  SELECT 'TRIGGER', TRIGGER_SCHEMA, TRIGGER_NAME, DEFINER
    FROM information_schema.TRIGGERS
  UNION ALL
  SELECT 'EVENT', EVENT_SCHEMA, EVENT_NAME, DEFINER
    FROM information_schema.EVENTS
) o
WHERE o.DEFINER NOT IN (SELECT CONCAT(User, '@', Host) FROM mysql.user)
ORDER BY db, type, name;

For each row, decide: should this account exist, or should the object belong to someone else?

Fix 1: re-create the account

If the account was dropped by mistake and the objects should keep working exactly as before, create it again with the privileges the objects need. There is a trap here. Since 8.0.22, CREATE USER also fails if the new account is named as the definer of an orphaned object, because the account would silently "adopt" those objects. To do it anyway you need ALLOW_NONEXISTENT_DEFINER on MySQL 8.2 and later, or SET_USER_ID on 8.0. The MySQL 8.2.0 release notes split SET_USER_ID into SET_ANY_DEFINER and ALLOW_NONEXISTENT_DEFINER; SET_USER_ID was deprecated in 8.2.0 and removed in 8.4.0.

Think before you re-create an old account just to silence the error. Every orphaned object with that definer becomes runnable with its privileges, including ones you forgot about.

Fix 2: change the definer

Usually the better fix is to give the objects to a real, dedicated owner account. How you do it depends on the type:

-- views: ALTER VIEW needs the full SELECT, take it from SHOW CREATE VIEW
SHOW CREATE VIEW shop.order_summary\G
ALTER DEFINER = 'app_owner'@'10.0.%' SQL SECURITY DEFINER
  VIEW shop.order_summary AS
  SELECT ...;   -- the original SELECT, unchanged

-- events: only the definer changes
ALTER DEFINER = 'app_owner'@'10.0.%' EVENT shop.purge_sessions;
-- procedures and functions: no ALTER ... DEFINER, so drop and re-create
SHOW CREATE PROCEDURE shop.close_month\G
DROP PROCEDURE shop.close_month;
CREATE DEFINER = 'app_owner'@'10.0.%' PROCEDURE shop.close_month(...)
  ...;          -- the original body

-- or, if the routine can run with the caller's privileges:
ALTER PROCEDURE shop.close_month SQL SECURITY INVOKER;
-- triggers: drop and re-create, with a short metadata lock wait
SET SESSION lock_wait_timeout = 5;
SHOW CREATE TRIGGER shop.orders_bi\G
DROP TRIGGER shop.orders_bi;
CREATE DEFINER = 'app_owner'@'10.0.%' TRIGGER shop.orders_bi
  BEFORE INSERT ON shop.orders FOR EACH ROW
  ...;          -- the original body

Switching a routine to SQL SECURITY INVOKER with ALTER PROCEDURE (or ALTER FUNCTION), or a view with ALTER VIEW ... SQL SECURITY INVOKER, avoids the error without re-creating anything, but it changes the security model: the code now runs with the caller's privileges. That is often what you want, but check that callers have every privilege the body needs.

Use a dedicated owner account. Do not set the definer to root or to a personal admin login. A definer-context object runs with all of its definer's privileges, whoever calls it, and personal accounts are exactly the ones that get dropped when someone leaves.

Stop the next migration from doing it again

mysqldump has no option to drop DEFINER clauses. Either create the definer accounts on the target before you load, or rewrite the definers in the dump. MySQL Shell's dump utilities have a strip_definers compatibility option: it removes the DEFINER clause from views, routines, events and triggers and switches views and routines to SQL SECURITY INVOKER. That changes behaviour too, so check that the application accounts have the privileges the code needs. Our comparison of mysqldump and XtraBackup covers which tool fits which migration.

After every restore, run the orphan query above before the application goes live.

On large production servers

Fixing one view on a test server takes a minute. On a production server with hundreds of objects and steady traffic, plan for these:

Key takeaways

MySQL Errors Security Migrations
Work with us

Errors after moving MySQL to a new server?

We migrate and clean up MySQL databases: accounts, definers, privileges and stored code, without surprises on the first day in production.

Get in touch More from the blog