MongoDB¶
MongoDB is a very popular NoSQL database that uses JSON to store and retrieve data. The flexibility and speed it offers in some use cases has lead to a steady increase in developer adoption, especially paired with Node.js.
MongoDB 3.2 is currently available on Simple Hosting and can be used with all languages.
You can create as many databases and users you want in your instance. You are only limited by the disk size, which you can increase at any time up to 1 TB.
The MongoDB database service can be managed from the console or from a Web interface. This article describes how to access, create and manage MongoDB databases on Simple Hosting.
Connecting to your database¶
The MongoDB database service is available on localhost
at the default port 27017
. It can only be accessed from within the instance and cannot be reached from the outside world. No username or password are necessary to quickly test your connection and perform management tasks.
Default connection settings:
Host: localhost
Port: 27017
User: <none>
Password: <none>
Database: <any>
The URL to connect to a database named myapp-production
would look like this:
mongodb://localhost:27017/myapp-production
Bemerkung
You are encouraged to create new users with strong credentials for your application. You’ll find instructions on how to perform management tasks below.
Managing your database with Adminer¶
You can access your Simple Hosting database by first logging into the Control Panel of your instance, and then click on the Adminer link in the MongoDB section of the Control Panel.
Create a database¶
To create a database from within Adminer, click on the Create database link. On the following page, enter a name for your database and click Save
Export a database¶
Adminer does not support exporting databases. Please see the section below „Managing your database from the command line“ for steps on how to export a MongoDB database.
Import a database¶
Adminer does not support importing databases. Please see the section below „Managing your database from the command line“ for steps on how to import a MongoDB database.
Managing your database from the command line¶
To access your MongoDB database by command line, you must first log into your instance via the Emergency Console.
Note that the console will automatically disconnect after a few minutes of inactivity. If this happens, you can just initiate a new SSH connection without needing to reactivate the console.
Access the database¶
Launch the mongo
shell with the following command:
$ mongo
MongoDB shell version: 2.4.8
connecting to: test
Welcome to the MongoDB shell.
>
Switch to the database you would like to perform operation on with the use
command:
> use database_name
switched to db database_name
Import / Export a database¶
To export a database from the SSH console, run the mongodump
command and specify which database/collection you’d like to export, and a writable directory to output the file:
# Export entire database
$ mongodump --db database_name \
--out ~/tmp/mongodump-yyyy-mm-dd
# Export a collection within a database
$ mongodump --collection collection_name \
--db database_name \
--out ~/tmp/mongodump-yyyy-mm-dd
Bemerkung
Only the /srv/data/tmp
directory can be written to - with the exception of virtualhosts. Consequently, we recommend creating a specific directory for MongoDB exports in order to gather them together in the same location.
To import a database from the SSH console, run the mongoimport
command and specify which database/collection you’d like to import the data into, along with the file containing the data to import:
$ mongoimport --db database_name \
--collection collection_name \
--file collection.json
Enable Full-text Search¶
Enabling full-text search requires setting an administrative parameter in the default admin database.
From the mongo shell you can list all administrative parameters with the following commands:
> use admin
switched to db admin
> db.runCommand( { getParameter: '*' } )
{
"enableLocalhostAuthBypass" : true,
"enableTestCommands" : 0,
"logLevel" : 0,
"logUserIds" : false,
"notablescan" : false,
"quiet" : false,
"releaseConnectionsAfterResponse" : false,
"replApplyBatchSize" : 1,
"replIndexPrefetch" : "uninitialized",
"supportCompatibilityFormPrivilegeDocuments" : true,
"syncdelay" : 60,
"textSearchEnabled" : false,
"ttlMonitorEnabled" : true,
"ok" : 1
}
You can then enable full-text search by setting the value of textSearchEnabled to true
:
> db.runCommand ( { setParameter: 1, textSearchEnabled: true } )
{ "was" : false, "ok" : 1 }