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
- 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.
- 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.
- 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). - Database server limits or faults. Lost connections during long queries, timeouts, packet size limits, a full disk or a server that is restarting.
- 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
- 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.
- 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.phpdocuments this setting as a list of user ids that "always see debug messages". Replace2with 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. - 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_itemsbelongs tolocal_example). The stack trace shows which plugin file called the query. - 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. - Compare the schema. Run
php admin/cli/check_database_schema.phpas the web server user. It compares the live database with every component'sinstall.xmland prints "Database structure is ok." or a list of differences per table. - 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.
- 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. - 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 says | Likely cause | Go to fix |
|---|---|---|
| Table does not exist, unknown column | Code and database out of step | 1 |
| Syntax error, invalid GROUP BY, function does not exist | Plugin or custom query | 2 |
| Number of parameters out of range, packet too large | Query too large | 3 |
| Server has gone away, connection lost, timeout | Server limits or faults | 4 |
| Illegal mix of collations | Collation mismatch | 5 |
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.phponce more and expect "Database structure is ok.". - Turn "Debug messages" back to its normal level, turn "Display debug messages" off and remove any
$CFG->debugusersline.
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.phpdocuments alogerroroption in$CFG->dboptionsthat stores erroneous queries in thelog_queriestable. 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
- Debugging - Moodle
- DML exceptions - Moodle HQ
- lib/dmllib.php (Moodle 5.3.0) - Moodle HQ
- config-dist.php (Moodle 5.3.0) - Moodle HQ
- admin/cli/check_database_schema.php (Moodle 5.3.0) - Moodle HQ
- Moodle 5.2.2 release notes (MDL-87395) - Moodle HQ
Learn more about this topic with AI
Gemini opens with the prompt copied - paste it into the chat to start.