I am running NginX, Node and Mongodb. And it seems that I can't acces the same database from a second app I am running. For example, I don't get anything back when I do:
collection.findOne({
name: someName
}, function(err, results){
// Returns no errors or results. Just stops working.
});
I can access the database perfectly fine from my first app, but not the second one.
This is the code I use to connect to the database in both apps.
Server = require('mongodb').Server,
Db = require('mongodb').Db,
db = new Db('database', new Server('localhost', 27017, { auto_reconnect: true }), { w: true });
Anyone know what the problem might be?
Edit: Does it have something to do with the subdomain or ports? Too many connections?
Edit 2 (more info):
I run mongodb with service mongodb start.
In my /etc/mongodb.conf I have bind_ip = 127.0.0.1 and dbpath=/var/lib/mongodb (rest is default)
In both my apps I run the same code to establish a connection to the database, but only the first one works (I know that because I am able to retrieve information from the database in my first app).
The apps are running on different ports. The first one is running on port 1337 and the second one runs on 3000.
You are using 'localhost' as the host name to connect to this server.
This means you will only be able to connect from the same machine that mongod is running on with that hostname.
Unless all your apps run on the same server as mongod you will need to change your connect code to use the actual hostname of the mongod server.
Related
I am having issues connecting to MongoDb running remotely, and the connection error response I am getting from the server is somewhat weird.
My network access whitelist is set to allow all (0.0.0.0/0). Hence, my local robo3t installation was able to connect. However, I could not connect from my NodeJs code. Error is: "MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist"
IP whitelist seems to be an unlikely error, given that my local robo3t client is able to connect remotely to the same remote Mongo Atlas instance, as IP whitelist is allow-all.
How do I debug this kind of thing, please?
UPDATE: this is how I connect to MongoDb. Works well on local, too.
try {
const connectionString =
process.env.APP_ENV == "test"
? await getInMemoryMongoDbAdapter()
: `mongodb://${process.env.MONGODB_HOSTNAME}:${process.env.MONGODB_PORT}/${process.env.CBT_DATABASE_NAME}`;
logger.info(`Connecting to MongoDB service: ${connectionString}`);
await mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (error) {
reject(error);
}
The logger line correctly shows: Connecting to MongoDB service: mongodb://<user>:<password>#cluster0-xxx.yyy.zzz.net:<port>/<database>
UPDATE 2:
My localhost also does not connect via this node app; whereas my robo3t (local MongoDb client) connects. I guess that means Heroku-specific issues can now be comfortably ruled out
A decade later, I found that for the connection parameters, I needed to supply the authSource and ssl options, as below:
{
useNewUrlParser: true,
useUnifiedTopology: true,
authSource: "admin",
ssl: true,
}
Neither one works without the other. Big shout-out to #darklightcode for all the insights he gave, leading me to dig deeper. Thanks man!
so this is my code, I watched a lot of videos how to connect mongo db to node and I tried every possible way but it doesnot connect. What's wrong with my code??? also I downloaded every important thing
Your Connection URL is without a port number, add the port number to the URL
mongodb://localhost:27017/subscribers
MongoDB - default port number is 27017
https://docs.mongodb.com/manual/reference/default-mongodb-port/
https://mongoosejs.com/docs/connections.html#connections
You can connect to MongoDB with the mongoose.connect() method.
mongoose.connect('mongodb://localhost:27017/myDB', {useNewUrlParser:
true}); This is the minimum needed to connect the myapp database
running locally on the default port (27017). If connecting fails on
your machine, try using 127.0.0.1 instead of localhost.
You can also specify several more parameters in the uri:
I am trying to connect a client device to a PostGreSQL host device. I have 2 devices with PostGreSQL on them: A Windows OS device (Host) and a RaspberryPi running raspbian (Client).
I have pgAdmin on the host device, with a PostGreSQL server running on it (The server is the default PostGreSQL server with default configurations).
The host device and the client device are on the same network (wifi).
I have the following node.js code on the client device:
const { Client } = require('pg');
const connectionString = 'postgres://postgres:MyPassword#192.168.1.8:5433/postgres';
const client = new Client({
connectionString: connectionString
});
client.connect();
client.query('CREATE TABLE test (something text, number int)', function(err,result){
if (err) throw err;
console.log(result);
console.log('hi');
});
based on the code above, the client would connect and query the making of a table, then return the result and 'hi' in the command line.
When I run it, it does nothing. After a minute or so, the connection times out.
Is there anything that I'm missing?
I used Wireshark to see if the client device was even sending packets. The client device was sending packets (directed to the postgresql post), so my assumption is that the problem is with the host device, but I don't know why because I have already edited the configuration files multiple times by now.
I have already...
changed config files
restarted the system
re-installed the software
switched to trying to connect from psql
Well, this is interesting...
So I found the real .config files on the host device, and I changed listen_addresses = '*' to listen_addresses = '192.168.1.12'.
After I made those changes, I tried to restart postgres and then the service stopped, but refused to be started again. I opened pgAdmin 4, and none of the servers were wanting to connect. After I changed the file back to listen_addresses = '*' the service started and everything was back to normal. My conclusion now is that there is a problem with the firewall. Does anyone here do Windows and can help me configure the firewall??
Turns out, I was bothering myself with the Postgres configuration files, but I had to look at the firewall configurations. Those were the ones that were blocking the pi. If anyone has the same problem, I suggest looking at your firewall first. (Specifically allowing port 5432)
I have been researching this for day and I haven't been able to find the way to do this.
I am building a react app, running express at the backend, that needs to access some data in a remote database that lives inside a VPN. At the moment the app lives on my localhost so its enough for me to connect my machine using openvpn client and everything works a beauty. The problem will rise when the app will be live and I will need it to have access to the vpn by (I'm guessing) having a vpn client running on the site/domain.
Has anyone done this before?
I have tried to install the node-openvpn package that seems could do the job but unfortunately I can't manage to make it work as the connection doesn't seem to be configured properly.
This is the function I call to connect to the vpn that systematically fails at the line
--> openvpnmanager.authorize(auth);
const openvpnmanager = require('node-openvpn');
...
const connectToVpn = () => {
var opts = {
host: 'wopr.remotedbserver.com',
port: 1337, //port openvpn management console
timeout: 1500, //timeout for connection - optional,
logpath: '/log.txt'
};
var auth = {
user: 'userName',
pass: 'passWord',
};
var openvpn = openvpnmanager.connect(opts);
openvpn.on('connected', function() {
console.log('connecting..');
openvpnmanager.authorize(auth); <-- Error: Unhandled "error" event. (Cannot connect)
});
openvpn.on('console-output', function(output) {
console.log(output)
});
openvpn.on('state-change', function(state) { //emits console output of openvpn state as a array
console.log(output)
});
};
Am I misusing this function? Is there a better way?
Any help will be extremely appreciated.
Thank You!
The problem will rise when the app will be live and I will need it to
have access to the vpn by (I'm guessing) having a OpenVPN client running
on the site/domain.
Thats correct, you will need an openvpn client instance on the server where you will run the backend.
The above library (node-openvpn) is simply a library to interact with the local OpenVPN client instance. It cannot create a connection on its own. It depends on the OpenVPN binary (which should be running).
The solution you need is simply run the OpenVPN client on your server (apt-get openvpn). And let the daemon run. Check out the references below.
node-openvpn issues that points out that a running instance of the client is needed
OpenVPN CLI tutorial
I installed mongo on my elastic beanstalk node.js app and started the mongo daemon process. I'm not quite sure how to connect to the database though. On my local node app, I'm able to connect with these credentials:
module.exports = {
'url' : 'mongodb://127.0.0.1:27017/test'
}
I'm assuming that it doesn't connect because I need a user, password, and to create a database to connect to, but I'm not sure how to go about doing that on the remote database. I'm also finding resources on setting mongo up on t1.micro to be very scarce, so there's not much help there.
I didn't realize that I had to start up the mongo processes myself. Run mongod.