Transform Learning with a Powerful Enterprise LMS

Learn more
Moodle

Moodle "Error Reading From Database": How to Debug and Fix It

"Error reading from database" is Moodle's wrapper for any failed read query. Get the database error, SQL and parameters from the logs, then fix the usual causes: pending upgrades, plugin SQL, limits and collations.

Learn more about this topic with AI

Gemini opens with the prompt copied - paste it into the chat to start.

Illustration of a Moodle error page with a database icon, representing debugging the Moodle error reading from database

A Moodle error reading from database page is a generic wrapper. Moodle shows "Error reading from database" whenever the database rejects a read query, and hides the real reason unless debugging is on. To debug it, get the hidden details first: the database's own error message, the SQL that failed and its parameters. Moodle writes all three to the PHP error log whenever the "Debug messages" setting is above NONE, and shows them on the error page at DEVELOPER level. Once you have that text, the cause is usually one of a handful: code and database out of step after an update, a plugin query that your database engine rejects, a query too large for the driver, a database server limit, or a collation mismatch on MySQL or MariaDB.

This guide follows the order we use on client sites: the problem, its symptoms, causes ranked by how often we see them, a diagnostic procedure, the fix for each cause, how to confirm the fix and how to prevent a repeat. Checked against the Moodle 5.3.0 source (dmllib.php, setuplib.php, config-dist.php and the admin/cli scripts) and the Moodle Debugging and DML exceptions documentation (October 2026). The exception, the language string and the debug settings named here are the same on 5.1, 5.2 and 4.5 LTS.

The problem: what the message means

All database access in Moodle goes through the DML layer ($DB). When a read query fails, the driver throws dml_read_exception. Its error code is dmlreadexception, and the language string for that code is the text users see: "Error reading from database". The developer documentation describes it simply as a problem that "occurred during reading from database".

The exception carries three pieces of debug information: the error returned by the database, the SQL that ran, and the parameter values. Users never see these by default, which is correct for a production site but means the visible message tells you almost nothing.

Do not confuse it with its neighbours, which point to different problems:

  • "Error writing to database" (dmlwriteexception) is the same mechanism for inserts and updates.
  • "Database connection failed" means Moodle could not connect at all. That needs a different checklist, covered in our guide to Moodle "Database connection failed".
  • "Can't find data record in database table" is a query that ran fine but returned no row where one was required, usually a broken link or deleted record, not a database fault.

Symptoms

  • A page, often a single course, activity, report or admin screen, shows "Error reading from database" with a "More information about this error" link, while the rest of the site works.
  • An AJAX-driven block or form fails and the browser's network tab shows the same message in the response.
  • A scheduled or ad hoc task fails, and its entry under Site administration > Server > Tasks > Task logs records the exception.
  • The error starts straight after a plugin install, a core update, a database migration or a server change. That timing is the most useful clue you have.

Root causes, ranked by likelihood

  1. Code and database out of step. New plugin or core code is in place, but the upgrade has not run, or a plugin's upgrade step failed part way. The code then queries a table or column that does not exist yet.
  2. A plugin or custom query the database rejects. SQL written and tested on one engine fails on another, for example MySQL-only syntax on PostgreSQL, or a GROUP BY that a stricter SQL mode refuses. Moodle's plugin checklist names "SQL fails to work on PostgreSQL" as an approval blocker for exactly this reason.
  3. A query too large for the driver. Very long IN() lists can exceed the database's limit on bound parameters. Moodle fixed one core case in 5.1.6 and 5.2.2, where a tag cleanup task failed with "Error reading from database (number of parameters must be between 0 and 65535)" (MDL-87395).
  4. Database server limits or faults. Lost connections during long queries, timeouts, packet size limits, a full disk or a server that is restarting.
  5. Collation or character set mismatch on MySQL and MariaDB. Tables left on different collations after a migration or a partial conversion, so joins between them fail.

