Why cannot ERPNext connect to database?

Hello,

Suddenly, my ERPNext site stopped working.

When seeing the log, I can see this:

File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 183, in connect
    from frappe.database import Database
ImportError: cannot import name Database

Database is up, I can list tables, make queries and whatever, so it is not problem of it.

Besides. the file site_config.json for the site shows the correct information.

What else can I check?

Thanks
Jaime

Does bench mariadb work?

Was there an update or other event before site stopped working?

Try: bench restart ?

Hello,

this is the output of bench mariadb

frappe@vps [~/frappe-bench]# bench mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 963
Server version: 10.3.13-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [1bd3e0294da19198]>

I can even execute SQL commands.

I have just run bench restart but the problem persists. I have even restarted the server.

I have many other sites hosted by Apache in this server, all connecting to their own database. All work but ERPNext site which is hosted by NGINX. I can be almost sure NGINX is not the problem.

The only fact that occured today was that the VPS was down. Before going down, I could use ERPNext without problems. VPS crashed and when hosting company technical support made it operational again, ERPNext does not load again cause of “Internal Server Error”. By looking at the log files, I saw there is some sort of problem with connection.

I have also tried bench update --path and bench update and both failed with the same error. I have used bench build and it finished correctly, but the problem still persists.

With some of those attempts, this error snapshot was recorded in ~/frappe-bench/sites/my.site/error-snapshots:

{
 "etype": "ImportError",
 "evalue": "ImportError('cannot import name Database',)",
 "exception": {
  "args": "('cannot import name Database',)",
  "message": "'cannot import name Database'"
 },
 "frames": [
  {
   "call": "(request=<Request 'http://my.site/favicon.ico' [GET]>)",
   "dump": {
    "global init_request": "<function init_request>",
    "request": "<Request 'http://my.site/favicon.ico' [GET]>"
   },
   "file": "/home/frappe/frappe-bench/apps/frappe/frappe/app.py",
   "func": "application",
   "lines": {
    "56": "\t\trollback = True",
    "57": "",
    "58": "\t\tinit_request(request)",
    "59": "",
    "60": "\t\tif frappe.local.form_dict.cmd:"
   },
   "lnum": 58
  },
  {
   "call": "(request=<Request 'http://my.site/favicon.ico' [GET]>)",
   "dump": {
    "frappe. auth": "<module 'frappe.auth' from '/home/frappe/frappe-bench/apps/frappe/frappe/auth.pyc'>",
    "frappe. local": "<werkzeug.local.Local object>",
    "frappe.auth. HTTPRequest": "<class frappe.auth.HTTPRequest>",
    "frappe.local.http_request": "undefined",
    "global frappe": "<module 'frappe' from '/home/frappe/frappe-bench/apps/frappe/frappe/__init__.pyc'>"
   },
   "file": "/home/frappe/frappe-bench/apps/frappe/frappe/app.py",
   "func": "init_request",
   "lines": {
    "118": "\tmake_form_dict(request)",
    "119": "",
    "120": "\tfrappe.local.http_request = frappe.auth.HTTPRequest()",
    "121": "",
    "122": "def make_form_dict(request):"
   },
   "lnum": 120
  },

after that content, some other content in the file, and at the end, this was recorded:

{
   "call": "(site=None, db_name=None)",
   "dump": {
    "Database": "undefined",
    "frappe": "undefined"
   },
   "file": "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py",
   "func": "connect",
   "lines": {
    "181": "\t:param site: If site is given, calls `frappe.init`.",
    "182": "\t:param db_name: Optional. Will use from `site_config.json`.\"\"\"",
    "183": "\tfrom frappe.database import Database",
    "184": "\tif site:",
    "185": "\t\tinit(site)"
   },
   "lnum": 183
  }
 ],
 "locals": {
  "db_name": "None",
  "site": "None"
 },
 "pyver": "Python 2.7.5: /home/frappe/frappe-bench/env/bin/python (prefix: /home/frappe/frappe-bench/env)",
 "timestamp": "2020-07-21 10:31:43.043987",
 "traceback": "Traceback (most recent call last):\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/app.py\", line 58, in application\n    init_request(request)\n  File \"/home/frappe/frappe-bench/apps/fra$
}

Finally, this was recorded in frappe.log file:

[ERROR] 2020-07-21 14:31:57,915 | /home/frappe/frappe-bench/apps/frappe/frappe/utils/scheduler.py:
Site: my.site
Exception in Enqueue Events for Site my.site
Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/scheduler.py", line 86, in enqueue_events_for_site
    frappe.connect()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 183, in connect
    from frappe.database import Database
ImportError: cannot import name Database

I am really lost with this.

Any further help will be greatly appreciated.

Regards
Jaime

Are memory and disk space on the VM sufficient? A bench migrate might give some other messages that could help troubleshoot.

Any bench command shows the same error:

