{solved}. New error
Heroku log error:
Error: connect ECONNREFUSED 127.0.0.1:3306 2021-09-23T18:24:12.236657+00:00 app[web.1]: at TCPConnectWrap.afterConnect. [as oncomplete] (node:net:1146:16) { 2021-09-23T18:24:12.236658+00:00 app[web.1]: errno: -111, 2021-09-23T18:24:12.236658+00:00 app[web.1]: code: 'ECONNREFUSED', 2021-09-23T18:24:12.236658+00:00 app[web.1]: syscall: 'connect', 2021-09-23T18:24:12.236659+00:00 app[web.1]: address: '127.0.0.1', 2021-09-23T18:24:12.236659+00:00 app[web.1]: port: 3306, 2021-09-23T18:24:12.236659+00:00 app[web.1]: fatal: true
Information/Background:
React js frontend (now hosted on Netlifty) Javascript Node backend using Express and MYSQL2 (hosted on Heroku)
Target: Connect Neflifty frontend POST request with Heroku backend, get POST payload data and insert it into MYSQL table.
Updated: September 24, 2021
I've done everything suggested. I have created a new database using clearDB. Add it and test the connection in mysql Workbench. The required tables are created. Updated backend code for creating connections to new databases. Check the Heroko variables and make sure they are correctly reflected in the new database. Now there is an authorization issue. {solved}
Corrected new backend code:
const express = require('express'); const app = express(); const port = process.env.Port || 8000 app.list en(port); console.log(`server is listing on ${port}`);time out
question: If this connects locally on the workbench, why won't Heroku connect if I add them as variables? {Answered}
New question: Why is it trying to use port 8000 when it should be using the environment? Why does it time out?
Any help with this would be greatly appreciated.
P粉6100288412023-12-14 09:46:11
You cannot connect to the database because there is no database instance running on Heroku. Once you push code using the Heroku CLI, Heroku will set up a NodeJS application for you, but that doesn't mean it will also set up a database for you.
You can add a database to your application through Heroku's interface or CLI. From the CLI (we'll be setting up ClearDB, but any MySQL database plugin will work):
heroku addons:create cleardb:ignite
Once completed, you want the new database URL (not localhost):
heroku config | grep CLEARDB_DATABASE_URL
The output of the last command will be similar to:
mysql://: @ / ?reconnect=true
Now, with this, you should modify your code slightly with the new information. You don't want to expose the database credentials in version control, so you can do it using environment variables:
const db = mysql.createConnection({
connectionaLimit: 50,
user: process.env.DB_USER,
host: process.env.DB_HOST,
password: process.env.DB_PASSWORD,
database: process.env.DATABASE,
port: 3306
});
Additionally, you need to set environment variables for your running Heroku application:
heroku config:set DB_USER=heroku config:set DB_PASSWORD= heroku config:set DB_HOST= heroku config:set DATABASE=
Now you have a database instance running on Heroku and a NodeJS application instance that can connect to the database.
For further reading, you may want to check out these links:
https:// /lo-victoria.com/build-a-mysql-nodejs-crud-app-4-deploying-to-heroku-finale
https://www.bezkoder.com/deployment-node-js-app-heroku-cleardb-mysql/
https://raddy.co.uk/blog/how-to-deploy-node-js-express-ejs-mysql-website-on-heroku-cleardb/(This uses Heroku interface)< /p>