When cron is running, Moodle displays database error

When cron is running, Moodle displays database error

by Nicholas R. Donohue -
Number of replies: 8

Hello everyone!

I recently came to be the admin of an existing Moodle installation. While checking the site handed to me, I noticed that cron hadn't run in a *very* long time. The site has been updated twice(!) while cron was not running, too.

I added cron to the crontab on the server and let it run manually in a console. It took about a minute to finish everything and send out emails.

At first, I thought everything was fine, but soon I noticed that Moodle would display a database error whenever cron had been run recently. Then it would take about a minute for Moodle to spring back to life without any intervention. 

In case of admin/index.php?cache=0, the he error shown would be this:

Fehler beim Lesen der Datenbank
Debug info: Lost connection to MySQL server during query
SELECT * FROM mdl_context WHERE contextlevel = ?
[array (
0 => 10,
)]
Error code: dmlreadexception
Stack trace:
  • line 486 of /lib/dml/moodle_database.php: dml_read_exception thrown
  • line 1273 of /lib/dml/mysqli_native_moodle_database.php: call to moodle_database->query_end()
  • line 1671 of /lib/dml/moodle_database.php: call to mysqli_native_moodle_database->get_records_sql()
  • line 1643 of /lib/dml/moodle_database.php: call to moodle_database->get_record_sql()
  • line 1622 of /lib/dml/moodle_database.php: call to moodle_database->get_record_select()
  • line 6308 of /lib/accesslib.php: call to moodle_database->get_record()
  • line 758 of /lib/setup.php: call to context_system::instance()
  • line 27 of /config.php: call to require_once()
  • line 88 of /admin/index.php: call to require()
I also got the following error, when I change the cache parameter from 1 to 0:
Error reading from database
Coding error detected, it must be fixed by a programmer: block_manager has not yet loaded the blocks, to it is too soon to request the information you asked for.
Afterwards, the cron job would often print an error message regarding the database, also executing this query:
SELECT * FROM mdl_context WHERE contextlevel = ?
[array (
0 => 10,
)]
What could cause such misery? Is the database corrupted due to the cron not running for so long or could it be something else?

We are using Moodle 3.10.3+ (Build: 20210430) with MariaDB.


Average of ratings: -
In reply to Nicholas R. Donohue

Re: When cron is running, Moodle displays database error

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
"Lost connection" generally means that MySQL has failed in some way. Find/check the error logs for MySQL
In reply to Howard Miller

Re: When cron is running, Moodle displays database error

by Nicholas R. Donohue -
This Moodle instance is installed on the Server of a Webhosting Service, where I'm not able to look into the logs.
In reply to Nicholas R. Donohue

Re: When cron is running, Moodle displays database error

by Ken Task -
Picture of Particularly helpful Moodlers

Pardon intrusion ... @Nicholas ... you might want to share more info about 'a webhosting service'.    Is site on shared hosting?  Do you have access to a 'cPanel'? (that does show brief info about a shared DB server).   If using a shared hosted DB server with site, that can be seen in config.php of the code of moodle.

On shared hosting, an admin person is greatly limited as to what one can do.

Site may have grown beyond the limits of shared hosting.

'SoS', Ken

In reply to Ken Task

Re: When cron is running, Moodle displays database error

by Nicholas R. Donohue -
I'm grateful for any help!

Yes, the site is on shared hosting, but I can't get access to a cPanel unfortunately, as I'm only supposed to "manage it" and don't have access to the server configuration, contractual data, and so on, sigh!

I don't think it's an overload of the database, because of the following leads:
- without a working cron, the site was able to handle the traffic just fine and yesterday nobody except me was online
- it's always this one query, that results in an error: SELECT * FROM mdl_context WHERE contextlevel = ?
[array (
0 => 10,
)]

I think the database contents is somehow corrupted – probably because of cron not running for so long. Is there a "repair everything" function may?
In reply to Nicholas R. Donohue

Re: When cron is running, Moodle displays database error

by Ken Task -
Picture of Particularly helpful Moodlers

Sounds like you've been given 'mission impossible' - Moodle admin interface doesn't admin the server OS, nor config of DB server, nor anything else but moodle code ... it's just an app!

Yes, it's a good guess that cron job/add_hoc_task jobs (those cron jobs that didn't finish for some reason now in a que to be tried again ... picking up where it left off ... until completed).

Since you have only moodle admin access and no other tools, suggest setting the path to php-cli.

admin/settings.php?section=systempaths 

Then, go to admin/settings.php?section=sitepolicies and make sure 'allow run now' is checked.

when one goes to admin/tasklogs.php one can see columns with info about running of whatever.

when one goes to admin/tool/task/scheduledtasks.php you will be able to see 'run now' links for each that are active.

See which ones failed and try to run 'em.

Moodle has no admin tool to repair a DB ... mysql client from cli does.

Got phpmyadmin access ... could have repair there.

That table:

mysql> explain mdl_context;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | bigint(10)   | NO   | PRI | NULL    | auto_increment |
| contextlevel | bigint(10)   | NO   | MUL | 0       |                |
| instanceid   | bigint(10)   | NO   | MUL | 0       |                |
| path         | varchar(255) | YES  | MUL | NULL    |                |
| depth        | tinyint(2)   | NO   |     | 0       |                |
| locked       | tinyint(2)   | NO   |     | 0       |                |
+--------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

If you cannot check other server settings/error logs, then ????

Good luck!

'SoS', Ken

In reply to Ken Task

Re: When cron is running, Moodle displays database error

by Nicholas R. Donohue -
Thanks for these pointers! I investigated and it seems like ALL tasks run flawless.

I think the error appears even before any tasks are run at all. I guess I'll have to talk to the hosting service. They did have some outages of services a few month ago, maybe this is related/the same and the database just dies(?). What keeps me wondering, is that it started to happen right about I started the cron schedule again.
In reply to Nicholas R. Donohue

Re: When cron is running, Moodle displays database error

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Then you need to get your host involved. You're highly unlikely to fix this from the Moodle side.
In reply to Howard Miller

Re: When cron is running, Moodle displays database error

by Nicholas R. Donohue -
Thanks, I will contact them. At at loss, what else I could do.