Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

MySQL ERROR 1449 (HY000) The user specified as a definer does not exist: find the objects that name it, then redefine them

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

MySQL returns ERROR 1449 (HY000): The user specified as a definer ('olduser'@'localhost') does not exist when a view, stored routine or event has to run with the rights of its DEFINER account and that account is not in MySQL. The Error Reference gives the text as "The user specified as a definer ('%s'@'%s') does not exist". List every object that names the missing account, then give each an account that exists. Creating the account directly needs a privilege that overrides the check, and the account then takes over every object that names it.

What the error means

A view, stored procedure, stored function, trigger or event has a DEFINER attribute that names an account. The MySQL 8.4 page on stored object access control says "If a definition omits the DEFINER attribute, the default object definer is the user who creates it." Routines and views also have an SQL SECURITY setting of DEFINER (the default) or INVOKER. In definer context the object runs with the definer's privileges; with INVOKER "The DEFINER attribute has no effect on object execution."

An object whose definer account does not exist is an orphan: "An orphan stored object is one for which its DEFINER attribute names a nonexistent account". The page says when it fails:

  • For a stored routine, "an error occurs at routine execution time if the SQL SECURITY value is DEFINER but the definer account does not exist".
  • For a view, "an error occurs when the view is referenced if the SQL SECURITY value is DEFINER but the definer account does not exist".
  • For an event, "an error occurs at event execution time if the account does not exist".
  • For a trigger, the page says "the behavior with respect to privilege checking is undefined", so test a trigger rather than assuming it gives error 1449.

How an object becomes an orphan on MySQL 8.4: it is created with a nonexistent DEFINER by someone holding SET_ANY_DEFINER, which generates a warning; or the account is dropped or renamed. The page says DROP USER "fails with an error" if any account to be dropped is a definer, says the same of RENAME USER, and says that a user with ALLOW_NONEXISTENT_DEFINER overrides this so that "the statements succeed with a warning rather than failing with an error".

Our own reading: a dump taken on another server carries the DEFINER clause of each object (SHOW CREATE VIEW prints it), so restoring it on a server that lacks the account produces the same orphans.

Likely causes

Cause What you see How to confirm
A view with SQL SECURITY DEFINER whose account is missing 1449 when the view is selected INFORMATION_SCHEMA.VIEWS shows the DEFINER and SECURITY_TYPE; the account is not in mysql.user
A stored routine with the same fault 1449 on CALL or on a function call INFORMATION_SCHEMA.ROUTINES shows the DEFINER
An event whose account is missing 1449 in the event's run INFORMATION_SCHEMA.EVENTS shows the DEFINER
A dump restored on a server without the account (our own reading) Several objects fail at once after the restore The same DEFINER appears on all of them
An account dropped or renamed by a user with ALLOW_NONEXISTENT_DEFINER Objects worked until the account changed The account is gone from mysql.user; objects still name it

Prove the cause before changing anything

All of these only read.

  1. Take the account from the message. The stored-object page's queries compare DEFINER with 'user_name@host_name', while the VIEWS and ROUTINES table pages describe the column as 'user_name'@'host_name'. If a query below returns no rows for an account that the error names, run the SELECT DISTINCT DEFINER queries in step 4 and copy the value exactly as printed.
  2. Check whether it exists. Our own advice: the account list is in the mysql.user table.
SELECT User, Host FROM mysql.user WHERE User = 'olduser';
  1. List the objects that name it. These are the queries from the manual, with an example account:
SELECT TABLE_SCHEMA, TABLE_NAME, SECURITY_TYPE FROM INFORMATION_SCHEMA.VIEWS WHERE DEFINER = 'olduser@localhost';
SELECT ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE FROM INFORMATION_SCHEMA.ROUTINES WHERE DEFINER = 'olduser@localhost';
SELECT EVENT_SCHEMA, EVENT_NAME FROM INFORMATION_SCHEMA.EVENTS WHERE DEFINER = 'olduser@localhost';
SELECT TRIGGER_SCHEMA, TRIGGER_NAME FROM INFORMATION_SCHEMA.TRIGGERS WHERE DEFINER = 'olduser@localhost';
  1. See every definer in use: SELECT DISTINCT DEFINER FROM INFORMATION_SCHEMA.VIEWS; and the same for ROUTINES, EVENTS and TRIGGERS. The manual says that for an account that does not exist, any object shown "are orphan objects".

Fix

The manual's guideline is to give an object a definer that has "only the privileges required for the operations performed by the object". Redefine to such an account if one exists, and check with step 2 that it does.

This section gives steps for views and for stored procedures and functions. For an event, the stored-object page names ALTER EVENT as the way to change its definer, and for a trigger it says the object must be dropped and re-created; no steps for events or triggers are given here.

Redefine a view

The page says "you can use ALTER EVENT or ALTER VIEW to directly modify the DEFINER account of events and views." ALTER VIEW needs the full SELECT text, and it is "permitted only to the definer or users with the SET_ANY_DEFINER or ALLOW_NONEXISTENT_DEFINER privilege". The ALTER VIEW syntax begins with an optional ALGORITHM clause; repeat the ALGORITHM clause from the saved text if it has one.

First save the current state: run this and copy its result into v_open_orders-before.txt.

SHOW CREATE VIEW shop.v_open_orders\G

Risk: the next statement replaces the definition of the view shop.v_open_orders and changes its definer to reporting@localhost. From then on the view reads tables with that account's privileges.

Alter the view, using the SELECT text from the saved file:

ALTER DEFINER = 'reporting'@'localhost' SQL SECURITY DEFINER VIEW shop.v_open_orders AS SELECT id, total FROM shop.orders WHERE status = 'open';

