As I’m busy setting up my Ubuntu server, I’m going to revisit a few topics that I’ve covered in the past, to see whether there are changes to working with various servers. Specifically I’ve gone Docker crazy and want to run these various server in Docker.
First up, let’s see what we need to do to get a basic MongoDB installation up and running and the C# client to access it (it seems some things have changes since I last did this).
Getting the server up and running
First off we’re going to get the Ubuntu server setup with an instance of MongoDB. So let’s get latest version on mongo for Docker
docker pull mongo:latest
this will simply download the latest version of the MongoDB but not run it. So our next step is to run the MongoDB Docker instance. By default the port MongoDB uses is 27017, but this isn’t available to the outside world. So we’re going to want to map this to a port accessible to our client machine(s). I’m going to use port 28000 (there’s no specific reason for this port choice). Run the following command from Ubuntu
docker run -p 28000:27017 --name my-mongo -d mongo
We’ve mapped MongoDB to the previously mentioned port and named the instance my-mongo. This will run MongoDB in the background. We can now look to write a simple C# client to access the instance.
Interactive Shell
Before we proceed to the client, we might wish to set-up users etc. in MongoDB and hence run its shell. Now running the following
docker exec -t my-mongo mongo
Didn’t quite work as expected, whilst I was placed inside the MongoDB shell, commands didn’t seem to run.
Note: This could be something I’m missing here, but when pressing enter, the shell seemed to think I was about to add another command.
To work with the shell I found it simpler to connect to the Docker instance using bash, i.e.
docker exec -t my-mongo bash
then run
mongo
to access the shell.
I’m not going to set up any users etc. at this point, we’ll just used the default setup.
Creating a simple client
Let’s fire up Visual Studio 2015 and create a console application. Then using NuGet add the MongoDB.Driver by MongoDB, Inc. Now add the following code to your Console application
public class Person { public ObjectId Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } class Program { static void Main(string[] args) { var client = new MongoClient("mongodb://xxx.xxx.xxx.xxx:28000"); var r = client.GetDatabase("MyDatabase"); var collection = r.GetCollection<Person>("People"); collection.InsertOne(new Person { FirstName = "Scooby", LastName = "Doo", Age = 27 }); }
Obviously replace the xxx.xxx.xxx.xxx with the IP address of your server (in my case my Ubuntu server box), the port obviously matches the port we exposed via Docker. You don’t need to “create” the database explicitly via the shell or a command, you can just run this code and it’ll create MyDatabase then the table People and then insert a record.
Did it work?
Hopefully your Console application just inserted a record. There should have been no timeout or other exception. Ofcourse we can use the Console application, for example
var client = new MongoClient("mongodb://xxx.xxx.xxx.xxx:28000"); var r = client.GetDatabase("MyDatabase"); var collection = r.GetCollection<Person>("People"); foreach (var p in collection.FindSync(_ => true).ToList()) { Console.WriteLine($"{p.FirstName} {p.LastName}"); }
I’m using the synchronous methods to find and create the list, solely because my Console application is obviously pretty simple, but the MongoDB driver library offers Async versions of these methods as well.
The above code will write out Scooby Doo as the only entry in our DB, so all worked fine. How about we do the same thing using the shell.
If we now switch back to the server and if its not running, run the MongoDB shell as previously outlined. From the shell run the following
use MyDatabase db.People.find()
We should now see a single entry
{ "_id" : ObjectId("581d9c5065151e354837b8a5"), "FirstName" : "Scooby", "LastName" : "Doo", "Age" : 27 }
Just remember, we didn’t set this instance of MongoDB up to use a Docker Volume and hence when you remove the Docker instance the data will disappear.
So let’s quickly revisit the code to run Mongo DB within Docker and fix this. First off exit back to the server’s prompt (i.e. out of the Mongo shell and out of the Docker bash instance).
Now stop my-mongo using
docker stop my-mongo
You can restart mongo at this point using
docker start my-mongo
and your data will still exist, but if you run the following after stopping the mongo instance
docker rm my-mongo
and execute Mongo again the data will have gone. If we add a volume command to the command line argument, and so we will execute the following
docker run -p 28000:27017 -v mongodb:/data/mongodb --name my-mongo -d mongo
the inclusion of the /v will map the mongodb data (/data/mongodb) to the volume on the local machine named mongodb. By default this is created in /var/lib/docker/volumes, but ofcourse you could supply a path to an alternate location
Remember, at this point we’re still using default security (i.e. none), I will probably create a post on setting up mongo security in the near future