Basics of using MongoDB on remote server with SSH - mongodb

I'm having trouble getting started with MongoDB via SSH on a VPS. Note that MongoDB was installed by admins with the VPS service, not me. I have to presume they installed it correctly. Unfortunately, they provide zero support documentation and I can't figure it out after reading countless official MongoDB docs and even more found elsewhere.
I am able to SSH into the remote server just fine. I can check the MongoDB version, and mongosh is there, too. But I cannot figure out how to get to the point where I can create a new collection, or even start MongoDB, if that's what I need to do.
Here are some of the things I've tried in the shell for the VPS. The closest I've gotten is by running mongosh -nodb which seems to get me inside of MongoDB, but I know that option means "no database," so it's not what I need to do.
$ ssh customusername#207.196.153.34
Last login: Thu Dec 29 11:56:53 2022 from 82.204.238.49
customusername#customdomain.com [~]# mongod -version
db version v6.0.3
Build Info: {
"version": "6.0.3",
"gitVersion": "f803681c3ae19817d31958965850193de067c516",
"openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "rhel70",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
customusername#customdomain.com [~]# mongosh --version
1.6.1
customusername#customdomain.com [~]# mongosh -nodb
Current Mongosh Log ID: 63abf66ca872ab436a8040c8
Using Mongosh: 1.6.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
> use test-db
MongoshInvalidInputError: [SHAPI-10004] No connected database
> exit
customusername#customdomain.com [~]# mongosh --username customusername
Enter password: ********************************
Current Mongosh Log ID: 63abf8c15a47f0e078be3d76
Connecting to: mongodb://<credentials>#127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.1
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
customusername#customdomain.com [~]#
I am able to pull up "help" files. And I've tried multiple variations of # mongod mongodb://customusername#127.0.0.1:27017/ which fail.
customusername#customdomain.com [~]# mongod help
customusername#customdomain.com [~]# mongosh -h
customusername#customdomain.com [~]# mongod mongodb://customusername#127.0.0.1:27017/
That last command returns a few lines which indicate it might be working, then it fails with message: "Invalid command: mongodb://customusername#127.0.0.1:27017/" And then it displays a number of "General options" such as "-h [ --help ]". I can't make sense of most of them.
Note that I cannot install MongoDB/mongosh locally because my machine is too old, so I have to be able to log in through SSH and handle it that way. If I can figure it out via SSH, maybe I'll try something like MongoDB Compass next, which I am able to install locally.
Thank you for your time and assistance.
UPDATE: I think I've made progress, but am still not there yet.
I think I got MongoDB running with this, after creating a directory called "mongodb" to hold data files:
# mongod --port 28015 --dbpath ./mongodb/
A string of messages come up in my terminal, with the last being related to listening ("ctx":"listener","msg":"Waiting for connections"), and the process appears to continue running.
I then open a new terminal window, SSH in, and run this:
# mongosh --port 28015
Which appears to land me in a mongo shell in which I'm able to create a new collection:
test> use test-db
switched to db test-db
test-db>
Also, I notice that the original terminal window with the open process outputs connection-related messages as I interact with the second window (e.g. "ctx":"listener","msg":"Connection accepted").
I looked inside of the /mongodb/ directory and there are a number of mongo-related files in there now, "collection-0-5001736531146682033.wt", "index-1-5001736531146682033.wt", "storage.bson", and so forth. So, I think the db creation is working.
UPDATE TWO
Was struggling with why the show command was throwing an error, since use was already working, then figured out I needed to insert a document first:
test> db.tours.insertOne({name: "Some Name", price: 19, rating: 3.5})
The full command is show dbs and then the "test" database appears.

So, you can check out my two updates above. I was able to access and do very basic operations with MongoDB on a VPS with SSH.
Also, here are some helpful links, if you struggle with similar issues.
Mongo Shell basics:
https://www.mongodb.com/basics/get-started#get-started-with-mongodb
(skip down to the "Get Started with MongoDB" subsection, since the top of the page is about the Atlas interface)
Setting up admin and user accounts via command line:
https://medium.com/#haxzie/getting-started-with-mongodb-setting-up-admin-and-user-accounts-4fdd33687741
Good luck!

Related

Why not so easy to connect to MongoDB

I am a newbie to MongoDB and having an issue connecting to MongoDB from the terminal.
MongoDB Tutorial on YouTube
When I tried to connect to MongoDB from the terminal in VS Code, I've got an error saying; "MongoServerSelectionError: read ECONNRESET".
What I did is that I googled and unchecked the Proxy Strict SSL in settings, then reloaded the window and ran the command below.
mongosh "mongodb+srv://cluster0.8tjjn.mongodb.net/myFirstDatabase" --apiVersion 1 --username mongo
The username is correct as shown, mongo. I was asked for my password and typed it correctly. It's just 5 letters password, so it's hard to imagine I mistyped it a few times in a row.
I double-checked if I installed mongo and mongosh by running:
mongo --version
↓output
mongo --version
MongoDB shell version v5.0.6
Build Info: {
"version": "5.0.6",
"gitVersion": "212a8dbb47f07427dae194a9c75baec1d81d9259",
"modules": [],
"allocator": "system",
"environment": {
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
Also
mongosh --version
↓output
1.2.2
After the two commands, I ran below and typed the password.
mongosh "mongodb+srv://cluster0.8tjjn.mongodb.net/myFirstDatabase" --apiVersion 1 --username mongo
The YouTuber explained how to connect it in a straightforward way, so I am guessing if my mac air has a problem in it such as the too old version. I still use macOS Mojave 10.14.6 causing some installation problems so far. I can't install a newer OS due to the lack of storage. It has 128GB and most of it is accounted for systems. I sometimes need to restart it when it's slow and have been thinking to buy a new one.
Thanks a lot in advance:)

Trying to run my project on localhost – how can I use "mongod" command in Mac terminal instead of "sudo mongod"?

(NOT A DUPLICATE: This question title looks very similar to Dont want to have to start mongod with `sudo mongod`, but they are slightly different problems, and that question did not have any approved answers and I don't want to try something unapproved if I don't know what it will do.)
I am using mongodb and I am trying to run it on a localhost.
Supposedly, I should do $ mongod first in one terminal tab, then $ turbo devserver in another terminal tab, and this should run my project on a localhost.
However, when I try $ mongod, a bunch of text I don't understand shows up and in the midst of this text I see the following exception:
exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
However, when I instead do $ sudo mongod it runs perfectly and I can see my project data in the localhost. But I don't want to have to use sudo, I want to be able to just use the $ mongod command.
I looked around on stackoverflow and although there were similar problems (but with a different exception), nothing really helped me. I am very new to mongodb/APIs/JS/JSON/etc. so it was hard to understand what some of the other posts were talking about.
In a nutshell
What I'm trying to do:
Use $ mongod command on Mac terminal to set up database access
Use $ turbo devserver to successfully connect database to localhost
What is happening:
$ mongod command returns IllegalOperation exception
Therefore $ turbo devserver returns DB Connection Failed!
Database not connected, can't see project data in localhost
What I want to happen:
$ mongod to return waiting for connections on port 27017 ...
...which would make $ turbo devserver return DB Successfully Connected! ...
...which would show my project data in localhost
What I've tried:
Using $ sudo mongod instead, which works, but I want only $ mongod
Help is much appreciated. Thanks community!

Mongodb is not running

I have installed mongodb in my computer. But whenever I try to launch it, it's not running. I have entered command , as "mongo" to start the mongodb, which shows following resuts, but the application is not opening.
Any one having any idea how can I fix this? Please share it. Thanx!
Environment: Ubuntu 16.04
Those are warnings that appear at the first mongo start. Don't worry to much about them.
Since the process is still running and you got the small > character there, that means you successfully connected to the MongoDB server.
mongod is the MongoDB server (you can check if it's running by running ps aux | grep mongod) and the mongo cli tool is a cli client connecting to the server. Here you can run commands.
For example, create your first document:
use myDatabase
db.people.insert({ name: "Rhea" })
db.people.find()
In conclusion, both the MongoDB server and client are running but you got a few warnings that only appear the first time when you run mongo.
Initially in one terminal write mongod.
Then on another terminal : mongo .
The first one will start the mongo server.
The second one will start the interpreter.
You can try various commands in the interpreter.

Mongo: network error while attempting to run command 'whatsmyuri' on host

I have been trying to access my mongo instance from another machine, but I get this error. I could not find many references to this whatsmyuri error. This is what I get from the external machine:
$ mongo <IP_ADDRESS>:27017/youtube_advertising -u user -p password
MongoDB shell version: 3.2.0
connecting to: <IP_ADDRESS>:27017/youtube_advertising
2016-02-19T17:10:02.923+0100 E QUERY [thread1] Error: network error while attempting to run command 'whatsmyuri' on host '<IP_ADDRESS>:27017' :
connect#src/mongo/shell/mongo.js:226:14
#(connect):1:6
exception: connect failed
I have already changed the /etc/mongod.conf file, opened connections through port 27017 (with iptables) and restarted mongo. I am able to connect via ssh to that machine.
Searching about this whatsmyuri, I ran this command on mongo:
> db.runCommand( { whatsmyuri: 1 } )
{ "you" : "127.0.0.1:36990", "ok" : 1 }
I do not know if that 36990 port is right or wrong. Just in case I opened connections from there too, but still nothing.
Any ideas?
UPDATE
Checking the /var/log/mongodb/mongod.log, this is what I get when I try to connect from remote:
2016-02-19T10:41:07.292-0600 I NETWORK [initandlisten] connection accepted from <EXT_IP_ADDRESS>:51800 #2 (1 connection now open)
2016-02-19T10:41:07.310-0600 I QUERY [conn2] operation isn't supported: 2010
2016-02-19T10:41:07.310-0600 I - [conn2] Assertion: 16141:cannot translate opcode 2010
Check your versions. That may help.
I was having the same problem. In my case, the server was version 3.2.0-rc2, while mongo shell version was 3.2.1.
Upgrading the server to 3.2.1 fixed the problem.
This issue bit me when I was running two versions (3.4 and 4.2) of MongoDB on the same Windows 10 machine. I ran v3.4 mongod with no problems mentioned in the console output, but then running the v3.4 mongo shell produced the above error. Checking the Task Manager, it turned out there was a MongoDB process (I'm not sure, but I think it was for v4.2) running. After ending that process through the Task Manager, the v3.4 mongo shell ran fine with no error.
Using mongodb-community-shell
In MacOs brew install mongodb/brew/mongodb-community-shell
It will ask you to overwrite link with mongo command.
brew link --overwrite mongodb-community-shell
The issue arises when the mongo client's and server's version mismatch.
Uninstall MongoDB from the client and for installation follow the detailed instruction provided over here.
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
The key step which needs to be done with the attention is
sudo apt-get install -y mongodb-org=4.2.7 mongodb-org-server=4.2.7 mongodb-org-shell=4.2.7 mongodb-org-mongos=4.2.7
mongodb-org-tools=4.2.7
Note: In my case, the mongo version of server was 4.2.7
Another thing that one can do is uninstall MongoDB from both the systems taking necessary backups and install it again.

mongoDB test error

I came to know about mongoDB and looked for test.So I made it install and then for test when I used command mongo on terminal it showed an error like this
MongoDB shell version: 1.8.2
connecting to: test
Sun Jul 31 01:06:07 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:79
exception: connect failed
So can someone tell me what is the problem.I am using ubuntu 11.04.For installation instruction I had used this site.I am newbie to this mongoDB so please helpe me.Any help will be highly appreciable.
All you need to do is open 2 terminal tabs. In one, run
mongod
which starts the MongoDB server.
In the other, run
mongo
which is the shell that connects to your MongoDB server.
It looks like MongoDB isn't running. Can you connect to the web interface in your browser?
http://localhost:28017
Also, do you see the process running on your machine? You should see an entry for mongod when running ...
$ top
or
$ ps aux
why not install mongodb from 10gen's own debian repository? much easier and more likely to work
http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages
To see if mongodb is running, this also helps:
sudo service mongodb status
if it is running, and you still get the same error, then it must be the weird localhost bug that mongodb has. it assumes localhost is 127.0.1.1 for some reason. try
mongo 127.0.1.1
I had the same problem. Just try to create folder c:\data and next c:\data\db