To undo it: run the same form with the definer and SELECT text saved in v_open_orders-before.txt. This statement needs SET_ANY_DEFINER or ALLOW_NONEXISTENT_DEFINER, because olduser does not exist.

Risk: if the new account breaks the view and you need to back out, the next statement gives the view shop.v_open_orders its old, missing definer back, so the view fails with error 1449 again.

ALTER DEFINER = 'olduser'@'localhost' SQL SECURITY DEFINER VIEW shop.v_open_orders AS SELECT id, total FROM shop.orders WHERE status = 'open';

Re-create a stored procedure or function

The page says "you must drop the object and re-create it to assign a different DEFINER account". The routine's text must come from SHOW CREATE PROCEDURE or SHOW CREATE FUNCTION; the manual says the displayed value is NULL "if you have only CREATE ROUTINE, ALTER ROUTINE, or EXECUTE". If you see NULL, stop; get the text from a dump or source control instead.

First save the current state: run this and copy its result into close_orders-before.txt.

SHOW CREATE PROCEDURE shop.close_orders\G

Risk: the next statement drops the procedure shop.close_orders. Until the following CREATE succeeds, callers get an error that it does not exist. The drop cannot be undone by a statement; the saved CREATE text is the only way back.

Drop it:

DROP PROCEDURE shop.close_orders;

Risk: the next statement creates the procedure shop.close_orders again, with the definer reporting@localhost. From then on the procedure runs with that account's privileges.

Create it again. The CREATE PROCEDURE syntax begins CREATE [DEFINER = user] PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...] routine_body; take the parameters, characteristics and body from close_orders-before.txt:

CREATE DEFINER = 'reporting'@'localhost' PROCEDURE shop.close_orders(<parameters from the saved text>) <characteristics and body from the saved text>;

To undo it: run only the case that applies. If the new procedure was not created yet, only the saved CREATE is needed. If it was, drop the new one first. Both need the privilege SET_ANY_DEFINER or ALLOW_NONEXISTENT_DEFINER for the final statement, because the saved text names a definer that does not exist.

Risk: if you need to back out, the next statement drops the new procedure shop.close_orders; callers get an error that it does not exist until the saved CREATE runs.

DROP PROCEDURE shop.close_orders;

Risk: the next statement creates shop.close_orders with its old, missing definer olduser@localhost, so callers get error 1449 again.

Run the saved CREATE statement from close_orders-before.txt unchanged, then read the result with SHOW CREATE PROCEDURE shop.close_orders\G.

Create the missing account

Create the account only when the objects are meant to belong to it. The manual says "CREATE USER fails with an error if any account to be created is named as the DEFINER attribute for any stored object". The CREATE USER page says that to perform the operation anyway you must have the SET_ANY_DEFINER or ALLOW_NONEXISTENT_DEFINER privilege, and the statement then succeeds with a warning. Without either, the page says to drop the orphan objects, create the account and grant its privileges, and then re-create the dropped objects. The account then adopts every object that names it.

First save the current state: run the queries from step 3 and this one, and copy the results into accounts-before.txt. Our own advice: this statement reads the account table.

SELECT User, Host FROM mysql.user;

Risk: the next statement creates the account olduser@localhost. Without ALLOW_NONEXISTENT_DEFINER or SET_ANY_DEFINER it fails with an error, because the account is named as a definer; with either it succeeds with a warning. Every orphan that names it starts running with whatever privileges the account is later given. The back-out below is one DROP USER, which needs one of those same two privileges.

Create it:

CREATE USER 'olduser'@'localhost' IDENTIFIED BY '<password>';

The create-user page says "An account when first created has no privileges". To undo it: drop the account again. The objects that named it become orphans again, as they were before. This back-out returns the account list to what accounts-before.txt recorded; it does not save the new account's password hash.

Risk: if you need to back out, the next statement removes the account olduser@localhost, and every view, routine or event that names it fails with error 1449 again. Our own reading: it also removes any privilege granted to that account since it was created. The DROP USER page says the statement fails with an error if the account is named as a definer, and that with SET_ANY_DEFINER or ALLOW_NONEXISTENT_DEFINER it "succeeds with a warning rather than failing with an error". Our own reading: without either privilege, first drop the objects that name the account, or give them another definer by the view or procedure steps above, each with its own save and warning.

Drop the account and read mysql.user again:

DROP USER 'olduser'@'localhost';

Example (illustrative): On db01, a dump from an older server was restored. SELECT * FROM shop.v_open_orders; fails with ERROR 1449 (HY000): The user specified as a definer ('olduser'@'localhost') does not exist. mysql.user has no olduser. The VIEWS query returns two views, v_open_orders and v_late_orders, and the ROUTINES query returns one procedure, close_orders: three objects. The administrator redefines both views and re-creates the procedure with reporting@localhost, which holds SELECT and UPDATE on shop.orders.

Confirm the fix

  1. The step 3 queries for olduser@localhost return no rows.
  2. SELECT * FROM shop.v_open_orders; returns rows, and CALL shop.close_orders(); runs in a test window you control.
  3. SHOW WARNINGS after each statement shows no definer warning.

What to watch afterwards

  • After a restore on a new server, run the step 4 queries and compare every definer with mysql.user.
  • The manual's guideline: "Do not cause stored objects to become orphaned by dropping or renaming an account named by the DEFINER attribute of any existing object."
  • For a routine or view where it fits, the manual advises "SQL SECURITY INVOKER in the object definition when possible".

Risks and limits

  • These commands have not been run or lab-tested.
  • Redefining changes whose privileges the object uses; check what the new account may read and write before you run the statement.
  • The pages describe MySQL 8.4 only. MariaDB and earlier MySQL releases may behave differently.

Sources