MongoDB on devweb #
For access to MongoDB on devweb, please see the guidance on Requesting Access.
If you have requested MongoDB access, you'll be given an account with the same username as your university DS account (e.g. xxy12345). You can connect from Linux (Cafe, Linux lab machines or CPU nodes - see Remote Compute) by running
mongo --host devweb2022.cis.strath.ac.uk:27017.
On Windows lab machines, you can use the Putty SSH client to connect to cafe; you can find a shortcut to cafe (using Putty) on the desktop, labelled SSH Cafe.
Querying from the CLI #
The following code querying MongoDB from the CLI (replace $user and $pass as necessary):
mongo --host devweb2022.cis.strath.ac.uk:27017 use admin db.auth("$user", "$pass") use username show collections exit
Querying from Python #
The following code querying MongoDB from Python (replace $user and $pass as necessary):
from pymongo import MongoClient client = MongoClient('mongodb://$user:$email@example.com:27017/') db=client.username db.collection_names()
Querying from PHP #
See http://zetcode.com/db/mongodbphp/ for a more complete tutorial on PHP and MongoDB.
The following code demonstrates reading the latest timestamp from a collection then inserting the current timestamp into that collection using PHP (replace $user and $pass as necessary):
$user = "xxy12345"; $pass = "MyMongoDBPassword"; $host = "devweb2022.cis.strath.ac.uk"; $mongo = new MongoDB\Client("mongodb://$user:$pass@$db_host:27017/"); $db = $mongo->$db_name; $coll = $db->testcoll; $count_before = $coll->count(); $coll->insertOne(['ts' => new MongoDB\BSON\UTCDatetime(), 'source' => 'php']); $count_after = $coll->count(); $max_ts_unix = $coll->findOne(, ['sort' => ['ts' => -1]])['ts']; $max_ts = date("Y-m-d\TH:i:s", (string)$max_ts_unix/1000); echo "<li>count was $count_before, now $count_after; new ts $max_ts</li>\n";
Accessing MongoDB from home / off-campus #
In order to access MongoDB off-campus, you need to tunnel your connection via cafe (ssh) as follows (replacing $username with your DS username):
ssh -L 27017:devweb2022.cis.strath.ac.uk:27017 $firstname.lastname@example.org
You'll then by able to connect your local MongoDB client or app to localhost:27017, which will be tunnelled to devweb2022 via cafe.
See Remote compute for more details.