Mail: DbalException "Duplicate entry for key 'mail_mb_account_id_name_hash'"

The Basics

  • Nextcloud Server version (e.g., 29.x.x):
    • 31.0.6
  • Operating system and version (e.g., Ubuntu 24.04):
    • CloudLinux 1.3
  • Web server and version (e.g, Apache 2.4.25):
    • LiteSpeed, unknown version
  • Reverse proxy and version _(e.g. nginx 1.27.2)
    • n/a
  • PHP version (e.g, 8.3):
    • 8.3.22
  • Is this the first time you’ve seen this error? (Yes / No):
    • yes
  • When did this problem seem to first start?
    • 2025-06-26T01:55:03+00:00
  • Installation method (e.g. AlO, NCP, Bare Metal/Archive, etc.)
    • Bare Metal
  • Are you using CloudfIare, mod_security, or similar? (Yes / No)
    • No

Summary of the issue you are facing:

Nextcloud Mail stopped working for 3 out of 4 users. According to the logs, 4 out of 6 mail accounts seem to be affected.

Steps to replicate it (hint: details matter!):

The first time this error occured was triggered by a cron job in the middle of the night. I have definitely not touched anything within 6 hours before, and previous cron jobs ran fine. Additionally, 4 mail accounts from 3 different users started being affected at the same time. 2 of these users barely use Nextcloud Mail at all.

Log entries

Nextcloud

Example (the same message appears also for 3 other account_id/name_hash combinations, repeated about every 20 minutes):

DbalException
An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5-e5ecf6489ff48b8032{redacted}' for key 'mail_mb_account_id_name_hash'
Trace
/home/{redacted}/lib/private/DB/ConnectionAdapter.php
Line 69
OC\DB\Exceptions\DbalException::wrap()
/home/{redacted}/lib/private/DB/QueryBuilder/QueryBuilder.php
Line 306
OC\DB\ConnectionAdapter->executeStatement()
/home/{redacted}/lib/public/AppFramework/Db/QBMapper.php
Line 116
OC\DB\QueryBuilder\QueryBuilder->executeStatement()
/home/{redacted}/apps/mail/lib/IMAP/MailboxSync.php
Line 244
OCP\AppFramework\Db\QBMapper->insert()
/home/{redacted}/apps/mail/lib/IMAP/MailboxSync.php
Line 185
OCA\Mail\IMAP\MailboxSync->createMailboxFromFolder(
  "*** sensitive parameters replaced ***"
)
/home/{redacted}/apps/mail/lib/IMAP/MailboxSync.php
Line 127
OCA\Mail\IMAP\MailboxSync->persist()
/home/{redacted}/lib/public/AppFramework/Db/TTransactional.php
Line 45
OCA\Mail\IMAP\MailboxSync->OCA\Mail\IMAP\{closure}(
  "*** sensitive parameters replaced ***"
)
/home/{redacted}/apps/mail/lib/IMAP/MailboxSync.php
Line 118
OCA\Mail\IMAP\MailboxSync->atomic()
/home/{redacted}/apps/mail/lib/BackgroundJob/SyncJob.php
Line 92
OCA\Mail\IMAP\MailboxSync->sync(
  "*** sensitive parameters replaced ***"
)
/home/{redacted}/lib/public/BackgroundJob/Job.php
Line 61
OCA\Mail\BackgroundJob\SyncJob->run()
/home/{redacted}/lib/public/BackgroundJob/TimedJob.php
Line 97
OCP\BackgroundJob\Job->start()
/home/{redacted}/lib/public/BackgroundJob/TimedJob.php
Line 84
OCP\BackgroundJob\TimedJob->start()
/home/{redacted}/cron.php
Line 170
OCP\BackgroundJob\TimedJob->execute()

Web Browser

n/a

Web server / Reverse Proxy

n/a

Configuration

Nextcloud

The output of occ config:list system or similar is best, but, if not possible, the contents of your config.php file from /path/to/nextcloud is fine (make sure to remove any identifiable information!):