Diagnostic procedure

  1. Read the PHP error log first. Moodle's default exception handler logs a line starting "Default exception handler:" with the message, the full debug information and a backtrace whenever the debug level is MINIMAL or higher. Check the web server or PHP-FPM error log for the time of the failure. This works on production without showing anything to users.
  2. If the log is empty, raise debugging for yourself only. Set Site administration > Development > Debugging > "Debug messages" to DEVELOPER and turn on "Display debug messages" briefly, or limit it to your account in config.php:
    // Debug output only for these user ids. Remove when finished.
    $CFG->debugusers = '2';
    config-dist.php documents this setting as a list of user ids that "always see debug messages". Replace 2 with your own user id. At DEVELOPER level the error page shows a "Debug info" block with the database error, the SQL and the parameters, plus the error code and a stack trace.
  3. Read the three parts. The first line is the database's own message, such as a missing table, unknown column, syntax error or collation conflict. The SQL shows the table names with your prefix, which identify the component (a table such as mdl_local_example_items belongs to local_example). The stack trace shows which plugin file called the query.
  4. Check for a pending upgrade. Open Site administration > Notifications, or from the code root run php admin/cli/upgrade.php --is-pending, which exits with code 2 if an upgrade is required.
  5. Compare the schema. Run php admin/cli/check_database_schema.php as the web server user. It compares the live database with every component's install.xml and prints "Database structure is ok." or a list of differences per table.
  6. Reproduce the query by hand. If the failing statement is a SELECT, paste it into your database client on a copy of the database, substitute the parameters, and read the full error from the engine. Never run anything other than a read on production.
  7. For a failing scheduled task, run it from the CLI with debugging. php admin/cli/scheduled_task.php --execute='\core\task\tag_cron_task' --showdebugging --showsql (with your task's class) prints developer-level debugging and each SQL statement before it runs, so the last statement printed is the one that failed.
  8. Check the database server log for the same timestamp: restarts, out-of-memory kills, disk full or timeout messages explain errors that come and go.
What the database error saysLikely causeGo to fix
Table does not exist, unknown columnCode and database out of step1
Syntax error, invalid GROUP BY, function does not existPlugin or custom query2
Number of parameters out of range, packet too largeQuery too large3
Server has gone away, connection lost, timeoutServer limits or faults4
Illegal mix of collationsCollation mismatch5

Fixes for each cause

1. Code and database out of step

Run the upgrade as the web server user: php admin/cli/upgrade.php, then php admin/cli/purge_caches.php. If a plugin's upgrade step failed part way, read its error, fix the cause and run the upgrade again. If the plugin code was rolled back to an older version than the database records, restore the matching code; never downgrade code without restoring the database from the same point. Then run the schema check again.

2. Plugin or custom query

Update the plugin to a release that supports your Moodle branch and database engine, and check its issue tracker for the error text. For your own code, build queries with the DML API rather than engine-specific SQL: use $DB->get_in_or_equal() for lists, placeholders for every value, and the sql_* helper methods for anything engine-specific. Test on PostgreSQL and MySQL or MariaDB if the plugin will run on both. If the plugin cannot be fixed quickly, disable the feature that calls it rather than leaving users on an error page.

3. Query too large

If the error is in core, update to the latest minor release for your branch; fixes like MDL-87395 arrive that way. In your own code, process large lists in batches, or join against a table or temporary table instead of passing thousands of ids in one IN() list.

4. Database server limits or faults

Fix the server, not Moodle: free disk space, raise timeouts or packet limits to suit your largest queries, and find why connections drop. If the failing query is simply slow, look at missing indexes and the query plan before raising limits. Our Moodle performance guide covers database tuning, and our Moodle hosting requirements list the supported database versions per branch.

5. Collation mismatch

Moodle ships a conversion script for MySQL and MariaDB. Take a backup, put the site into maintenance mode, list the current state with php admin/cli/mysql_collation.php --list, then convert with php admin/cli/mysql_collation.php --collation=utf8mb4_unicode_ci. The script's own help recommends stopping the web server first. Make sure the dbcollation value in $CFG->dboptions matches the collation you converted to.

Confirm the fix

  • Repeat the exact action that failed, as the same user role, and check the PHP error log stays clean.
  • For task failures, run the task once from the CLI with php admin/cli/scheduled_task.php --execute='\core\task\tag_cron_task', replacing the class with the one shown in the task log, and check the next scheduled run in Task logs.
  • Run check_database_schema.php once more and expect "Database structure is ok.".
  • Turn "Debug messages" back to its normal level, turn "Display debug messages" off and remove any $CFG->debugusers line.

Preventing it from happening again

  • Upgrade on staging first, with a copy of the production database on the same engine and version, then run the schema check before and after.
  • Keep the PHP error log and alert on "Default exception handler" lines. Leaving "Debug messages" at MINIMAL or NORMAL with display off gives you the details without exposing them to users.
  • Log failing queries during an incident. config-dist.php documents a logerror option in $CFG->dboptions that stores erroneous queries in the log_queries table. Turn it on while you investigate a recurring error, then turn it off.
  • Trace SQL back to code. The "Show origin of SQL calls" debugging setting adds a partial or full PHP stack trace to each query as a comment, so a query in the database's slow or error log points at the file that sent it.
  • Stay on the latest minor release of your branch, and keep plugins on versions marked for that branch.

For the wider picture of Moodle versions, upgrades and support dates, see our Moodle guide.

How we can help

We debug and maintain Moodle sites every week, from failed plugin upgrades and cross-database query bugs to database tuning and migrations between engines. See our Moodle services, or send us the debug output from your error log through our contact page, with passwords and personal data removed.

Frequently asked questions

What does "Error reading from database" mean in Moodle?

It means the database rejected a read query. Moodle throws dml_read_exception with the error code dmlreadexception and hides the database's own message, the SQL and the parameters unless debugging is enabled.

How do I see the SQL behind the error?

Check the PHP error log for a line starting "Default exception handler:", which Moodle writes when the debug level is MINIMAL or higher. Or set Debug messages to DEVELOPER, or add your user id to $CFG->debugusers, and reload the page to see the Debug info block.

Why did the error start after installing a plugin?

Usually the upgrade has not run, or the plugin's upgrade step failed, so its tables or columns are missing. Run php admin/cli/upgrade.php, then php admin/cli/check_database_schema.php. If the schema is fine, the plugin's SQL may not support your database engine.

Is it safe to turn on debugging on a live Moodle site?

Displaying debug output to everyone is not, because it reveals SQL and file paths. Limit it to your own account with $CFG->debugusers, or rely on the PHP error log with display turned off, and revert the settings when you are done.

Free 30-minute consultation

Not sure where to start? Talk to an LMS engineer.

Tell us what platform you run and what you need. You’ll get honest, practical advice - even if the answer is that you don’t need us.

Sources & references

  1. Debugging - Moodle
  2. DML exceptions - Moodle HQ
  3. lib/dmllib.php (Moodle 5.3.0) - Moodle HQ
  4. config-dist.php (Moodle 5.3.0) - Moodle HQ
  5. admin/cli/check_database_schema.php (Moodle 5.3.0) - Moodle HQ
  6. Moodle 5.2.2 release notes (MDL-87395) - Moodle HQ
Add as a preferred source on Google

Add All-in-One LMS as a preferred source to see more of our articles in Google’s Top Stories.

Learn more about this topic with AI

Gemini opens with the prompt copied - paste it into the chat to start.

Keep reading

Step path illustration for diagnosing a Moodle cron that is not running, from the status check to fixing the crontab and stuck tasks

· 11 min read

Moodle Cron Not Running: Diagnosis and Fixes

Why Moodle cron stops, how to diagnose it from the status check and a manual run of admin/cli/cron.php, the fix for each cause, and how to confirm an…

Illustration of five ranked cards with the top card starred, representing a Moodle theme comparison for institutions in 2027

· 15 min read

Moodle Themes for 2027, Ranked for Institutions

Eleven free and premium Moodle themes scored for universities, colleges and training providers on 5.3 LTS readiness, upgrade safety, accessibility, b…

Before you go - get a free LMS audit

Tell us which LMS you use and what is not working. An engineer replies within one business day.

Choose which optional cookies we may use. You can change this at any time from "Cookie preferences" in the footer. Cookie policy · Privacy policy