Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySQL error 1305: the function you call does not exist

ErrorERROR 1305 (42000): FUNCTION mydb.my_func does not exist

Short answer

Error 1305 means MySQL found no built-in, loadable or stored function with that name in the place it looked. Ask the server which stored functions exist and in which database. The usual causes are a function in another database, a restore that left routines out, or a name that is not a function in your MySQL version.

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

Fix it fast

  1. 1Find out which cause you have

    List the stored functions with that name and see which database holds them. Note that the ROUTINES table does not list built-in or loadable functions, and an account only sees routines it has some privilege on. An empty result therefore means: no stored function of that name that your account can see.

    SELECT ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE
    FROM INFORMATION_SCHEMA.ROUTINES
    WHERE ROUTINE_TYPE = 'FUNCTION' AND ROUTINE_NAME = 'my_func';
  2. 2Match what you see, then jump to its fix

    1. 1The first-step query returns a row, but its ROUTINE_SCHEMA is not the database named in the error messageThe function lives in another database than the one you are inFix
    2. 2The first-step query returns no row anywhere, and the function worked on the source serverA restore or a dropped database removed the functionFix
    3. 3No row in ROUTINES for any database that your account can see, and the name is meant as a built-in function (a typo, or a function copied from another database system)The name is not a function in this MySQL version at allFix

What does the error look like?

  • ERROR 1305 (42000): FUNCTION mydb.my_func does not exist
  • Error Code: 1305. FUNCTION mydb.my_func does not exist

Cause 1 of 3

The function lives in another database than the one you are in

1Check

A stored function belongs to one database. The manual says you can qualify a routine name with the database name to refer to a routine that is not in the current database. Compare the schema from the query with the current one.

SELECT DATABASE();
SELECT ROUTINE_SCHEMA, ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'my_func';

2Fix

Call the function with the database name in front, or switch to the database that holds it before the call.

SELECT otherdb.my_func(42);
-- or
USE otherdb;
SELECT my_func(42);

3Verify

The call returns a value instead of error 1305.

SELECT otherdb.my_func(42);

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

A restore or a dropped database removed the function

1Check

Stored routines go away with their database: dropping a database drops its routines. A dump contains routines only when --routines is used: the manual describes that option as including stored routines in the output, and for --all-databases in MySQL 8.4 it says to name --routines explicitly.

SELECT ROUTINE_SCHEMA, COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES GROUP BY ROUTINE_SCHEMA;

2Fix

Dump the database again from the source server with --routines, which adds the CREATE FUNCTION and CREATE PROCEDURE statements, and load that file. The option needs the global SELECT privilege.

mysqldump -u root -p --routines --databases mydb > mydb.sql
mysql -u root -p < mydb.sql

3Verify

The first-step query now returns the function.

SELECT ROUTINE_SCHEMA, ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION';

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

The name is not a function in this MySQL version at all

1Check

MySQL reads an unqualified name as a built-in function if one exists, and otherwise as a loadable or stored function if one exists with that name. If none of the three exists, you get this error.

SELECT VERSION();
-- then look the name up in the manual's built-in function reference for that version

2Fix

Use the documented built-in function with the right spelling for your version, or create your own stored function and call it with its database name.

-- built-in functions are listed here for each MySQL version:
-- https://dev.mysql.com/doc/refman/8.4/en/built-in-function-reference.html

3Verify

The call runs without error 1305.

SELECT my_func(42);

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Call your own functions with the database name in front, so a different default database does not break them.
  • Dump with --routines, and --events if you use them, every time you copy a database; an --all-databases dump needs both in 8.4.
  • After a restore, run the ROUTINES query above and compare the count with the source server.

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.