occ config:list system
{
    "system": {
        "instanceid": "***REMOVED SENSITIVE VALUE***",
        "passwordsalt": "***REMOVED SENSITIVE VALUE***",
        "secret": "***REMOVED SENSITIVE VALUE***",
        "trusted_domains": [
            "***REMOVED SENSITIVE VALUE***"
        ],
        "datadirectory": "***REMOVED SENSITIVE VALUE***",
        "dbtype": "mysql",
        "version": "31.0.6.2",
        "overwrite.cli.url": "***REMOVED SENSITIVE VALUE***",
        "default_language": "de",
        "default_locale": "de_DE",
        "default_phone_region": "DE",
        "dbname": "***REMOVED SENSITIVE VALUE***",
        "dbhost": "***REMOVED SENSITIVE VALUE***",
        "dbport": "",
        "dbtableprefix": "oc_",
        "dbuser": "***REMOVED SENSITIVE VALUE***",
        "dbpassword": "***REMOVED SENSITIVE VALUE***",
        "htaccess.RewriteBase": "\/",
        "installed": true,
        "mysql.utf8mb4": true,
        "mail_from_address": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpmode": "smtp",
        "mail_sendmailmode": "smtp",
        "mail_domain": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpauth": 1,
        "mail_smtphost": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpport": "465",
        "mail_smtpname": "***REMOVED SENSITIVE VALUE***",
        "mail_smtppassword": "***REMOVED SENSITIVE VALUE***",
        "maintenance": false,
        "maintenance_window_start": 1,
        "memcache.local": "\\OC\\Memcache\\APCu",
        "twofactor_enforced": "true",
        "twofactor_enforced_groups": [
            "admin"
        ],
        "twofactor_enforced_excluded_groups": [],
        "oidc_login_provider_url": "***REMOVED SENSITIVE VALUE***",
        "oidc_login_client_id": "***REMOVED SENSITIVE VALUE***",
        "oidc_login_client_secret": "***REMOVED SENSITIVE VALUE***",
        "oidc_login_auto_redirect": false,
        "oidc_login_button_text": "Log in with Authelia",
        "oidc_login_hide_password_form": false,
        "oidc_login_use_id_token": true,
        "oidc_login_attributes": {
            "id": "preferred_username",
            "name": "name",
            "mail": "email"
        },
        "oidc_login_default_group": "oidc",
        "oidc_login_scope": "openid profile email",
        "oidc_login_code_challenge_method": "S256",
        "theme": "",
        "loglevel": 2
    }
}

Apps

The output of occ app:list (if possible).

occ app:list

Enabled:

  • activity: 4.0.0
  • app_api: 5.0.2
  • bruteforcesettings: 4.0.0
  • calendar: 5.3.3
  • circles: 31.0.0
  • cloud_federation_api: 1.14.0
  • comments: 1.21.0
  • contacts: 7.1.3
  • cospend: 3.0.11
  • dav: 1.33.0
  • deck: 1.15.1
  • federatedfilesharing: 1.21.0
  • federation: 1.21.0
  • files: 2.3.1
  • files_downloadlimit: 4.0.0
  • files_pdfviewer: 4.0.0
  • files_reminders: 1.4.0
  • files_retention: 2.0.1
  • files_sharing: 1.23.1
  • files_trashbin: 1.21.0
  • files_versions: 1.24.0
  • firstrunwizard: 4.0.0
  • logreader: 4.0.0
  • lookup_server_connector: 1.19.0
  • mail: 5.1.4
  • nextcloud_announcements: 3.0.0
  • notes: 4.12.1
  • notifications: 4.0.0
  • oauth2: 1.19.1
  • oidc_login: 3.2.2
  • password_policy: 3.0.0
  • privacy: 3.0.0
  • profile: 1.0.0
  • provisioning_api: 1.21.0
  • recommendations: 4.0.0
  • registration: 2.7.0
  • related_resources: 2.0.0
  • serverinfo: 3.0.0
  • settings: 1.14.0
  • sharebymail: 1.21.0
  • support: 3.0.0
  • survey_client: 3.0.0
  • systemtags: 1.21.1
  • text: 5.0.0
  • theming: 2.6.1
  • twofactor_backupcodes: 1.20.0
  • twofactor_totp: 13.0.0-dev.0
  • updatenotification: 1.21.0
  • user_status: 1.11.0
  • viewer: 4.0.0
  • weather_status: 1.11.0
  • webhook_listeners: 1.2.0
  • workflowengine: 2.13.0

Disabled:

  • admin_audit: 1.21.0
  • contactsinteraction: 1.12.0 (installed 1.11.0)
  • dashboard: 7.11.0 (installed 7.10.0)
  • encryption: 2.19.0
  • files_external: 1.23.0
  • photos: 4.0.0-dev.1 (installed 3.0.2)
  • suspicious_login: 9.0.1
  • twofactor_nextcloud_notification: 5.0.0
  • user_ldap: 1.22.0

Do you have access to the DB? What DB and version is it?

Go into the “mail-mb-account-id-name” table and look at the ID’s see if by chance there is a repeated one? That field may bey a key index and that would break the DB.

