Introduction
MongoDB is one of the most popular database solutions for a number of applications on the web and is the most popular NoSQL solution. Since MongoDB is NoSQL, your application data does not have to adhere to a rigid schema. This allows for fast prototyping and development, since you never have to perform database migrations. Stack Harbor has made it easy for your to deploy a MongoDB database server using our Turnkey Stacks. We’ll show you how to get up and running with MongoDB.
Accessing Your Stack
Once your stack is built, you should be able to interface with the MongoDB process through port 27017. By default, your MongoDB stack only accepts connections to the database server locally. In other words, you’ll need to SSH into your server and interact with the MongoDB process. If you don’t know how to SSH into your machine, check out our detailed tutorial.
Once you’ve logged into your machine using SSH, you can verify that the MongoDB server is running by running the following command in your shell:
service mongod status
You should be returned something along the lines of mongod start/running, process 8888. The portion reading process 8888 will probably be different since the MongoDB server doesn’t always run with process ID 8888. This means that your database server is running and that you’re able to read/write to the server. We can use the MongoDB shell client to interact with the database server. Run the following command to connect to the server using the mongo
mongo
From here on, you’re free to create databases, create collections, and insert documents into collections. Check out the documentation distributed by the folks who maintain MongoDB for more information on how to use the mongo shell.
Creating Database User Accounts
Right now, we have no issue connecting to the MongoDB server using the mongo client since we are running the client locally. However, this is both unsecure (if someone acquires access to your machine, your database is compromised) and inefficient (you will probably want to write data from your application server to your database server eventually). Before we allow remote users and applications to connect to our database server, we should set up some authentication to secure our server. Let’s first connect to the mongo client as we have been doing:
mongo
Now we can change to the admin database and create a user administrator that will be used to perform administrative tasks for the whole database server.
use admin
db.createUser(
{
user: "your_admin_username",
pwd: "your_admin_password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
You should replace your_admin_username and your_admin_username_password with the appropriate username/password for the user admin you want to create. While you’re creating user accounts, it is recommended that you create a different database user for every single application that you use to interact with the database server. For example, if I have a node.js application that needs to connect to the ‘complicatedApplication’ database on my MongoDB server, I should create a ‘complicatedApplication_user’ account that will be used to connect to the database. The syntax to create a database user is very similar to the syntax for creating a administrative user:
use complicatedApplication
db.createUser(
{
user: "complicatedApplication_user",
pwd: "complicatedApplication_user_password",
roles: [ { role: "userAdmin", db: "records" } ]
}
)
Again, replace complicatedApplication_user_password with the password you wish to set for the new user account. After you’ve created the user accounts, you will need to inform the MongoDB server that you wish to use authentication and also that you wish to allow remote connections from the internet. We’ll need to edit the mongod.conf file in the /etc/ folder.
nano /etc/mongod.conf
This file contains all the default settings for your MongoDB database server. Add the following line to the file (anywhere in the file is fine):
auth=true
In order to allow remote connections, find the line that reads bind_ip = 127.0.0.1 and comment out the line by inserting a ‘#’ in front. Press CTRL-O, Enter/Return, CTRL-X to save and quit the file. In order to allow the changes to take place, we have to restart the MongoDB database server. Run the following command:
service mongod restart
Now if you run the mongo mongo command, you will still be shown the mongo shell prompt, but will not be able to perform any fundamental queries. Try running the show databases command. You should get an “not authorized on admin to execute command” error. Type exit to exit the shell. You can enter the mongo shell with authentication by running the following command:
mongo -u your_admin_username -p your_admin_password --authenticationDatabase admin
You should again be prompted with the mongo shell. This time, if you run show databases, you should see a list of all databases.
You’re all set up to connect to your MongoDB server using authentication now. If you have MongoDB installed on your local machine, you should be able to connect to the remote database using the following command:
mongo -u your_admin_user -p your_admin_password --authenticationDatabase admin your_ip_address
Replace your_public_ip_address with either your Stack’s IP address or a domain name which is pointed at the Stack. Most of the time, you won’t be reading/writing data use the mongo shell. MongoDB has provided a list of drivers for a number of languages that can be used to connect your application to your MongoDB instance.
Final Words
Congratulations! You’re on your way to storing structured, semi-structured, and unstructured data using the flexibility provided by MongoDB. Check out the excellent documentation provided by the MongoDB organization for more information on how to utilize the power of NoSQL. For more information on database and server administration, check out our Community Section. From all of us at Stack Harbor, ahoy!