Mongodb Compass: Connection timed out - mongodb

I installed Mongodb on my remote server using this documentation. I have Ubuntu 16.04 on my remote server. Mongodb got installed successfully. I added the user like this:
use admin
db.createUser(
{
user: 'myuser',
pwd: 'password',
roles: [ { role: 'readWrite', db: 'mydb' } ]
}
);
I also made changes in the mongod.conf like this:
net:
port: 27017
bindIp: 127.0.0.1,<server_ip>
security:
authorization: 'enabled'
Now when I try to connect to mongodb using conneciton string like this:
mongodb://myuser:password#server_ip:27017/mydb
It gives me the following error:
connection timed out
What am I doing wrong here? I am using Laravel Forge to manage sever.

As it turned out the port was not open and that was the only issue. Opened the port and now its working fine.

Related

MongoDb user permission denied

First I created mongoDB database and made it live AWS. Then I'm creating mongoDB database to MongoDb BI connector locally.
And I created Configuration file which is mentioned below:
security:
enabled: true
defaultMechanism: default
defaultSource: admin
mongodb:
net:
uri: MongoUrl
auth:
username: MongoUser
password: MongoPass
net:
bindIp: localhost
port: 3307
ssl:
mode: "allowSSL"
PEMKeyFile: "/mongo.pem"
MongoDB BI connector
And my cubeJS env file is this:
CUBEJS_DEV_MODE=true
CUBEJS_DB_TYPE=mongobi
CUBEJS_API_SECRET=9c7a834d9be0cd2fb4e7b86a88a2bd1368ffbc614dcf8fa8f836933b00d00c6916fa83b074c66a2b26579266edc93e0e1990b1271f6214ee799dc08b05b6a097
CUBEJS_EXTERNAL_DEFAULT=true
CUBEJS_SCHEDULED_REFRESH_DEFAULT=true
CUBEJS_WEB_SOCKETS=true
CUBEJS_DB_HOST=localhost
CUBEJS_DB_NAME=MongoDbName
CUBEJS_DB_USER=mongoUserName
CUBEJS_DB_PASSWORD=MongoPassword
CUBEJS_DB_PORT=3307
CUBEJS_DEV_MODE=true
CUBEJS_REFRESH_WORKER=true
port=4242
CUBEJS_DB_SSL=true
#CUBEJS_DB_SSL_CA=<SSL_CA>
#CUBEJS_DB_SSL_CERT=<SSL_CERT>
#CUBEJS_DB_SSL_CIPHERS=<SSL_CIPHERS>
#CUBEJS_DB_SSL_PASSPHRASE=<SSL_PASSPHRASE>
CUBEJS_DB_SSL_REJECT_UNAUTHORIZED=false
and Error is showing this:
Access denied for user
C:\Users\dell 7470>"C:\Users\dell 7470\Desktop\sql\mysql-8.0.31-winx64\bin\mysql.exe" --enable-cleartext-plugin --protocol tcp --port 3307 --user=hikarsmarttag --ssl-key=C:\mongo.pem --ssl-cert=C:\certificate.pem -p
Enter password: *****************
ERROR 1045 (28000): Access denied for user 'hikarsmarttag'
Whenever I try to connect to BI tools MySQL-Client, The same Error is being thrown.
Access denied in mySQL-Client

Connecting to a remote mongoDB server