1 Like

There’s no table with such a name.

The index “mail_mb_account_id_name_hash” belongs to oc_mail_mailboxes. The purpose is to ensure there are no folders/mailboxes with the same name for one email account.

Does one of your email accounts has folders wit hthe same name?

I do have access to the DB and to all affected Mailboxes.
The index “mail_mb_account_id_name_hash” consist of two fields: account_id (internal unique ID of the mail account in the mail-app) and name_hash (MD5 Hash of the mail folder, e.g. md5(“INBOX.Archive.Kinderkram.Bücherhalle”)=c6a8eb68996d02477a80a984f2f8c7dd.

As the IMAP server would not allow two folders with the same path on the same account, the answer is: No, no account does have multiple folders with the same name.

However, two things I have just noticed:

  1. Two of the app accounts actually refer to the same mail account (two NC users sharing the same IMAP account), so in their case the folders have the same name, but of course a differnt account_id. Because of that I don’t think that’s an issue and I don’t get errors on all the other folders that appear in both app accounts.
  2. All affected folders contain the German umlaut ü in their name! One of them is located in the “shared” mail account and affected in both app accounts.

Digging deeper into the NC logs I also found these two entries almost as often as the DbalException:
JsonException Malformed UTF-8 characters, possibly incorrectly encoded
TypeError md5(): Argument #1 ($string) must be of type string, false given

Raw entries
{"reqId":"doS6Zje8y4DLJAj789W5","level":3,"time":"2025-06-26T02:15:37+00:00","remoteAddr":"87.177.15.21","user":"{redacted}","app":"index","method":"GET","url":"/index.php/apps/deck/api/v1.0/boards?details=true","message":"Malformed UTF-8 characters, possibly incorrectly encoded","userAgent":"Mozilla/5.0 (Android) Nextcloud-android/3.31.4","version":"31.0.6.2","exception":{"Exception":"JsonException","Message":"Malformed UTF-8 characters, possibly incorrectly encoded","Code":5,"Trace":[{"file":"/home/{redacted}/lib/public/AppFramework/Http/JSONResponse.php","line":63,"function":"json_encode"},{"file":"/home/moritzge/geisberger.cloud/lib/private/AppFramework/Http/Dispatcher.php","line":159,"function":"render","class":"OCP\\AppFramework\\Http\\JSONResponse","type":"->"},{"file":"/home/{redacted}/lib/private/AppFramework/App.php","line":161,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/home/{redacted}/lib/private/Route/Router.php","line":307,"function":"main","class":"OC\\AppFramework\\App","type":"::"},{"file":"/home/{redacted}/lib/base.php","line":1040,"function":"match","class":"OC\\Route\\Router","type":"->"},{"file":"/home/{redacted}/index.php","line":24,"function":"handleRequest","class":"OC","type":"::"}],"File":"/home/{redacted}/lib/public/AppFramework/Http/JSONResponse.php","Line":63,"message":"Malformed UTF-8 characters, possibly incorrectly encoded","exception":{},"CustomMessage":"Malformed UTF-8 characters, possibly incorrectly encoded"}}
{"reqId":"DkzxtOfGVuEsm2Qn2aXz","level":3,"time":"2025-06-26T08:21:38+00:00","remoteAddr":"176.4.16.9","user":"{redacted}","app":"notes","method":"GET","url":"/index.php/apps/notes/api/v1/notes?pruneBefore=0","message":"Controller failed with TypeError","userAgent":"Mozilla/5.0 (Android) Nextcloud-android/3.31.4","version":"31.0.6.2","exception":{"Exception":"TypeError","Message":"md5(): Argument #1 ($string) must be of type string, false given","Code":0,"Trace":[{"file":"/home/{redacted}/apps/notes/lib/Service/MetaService.php","line":215,"function":"md5"},{"file":"/home/{redacted}/apps/notes/lib/Service/MetaService.php","line":181,"function":"generateEtag","class":"OCA\\Notes\\Service\\MetaService","type":"->"},{"file":"/home/{redacted}/apps/notes/lib/Service/MetaService.php","line":94,"function":"updateIfNeeded","class":"OCA\\Notes\\Service\\MetaService","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/home/{redacted}/apps/notes/lib/Controller/Helper.php","line":83,"function":"getAll","class":"OCA\\Notes\\Service\\MetaService","type":"->"},{"file":"/home/{redacted}/apps/notes/lib/Controller/NotesApiController.php","line":69,"function":"getNotesAndCategories","class":"OCA\\Notes\\Controller\\Helper","type":"->"},{"file":"/home/{redacted}/apps/notes/lib/Service/Util.php","line":31,"function":"OCA\\Notes\\Controller\\{closure}","class":"OCA\\Notes\\Controller\\NotesApiController","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/home/{redacted}/apps/notes/lib/Controller/Helper.php","line":146,"function":"retryIfLocked","class":"OCA\\Notes\\Service\\Util","type":"::"},{"file":"/home/{redacted}/apps/notes/lib/Controller/NotesApiController.php","line":57,"function":"handleErrorResponse","class":"OCA\\Notes\\Controller\\Helper","type":"->"},{"file":"/home/{redacted}/lib/private/AppFramework/Http/Dispatcher.php","line":200,"function":"index","class":"OCA\\Notes\\Controller\\NotesApiController","type":"->"},{"file":"/home/{redacted}/lib/private/AppFramework/Http/Dispatcher.php","line":114,"function":"executeController","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/home/{redacted}/lib/private/AppFramework/App.php","line":161,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/home/{redacted}/lib/private/Route/Router.php","line":307,"function":"main","class":"OC\\AppFramework\\App","type":"::"},{"file":"/home/{redacted}/lib/base.php","line":1040,"function":"match","class":"OC\\Route\\Router","type":"->"},{"file":"/home/{redacted}/index.php","line":24,"function":"handleRequest","class":"OC","type":"::"}],"File":"/home/{redacted}/apps/notes/lib/Service/MetaService.php","Line":215,"message":"Controller failed with TypeError","exception":{},"CustomMessage":"Controller failed with TypeError"}}

Initially I didn’t pay much attention on them because they seemed to be raised by the deck and the notes app (that both stopped working, too - but I wanted to take care of the mail app first), but that might be misleading?

Additional remarks:
Over all app accounts I do have 5 additional folders with ü, ä or ß in their names - none of them causing any log entries.
All folders have been present since I set up my NC and the mail app about three months ago. I have not created any mail folders ever since. Last time I have really changed something intentionally was one week before the errors started when I updated my NC and the mail app to the latest version, which succeeded without any errors.
The IMAP server (all accounts are on the same server) is not under my control, so I can’t tell whether something happened on this end.

Any ideas how to proceed?
I could rename the affected folders on the IMAP server, would that cause some re-synchronisation that could fix the issues? Or do I have to delete and recreate the app accounts? If yes, should I try doing so on the web ui or using the occ command?

Short update: It seems the error somehow fixed itself :man_shrugging:

I kept receiving the error messages until Friday (2025-06-27 09:05), but then it stopped and now my NC is completely working again :slight_smile:

I will check with my hosting provider if they did something, because I did not do anything (apart from stopping to use the NC mail app because I couldn’t use it anyway).

edit: According to my hosting provider, the errors were caused by a faulty update of the PHP extensions mysqli / pdo_mysqli. Their full reply is unfortunately only available in German and does not really contain more details:

Full reply

Aktuell gibt es vermehrt Fehlermeldungen durch Kunden, die auf falsch dargestellte Umlaute, bzw. Fehler beim Aufbau einer Datenbankverbindung hinweisen.

Leider scheint es bei einem automatischen Update heute Nacht einen Fehler seitens des Upstream Providers gegeben zu haben.

Dieses Problem kann man in den meisten Fehlern kurzfristig beheben indem man im PHP Selector “mysqli” und “pdo_mysqli” deaktiviert/abhakt und dann “nd_mysqli” und “nd_pdo_mysqli” aktiviert/anhakt.

Sie können im Controlpanel wie folgt auf den PHP Selector zugreifen:

  1. Melden Sie sich im ControlPanel an.
  2. Scrollen Sie bis zum Punkt “Software” runter.
  3. Wählen Sie den Punkt “PHP-Version wählen” aus.
  4. “mysqli” und “pdo_mysqli” deaktivieren/abhaken
  5. “nd_mysqli” und “nd_pdo_mysqli” aktivieren/anhaken

Update 17:45 Uhr:
Wir haben aktuell einen Workaround aktiviert, der sicherstellt, dass die mysqli -Extension wieder einwandfrei funktioniert.
Sollten Sie in der Zwischenzeit auf nd_mysqli umgestellt haben und weiterhin Probleme feststellen, können Sie problemlos wieder auf mysqli zurückwechseln.

Update 27.06.2025 08:00 Uhr:
Unser Upstream Provider hat einen Hotfix für den Bug bereitgestellt. Wir spielen diesen auf allen Systemen gerade entsprechend ein.

Thank you! at least we got an actual root cause.

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.