I deployed Strapi CMS to Heroku, but I get this error
Error connecting to the Mongo database. Server selection timed out after 30000 ms
Log:
2020-05-27T17:43:55.398256+00:00 heroku[web.1]: Starting process with command `npm start`
2020-05-27T17:43:58.724121+00:00 app[web.1]:
2020-05-27T17:43:58.724143+00:00 app[web.1]: > strapi-oskogen-mongodb#0.1.0 start /app
2020-05-27T17:43:58.724143+00:00 app[web.1]: > strapi start
2020-05-27T17:43:58.724143+00:00 app[web.1]:
2020-05-27T17:44:02.234160+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
2020-05-27T17:44:02.234179+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2020-05-27T17:44:02.234732+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
2020-05-27T17:44:02.234879+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
2020-05-27T17:44:02.235021+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
2020-05-27T17:44:32.238852+00:00 app[web.1]: [2020-05-27T17:44:32.238Z] debug ⛔️ Server wasn't able to start properly.
2020-05-27T17:44:32.253150+00:00 app[web.1]: [2020-05-27T17:44:32.253Z] error Error connecting to the Mongo database. Server selection timed out after 30000 ms
My environments settings:
database.js
** server.js **
** response.js **
** config vars **
Site works well on localhost with both dev and prod environment. So it connects to MongoDB on Atlas and no problem with that.
I do not have any addons installed on Heroku.
** packages.json **
On Atlas side I opened all IPs in white list.
Any idea? Thank you! :)
My configuration for deployment of Strapi 3.0.1 on Heroku, both for develop and production environments:
module.exports = ({ env }) => ({
defaultConnection: "default",
connections: {
default: {
connector: "mongoose",
settings: {
uri: env("DATABASE_URI"),
ssl: { rejectUnauthorized: false }
},
options: {
ssl: true,
authenticationDatabase: "",
useUnifiedTopology: true,
pool: {
min: 0,
max: 10,
idleTimeoutMillis: 30000,
createTimeoutMillis: 30000,
acquireTimeoutMillis: 30000
}
},
},
},
});
server.js
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 443), <-- 443 was critical to make it work on production
});
.env - local file
DATABASE_URI="mongodb+srv://OdegXXXXXUser:OXXXX20#odXXXXcluster-h9o2e.mongodb.net/odeXXXXXndb?retryWrites=true&w=majority"
HOST="0.0.0.0"
PORT="1337"
Vars on Heroku:
DATABASE_URI er identical as on localhost, the same database.
I hope it will help to anybody :-)
make this your database.json
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"client": "mongo",
"host": "${process.env.DATABASE_HOST}",
"port": "${process.env.DATABASE_PORT}",
"database": "${process.env.DATABASE_NAME}",
"username": "${process.env.DATABASE_USERNAME}",
"password": "${process.env.DATABASE_PASSWORD}",
},
"options": {}
}
}
}
Also make sure to add package-lock.json to your git.ignore file.
then do
git add .
git commit -m "(message) "
git push heroku master
then
heroku open
Start your app servers too
Related
I have a custom made docker image for the backend of my app. I have a yaml file that runs my app image and a mongo image. However, when I use docker-compose on the yml file, I get the following error (about 20 seconds and the containers start running):
(node:33) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
Server listening on port 3000
/cloudband/node_modules/mongoose/lib/connection.js:825
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Connection.openUri (/cloudband/node_modules/mongoose/lib/connection.js:825:32)
at /cloudband/node_modules/mongoose/lib/index.js:409:10
at /cloudband/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/cloudband/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/cloudband/node_modules/mongoose/lib/index.js:1262:10)
at Mongoose.connect (/cloudband/node_modules/mongoose/lib/index.js:408:20)
at Object.<anonymous> (/cloudband/server/server.js:15:4)
at Module._compile (node:internal/modules/cjs/loader:1239:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1293:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 28094812,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
at connectionFailureError (/cloudband/node_modules/mongodb/lib/cmap/connect.js:387:20)
at Socket.<anonymous> (/cloudband/node_modules/mongodb/lib/cmap/connect.js:310:22)
at Object.onceWrapper (node:events:628:26)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
cause: Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 27017
},
[Symbol(errorLabels)]: Set(1) { 'ResetPool' }
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
'$clusterTime': null
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
Here are my files:
Dockerfile:
FROM node:19.4.0
WORKDIR /cloudband
COPY package.json /cloudband/
COPY package-lock.json /cloudband/
RUN npm ci
COPY .env /cloudband/
COPY server /cloudband/server/
EXPOSE 3000
CMD ["npm", "run", "dev:server"]
YAML file:
version: '3'
services:
mongo:
image: mongo
container_name: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
cloudband:
image: cloudband
container_name: cloudband
ports:
- 3000:3000
command: npm run dev:server
networks:
app:
I expected my application and mongo db to start running in their respective containers and for them to be able to communicate (i.e. create documents / find documents / etc.).
What I have already tried:
-making sure they are in the same network (they are)
-making sure they can ping each other (they can)
-adding links to my app in the yaml file
-checked configurations and i think they are ok (port, host, ip)
-switching my uri to the following things:
# MONGO_URI_=mongodb://admin:password#localhost:27017/dbname
MONGO_URI_=mongodb://localhost:27017/dbname
# MONGO_URI_=mongodb://127.0.0.1:27017/dbname
Things to consider:
node v18.12.0 is installed on my computer
In a container, localhost means the container itself.
Docker-compose creates a docker network where the containers can talk to each other using their service name or container names as host names.
So, instead of
MONGO_URI_=mongodb://localhost:27017/dbname
you need to use
MONGO_URI_=mongodb://mongo:27017/dbname
I'm front dev and I need to test locally my front app with backend (nest js) and postgresql DB. Who can write me the right way How to run and connect to DB ? I get some errors on app start. I work on win 10 and there is my steps for start this app.
install postgresql
npm install for my nest js app
run pgAdmin4 and create DB for my app
npm start
There is my ormconfig
module.exports = {
"type": "postgres",
"host": process.env.POSTGRES_HOST || "localhost",
"port": process.env.POSTGRES_PORT || 5432,
"username": process.env.POSTGRES_USER || "", //<- Here I try to set all possible username
"password": process.env.POSTGRES_PASSWORD || "", //<- Here I try to set all possible password
"database": process.env.POSTGRES_DB || "my_database",
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": true,
"logging": true
}
There is error that I encountered
error
Also on other computer I try to do this and I get error like
[Nest] ERROR [TypeOrmModule] Unable to connect to the database.
FATAL: password authentication failed for user "postgres" (postgresql 14 with pgAdmin 4)
In your typeormconfig.ts , you should write this:
export class PostgresTypeormConfiguration implements TypeOrmOptionsFactory
{
createTypeOrmOptions(connectionName?: string): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions> {
const TypeOrmOptions:TypeOrmModuleOptions=
{
type: "postgres",
host: process.env.POSTGRES_HOST ,
port: process.env.POSTGRES_PORT ,
username: process.env.POSTGRES_USER ,
password: process.env.POSTGRES_PASSWORD ,
database: process.env.POSTGRES_DB,
entities: ["dist/**/*.entity{.ts,.js}"],
synchronize: true,
logging: true
}
return TypeOrmOptions
}
}
and you should define this in your module like this:
#Module({
imports:[TypeOrmModule.forRootAsync({useClass:PostgresTypeormConfiguration})]
})
note: if you still got an error , you wrote one of the config option wrong in your .env file or you did not define .env file in your configModule
Trying to connect to an Azure PostgreSQL server from my local Strapi project (eventually deployed in a docker container).
I have the connection configured according to the strapi docs, including using the cert downloaded from the azure portal:
https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/required/databases.html#configuration-structure
my database.js file:
// PostgreSQL
////////////////////////////////////////
const fs = require("#strapi/strapi/lib/services/fs");
const parse = require("pg-connection-string").parse;
const db = parse("azure-conn-string");
module.exports = ({ env }) => ({
connection: {
client: "postgres",
connection: {
host: db.host,
port: db.port,
database: db.database,
user: db.user,
password: db.password,
ssl: {
ca: fs.readFileSync(`${__dirname}/db.crt.pem`).toString(),
},
},
},
});
when starting the server i get this in the terminal:
error: no pg_hba.conf entry for host "ip-address", user "username", database "db_name", SSL off
at Parser.parseErrorMessage (/Users/x/x/dockertest/node_modules/pg-protocol/dist/parser.js:287:98)
at Parser.handlePacket (/Users/x/x/dockertest/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/Users/x/x/dockertest/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/Users/x/x/dockertest/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (events.js:400:28)
at Socket.emit (domain.js:475:12)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:267:9)
at Socket.Readable.push (internal/streams/readable.js:206:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
It seems like its trying to connect with ssl off, I'm not sure why since it should be configured to be enabled. If I remove the ssl rule from the azure database, the connection will go through so it seems like the problem is something with the ssl config.
Can anyone help?
module.exports = ({ env }) => ({
defaultConnection: "default",
connection: {
client: "postgres",
connection: {
host: env("DATABASE_HOST", "localhost"),
port: env.int("DATABASE_PORT", 5432),
database: env("DATABASE_NAME", "postgres"),
user: env("DATABASE_USER", "postgres"),
password: env("DATABASE_PASSWORD", "0000"),
schema: env("DATABASE_SCHEMA", "public"),
},
}
});
I have followed and finished the tutorial
https://medium.com/#malliksarvepalli/hyperledger-fabric-1-2-on-multiple-hosts-using-docker-swarm-and-compose-11c13635e69e
and I have 3 hosts connected and all services are up. I had the correct result when I run the ./script.sh in the PC2 host.
Now I am following the next tutorial which is:
https://medium.com/#malliksarvepalli/hyperledger-explorer-with-fabric-1-2-running-on-multiple-hosts-89c5af691b7e
Can anyone please enlighten me in this tutorial? I have figured out that I should create a new host with ubuntu 16.04 and install the following prerequites:
nodejs 8.11.x
PostgreSQL 9.5 or greater
Jq
Am I right?
And in the exploreconfig.json I updated postgres variables:
host:192.168.1.136(4th's vm ip),
port:5432,
username:postgres,
password:psql,
database:fabric
Are they correct?
I have also modified Orderer, Org1 [peer0 & peer1), Org2(peer0 & peer1) IP address in config.json file with the ips of first three Vms where the network is up and running.
And follow the rest instructions and tests but when I run the command ./start.sh I have logs in console which are:
false 'ssl-certs' '/home/database/blockchain-explorer/ssl-certs'
postgres://christy:christy#192.168.1.136:5432/fabric
error when connecting to db: { Error: connect ECONNREFUSED 192.168.1.136:5432
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.1.136',
port: 5432 }
******* Initialization started for hyperledger fabric platform ******, {
'network-1':
{ version: '1.0',
clients: { 'client-1': [Object] },
channels: { mychannel: [Object] },
organizations: { Org1MSP: [Object], Org2MSP: [Object], OrdererMSP:
[Object] },
peers:
{ 'peer0.org1.ntua.com': [Object],
'peer1.org1.ntua.com': [Object],
'peer0.org2.ntua.com': [Object],
'peer1.org2.ntua.com': [Object] },
orderers: { 'orderer.ntua.com': [Object] } },
'network-2': {} }
client_configs.name undefined client_configs.profile undefined
FabricUtils.createFabricClient
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
Error : [ 'Invalid platform configuration, Please check the log' ]
error when connecting to db: TypeError: Cannot read property 'on' of
undefined
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:68:16)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
TypeError: "callback" argument must be a function
at setTimeout (timers.js:450:11)
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:85:5)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Received kill signal, shutting down gracefully
Closed out connections
false 'ssl-certs' '/home/database/blockchain-explorer/ssl-certs'
postgres://christy:christy#192.168.1.136:5432/fabric
error when connecting to db: { Error: connect ECONNREFUSED 192.168.1.136:5432
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.1.136',
port: 5432 }
******* Initialization started for hyperledger fabric platform ******,
{
'network-1':
{ version: '1.0',
clients: { 'client-1': [Object] },
channels: { mychannel: [Object] },
organizations: { Org1MSP: [Object], Org2MSP: [Object], OrdererMSP:
[Object] },
peers:
{ 'peer0.org1.ntua.com': [Object],
'peer1.org1.ntua.com': [Object],
'peer0.org2.ntua.com': [Object],
'peer1.org2.ntua.com': [Object] },
orderers: { 'orderer.ntua.com': [Object] } },
'network-2': {} }
client_configs.name undefined client_configs.profile undefined
FabricUtils.createFabricClient
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
Error : [ 'Invalid platform configuration, Please check the log' ]
error when connecting to db: TypeError: Cannot read property 'on' of
undefined
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:68:16)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
<<<<<<<<<<<<<<<<<<<<<<<<<< Explorer Error >>>>>>>>>>>>>>>>>>>>>
TypeError: "callback" argument must be a function
at setTimeout (timers.js:450:11)
at Timeout.handleDisconnect [as _onTimeout] (/home/database/blockchain-
explorer/app/persistence/postgreSQL/PgService.js:85:5)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Received kill signal, shutting down gracefully
Received kill signal, shutting down gracefully
Closed out connections
If the other logs are also needed please let me know. Any help will be helpful. Thanks a lot.
I can give you some aspects to be checked on your environment.
First you need to do check if you are able to access postgresql DB(4th's vm) from the vm where you are trying to launch Hyperledger Explorer. If not, you need to change your postgres configuration (postgresql.conf/pg_hba.conf) for enabling external access, I think.
$ sudo -u postgres psql -h 192.168.1.136 -d fabric -c 'table peer'
Second, please confirm that you did run ./createdb.sh on 4th VM. If you changed database name, you also need to align the script
$ cd app/persistence/fabric/postgreSQL/db/
$ ./createdb.sh
Docker config:
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
Docker compose:
version: "2"
services:
web:
build: .
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"
package.json:
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "",
"author": "Stepan Yakovenko <stiv.yakovenko#gmail.com>",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {"express": "^4.16.1","mongodb": "~3.0.1","monk": "~6.0.5" }
}
server.js:
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('mongo:27017/nodetest1');
db.then(function(){console.log("hello"});
Most of the time it works, but if I purge docker cache, usually it doesn't work and gives me this:
web_1 | (node:15) UnhandledPromiseRejectionWarning: MongoError: failed to connect to server [mongo:27017] on first connect [MongoErr
or: connect ECONNREFUSED 172.18.0.2:27017]
The root cause I think that docker's depends_on doesn't guarantee me that mongo has started listening, because in this case I get listening message from mongo after this error. How can I fix this? Does docker has any fix for this situation? Or how can I ask mongo to try connecting forever?
Thanx
This is sample reconnect code, which worked for me:
var connect = function () {
var db = monk('mongo:27017/nodetest1');
db.then(function () {
console.log("connected");
}).catch(function () {
// sometimes node starts before mongo, so we have to reconnect in case of error
connect();
});
};
connect();