I have a remote machine which I connect to using SSH, I installed mongoDB on it, and I wish to use it remotely, how do I connect to it using nodejs and mongoDB compass? the localhost is the IP ?
const db = "mongodb://what do I write here?";
const connectDB = async () => {
try {
await mongoose.connect(db, { useNewUrlParser: true, useCreateIndex: true });
console.log("MongoDB Connected...");
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
connectDB();
Short answer
Login to your machine, open mongodb configuration file located at /etc/mongod.conf and change the bindIp field to your machine ip address (it is the same ip address which you are using to ssh to your machine), after that restart mongodb server.
Detailed answer
Open /etc/mongod.conf file using any of the editor, if you are running a desktop version then you can make use of gedit utility tool
sudo gedit /etc/mongod.conf
If you are running a server version, then you can make use of vi editor command
sudo vi /etc/mongod.conf
The file should contain the following kind of content:
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1 // enter your ip address here
port: 27017
setParameter:
enableLocalhostAuthBypass: false
Once you change the bindIp, then you have to restart the mongodb, using the following command
sudo service mongod restart
Now you'll be able to connect to the mongodb server, with the same ip address which you are using to ssh to your system.
mongoose.connect('mongodb://<machine_ip_address>:27017/<database_name>')
mongoose.connect('mongodb://username:password#host:port/database')
Now for the host, is there any hostname or IP you could use?
Try this one:
mongoose.connect("mongodb://localhost/<database-name>", { useNewUrlParser: true });
const db = mongoose.connection
db.on('error', (error) => console.error(error));
db.once('open', () => console.log('Connected to Database'));
Make sure to run MongoDB
mongod --config /usr/local/etc/mongod.conf
first try this : mongoose.connect('mongodb://localhost:27017/database')
mongoose.connect('mongodb://<machine_ip_address>:27017/<database_name>')
mongodb://[username:password#]host1[:port1][,...hostN[:portN]][/[database][?options]]
check https://docs.mongodb.com/manual/reference/connection-string/
If you are using MongoDb Compass
Open 27017 port in Inbound of your Server
Form a link just like mongodb://11.11.111.11 in case you are not going to use auth
Click Connect
Basicly the link is mongodb://[username:password#]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
More information here

user authentication not working on mongodb 3.6.5

I have installed mongodb 3.6.5 on aws ec2 server and setup following in .conf file
net:
port: 27017
bindIp: serverIP
security:
authorization: 'enabled'
then created user with following command in admin db and restarted mongodb
db.createUser(
{
user: "mydbuser",
pwd: "mydbpass",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
now when I try to access db from console with following command if doesnt allow me and gives me error of authentication.
mongo serverIP --port 27017 -u "mydbuser" -p "mydbpass" --authenticationDatabase "admin"
Can someone help me what is wrong with it.
getting following error
MongoDB server version: 3.6.5
2019-01-11T08:46:50.245+0000 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1608:20
#(auth):6:1
#(auth):1:2
exception: login failed
The method you are following is correct but there is some variations in mongodb while accepting credentials as string. You might need to enclose your username and password within single quote instead of double quote as follows.
mongo serverIP --port 27017 -u 'mydbuser' -p 'mydbpass' --authenticationDatabase 'admin'
Double quotes will work if you dont have special characters in your username/password.
For more details on mongo Check here https://docs.mongodb.com/manual/reference/program/mongo/

MongoDB replicaset TLS/SSL

I have launched a MongoDB 4 replica-set on 3 servers by private IP successfully. Now I wanna bind another IP and it needs enabling TLS/SSL.
I have created PEMKeyFile and CAFile and copied these file s on all 3 servers and added the codes below to mongod.config file of all 3 servers.
# network interfaces
net:
port: 27017
bindIp: 10.10.20.21,5.22.25.45 # example private ip and one example valid IP
ssl:
mode: requireSSL
PEMKeyFile: /opt/mongo/mongo.pem
PEMKeyPassword: MyPassword
CAFile : /opt/mongo/CA.pem
allowInvalidCertificates: true
allowInvalidHostnames: true
security:
keyFile: /opt/mongo/mongo-keyfile
I got error
E STORAGE [initandlisten] Failed to set up listener: SocketException: Cannot assign requested address
I CONTROL [initandlisten] now exiting
I CONTROL [initandlisten] shutting down with code:48
What is wrong with it? How can I fix it?
should I see both of these IPs
yes, of course.
bindIp tells mongodb service which system network interfaces to listen on. These are local system interfaces, not the clients. As soon as mongobd bind to an interface, clients from anywhere can connect to this IP:
binding to 10.10.20.XXX : the private class A network interface allows clients to connect from any 10.XXX.XXX.XXX IP within the same network
binding to 5.22.25.XXX : the public network interface allows clients to connect from anywhere in the internet.
If you want to restrict access to mongodb and allow to connect from specific IPs/networks only, you need to enable authentication and apply the restriction to the user or a group: https://docs.mongodb.com/manual/reference/method/db.createUser/#authentication-restrictions.
E.g.
use admin
db.createUser(
{
user: "remote-client",
pwd: "password",
roles: [ { role: "readWrite", db: "reporting" } ],
authenticationRestrictions: [ {
clientSource: ["5.22.25.45"]
} ]
}
)
Will allow mongo -u remote-client -p password to connect from IP 5.22.25.45 only and permit reads and writes to/from "reporting" database.

How to use mongodb-express with MeteorJS' MongoDB?

I'm building an application in MeteorJS.
I want to have GUI access to built-in MongoDB database.
So I found: https://github.com/andzdroid/mongo-express
I installed it, configured it to connect to localhost:3001.
Since mongodb doesn't have a default admin password, I tried to create it by:
meteor mongo
use admin
db.addUser("admin","password")
then I set
adminUsername: 'admin',
adminPassword: 'password',
in mongo-express\config.js.
However when I open localhost:8081, it asks me login credentials again and even if I insert them manaully (admin, password) it doesn't work.
So I went back to meteor mongo, tried to create admin user again and go error
Error: couln't add user: User admin#admin" already exists
What am I doing wrong?
edit /usr/local/lib/node_modules/mongo-express/config.default.js
find and edit to:
} else {
mongo = {
db: 'meteor',
host: 'localhost',
password: '',
port: 3001,
ssl: false,
url: 'mongodb://localhost:3001/meteor',
username: '',
};
}