frappe@vps [~/frappe-bench]# bench migrate
Migrating my.site
Traceback (most recent call last):
  File "/usr/lib64/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 97, in <module>
    main()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 18, in main
    click.Group(commands=commands)(prog_name='bench')
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 25, in _func
    ret = f(frappe._dict(ctx.obj), *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/commands/site.py", line 225, in migrate
    frappe.connect()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 183, in connect
    from frappe.database import Database
ImportError: cannot import name Database
frappe@vps [~/frappe-bench]#

This is the output of df command:

frappe@vps [~/frappe-bench]# df -h
Filesystem         Size  Used Avail Use% Mounted on
/dev/ploop13242p1  148G   84G   57G  60% /
devtmpfs           2.0G     0  2.0G   0% /dev
tmpfs              2.0G  4.0K  2.0G   1% /dev/shm
tmpfs              2.0G  500K  2.0G   1% /run
tmpfs              2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs              410M     0  410M   0% /run/user/0
frappe@vps [~/frappe-bench]#

And this is the memory:

frappe@vps [~/frappe-bench]# free -m
              total        used        free      shared  buff/cache   available
Mem:           4096        2409           0        1230        1686        1495
Swap:          4096         224        3871

And this is more memory detail:

frappe@vps [~/frappe-bench]# vmstat -s
      4194304 K total memory
      2460516 K used memory
      2311556 K active memory
      1551908 K inactive memory
            0 K free memory
            0 K buffer memory
      1733788 K swap cache
      4194304 K total swap
       229412 K used swap
      3964892 K free swap
        19969 non-nice user cpu ticks
          111 nice user cpu ticks
         7368 system cpu ticks
       109256 idle cpu ticks
          434 IO-wait cpu ticks
            0 IRQ cpu ticks
            0 softirq cpu ticks
            0 stolen cpu ticks
      2132572 pages paged in
        64928 pages paged out
         2991 pages swapped in
        62477 pages swapped out
            0 interrupts
      1186104 CPU context switches
   1595357425 boot time
         6132 forks
frappe@vps [~/frappe-bench]#

As I told, all worked perfectly until today, before the VPS crashed. And even, now all my sites in Apache work. The only site that does not work is ERPNext.

Regards
Jaime

bench update --requirements perhaps, again to see if a message with more info can be obtained.

Same error. All bench commands cause a site backup to be performed in the beginning. In that process, the error is shown:

frappe@vps [~/frappe-bench]# bench update --requirements
Backing up sites...
Traceback (most recent call last):
  File "/usr/lib64/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 97, in <module>
    main()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 18, in main
    click.Group(commands=commands)(prog_name='bench')
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/frappe/frappe-bench/env/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 25, in _func
    ret = f(frappe._dict(ctx.obj), *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/commands/site.py", line 301, in backup
    frappe.connect()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 183, in connect
    from frappe.database import Database
ImportError: cannot import name Database
frappe@vps [~/frappe-bench]#

Is the database server on the same VM as ERPNext? Did the crash result in a change to db_name , host_name or something that affects site_config.json? Seems that bench mariadb should not work if that were the case.

Since bench mariadb works, that is not the case.

The database is on the same server. Database name is 1bd3e0294da19198, as reported by that command. Besides that, if I connect to the database using phpMyAdmin interface, I can check that the name is the same.

This is the content of the configuration file:

frappe@vps [~/frappe-bench/sites/my.site]# cat site_config.json
{
 "db_name": "1bd3e0294da19198",
 "db_password": "UYSO4SoOyou8R33y",
 "encryption_key": "VRdHuMl2r-OEoHL_xnhFxXE5BMpnxI_lcc9i7EgmjnU=",
 "host_name": "https://my.site.com",
 "limits": {
  "space_usage": {
   "backup_size": 23.0,
   "database_size": 83.01,
   "files_size": 35.0,
   "total": 141.01
  }
 }
}frappe@vps [~/frappe-bench/sites/my.site]#

May those limits affect the database connection? how can I check that?

Maybe try changing to https://ip.of.the.server if the database names agree.

host_name setting is to be used in e-mails links. That has nothing to do with database.

Look at the https part. That is the URL that only a browser can understand.

Regards
Jaime

May we know the version of bench / ERPNext and the OS on the VM? It may be of use to others who can help more…

frappe@vps [~/frappe-bench]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
frappe@vps [~/frappe-bench]# bench --version
5.0.0
frappe@vps [~/frappe-bench]# bench version
erpnext 11.1.56
frappe 11.1.52
frappe@vps [~/frappe-bench]#

Do you know what this instruction does? At this point of __init__.py file execution is stopping:

from frappe.database import Database

Hello, again…

I decided to reinstall ERP Next. I did it, I changed branch to version-11, however, when I load the site and choose “about” option, this version is reported:

Frappe Framework:** v13.x.x-develop () (develop)

How can I downgrade?

Any other advice?
Thanks

Hello,

Just to let you know that I could finally install version-11-hotfix branch, and after I checked web site was up, I set previous database name and password in site_config.json file. Finally, I executed bench update --reset and the system was finally recovered.

Regards
Jaime