简体   繁体   中英

Basics of using MongoDB on remote server with SSH

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 (eg "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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM