Connecting your Node.js app to its database

If your application shows "We're sorry, but something went wrong" and the log reports ER_ACCESS_DENIED_ERROR, the app is still pointing at database credentials from the server it was built on.

Why this happens

Every cPanel account has a prefix, and all database names and database users must begin with it. Your prefix is your cPanel username followed by an underscore. In this guide we write it as youruser_ — replace it with your own.

ER_ACCESS_DENIED_ERROR: Access denied for user 'olduser_appdb'@'localhost' (using password: YES)

Here the app asks for olduser_appdb, a user belonging to the account it was built on. That user does not exist on this server, so every query fails and the application cannot start.

Step 1 — Create the database and user

In cPanel open MySQL® Databases and complete all three sections:

  1. Create New Database — enter appdb. It is created as youruser_appdb.
  2. Add New User — enter appdb, set a strong password, and save it for Step 3.
  3. Add User To Database — select the user and the database, click Add, tick ALL PRIVILEGES, then Make Changes.

Important: Step 3 is the one most people miss. Creating a user and creating a database does not connect them — without it you will still get "Access denied", even with the correct password.

Step 2 — Import your existing data

Open phpMyAdmin, select youruser_appdb in the left sidebar, choose the Import tab and upload your .sql file. For files larger than the upload limit, upload the .sql to your home directory with File Manager, then run from cPanel Terminal:

mysql -p -u youruser_appdb youruser_appdb < ~/backup.sql

Step 3 — Update your application configuration

Edit your .env file (or config.js / database.js — wherever your connection settings live):

# WRONG - credentials from the previous server
DB_USER=olduser_appdb
DB_NAME=olduser_appdb

# CORRECT - your own account's prefix
DB_HOST=localhost
DB_PORT=3306
DB_NAME=youruser_appdb
DB_USER=youruser_appdb
DB_PASSWORD=the-password-you-set-in-step-1

Check the whole file for other references to the old server — connection strings, seed scripts and migration configs often carry them too.

Step 4 — Check your Node.js application settings

Open Setup Node.js App in cPanel and confirm:

  • Node.js version — the version your app requires
  • Application mode — Production
  • Application root — the folder containing package.json, e.g. your-app
  • Application URL — the domain or subdomain the app serves
  • Application startup file — e.g. app.js, server.js or index.js

You may instead add your database values here as Environment variables — either approach works, but do not split them across both.

Step 5 — Install dependencies and restart

On the same screen click Run NPM Install, wait for it to finish, then click Restart.

A restart is mandatory after any configuration change. Node keeps its database connections open in memory, so an edited .env has no effect until the app restarts. This is the most common reason a correct fix appears not to work.

From the Terminal instead:

cd ~/your-app
npm install
touch tmp/restart.txt

Step 6 — Verify

Confirm the credentials work independently of your app:

mysql -u youruser_appdb -p youruser_appdb -e "SHOW TABLES;"

If your tables are listed, the database side is correct. If the app still errors, read the log — it names the exact problem:

ls -lt ~/logs/ | head
tail -40 ~/your-app/stderr.log

Troubleshooting

  • ER_ACCESS_DENIED_ERROR — wrong username, wrong password, or the user was never added to the database. Re-check Step 1, especially Add User To Database with ALL PRIVILEGES.
  • ER_BAD_DB_ERROR / Unknown database — the database name must include your account prefix.
  • ECONNREFUSED 127.0.0.1:3306 — use DB_HOST=localhost and DB_PORT=3306. If it persists, contact support.
  • Passenger: "could not be started" — the app crashed at startup. Check the startup file name, run NPM Install, and read stderr.log.
  • Changes have no effect — the application was not restarted.
  • Cannot find module — dependencies are not installed for the selected Node version. Run NPM Install.

Checklist: database and user both start with your account prefix · user added to the database with ALL PRIVILEGES · .env updated · DB_HOST=localhost · NPM Install run · application restarted.

¿Le ha resultado útil esta respuesta? 0 Los usuarios encontraron esto útil (0 Votos)