Install MongoDB on Windows 8

Share httpJunkie.com on..
Tweet about this on TwitterShare on FacebookShare on Google+Share on TumblrEmail this to someone

Installing MongoDB on Windows 7 or 8

1) Go to mongodb.org/downloads and select the option to download the latest product release forWindows and you will have an option of choosing 32 bit or 64 bit. A 32 bit install has a limitation of about 2 GB of data. For developing, 32 bit is ok, but for development purposes it will never be enough. If you machine uses 64 bit that is what we recommend you use. Any 32-bit program will run on a 64 bit OS. That’s why you have both a ‘program files’ and a ‘program files x86’ folder. The x86 refers to 32 bit programs. If you need to make sure your computer can handle 64 bit and you don’t know. open a command prompt and type

C:\>wmic os get osarchitecture

Download the 64 bit msi version to your computer. ANd once it’s done downloading, let’s run it.

Accept the terms of the license agreement, click next, typical installation is fine, install, give it permission to run if asked. Now click finish when it’s done.

First thing, Our mongodb folder has been installed to the Program files folder. Let’s go find that. Now you can either leave it here and interact with it at this directory level or you can do what I like to to do. Move it to the C drive. SInce I am going to be interacting with it a lot, I want it to be somewhere I can easily cd into it. So I am just going to copy and paste to my C drive. I am going to take one more step and rename the installation directory from “MongoDB 2.6 Standard” to “mongodb” If I want to know the version I can always find that out in the command line. Moving mongos installation directory will not affect it. Let’s go ahead and pull up our command prompt and navigate to the C:\ drive if not already there. remember you can always get back to your C drive by typing at the prompt

>cd C:\

now lets cd into the mongodb folder we just renamed:

C:\>cd mongodb

Let’s see what files are here by typing ls

C:\mongodb>ls

Let’s cd into our bin directory now.

C:\mongodb>cd bin

C:\mongodb\bin>ls (show the files in this directory)

mongo.exe  mongod.exe (these are the two executables we are interested in)

Let’s start mongod.exe

C:\mongodb\bin>mongod.exe

Now you will see an error when you do this and it’s because we have not specified a database folder somewhere on our computer or created on in the default area it looks for the data folder. So let’s do that now. Run both of these commands.

C:\mongodb\bin>mkdir \data

C:\mongodb\bin>mkdir \data\db

This will create a data directory on your C drive and a db directory within that data directory and this is where MongoDB will store your data. Now it is possible to set up a different directory, but upon spinning up your database like we are about to do you would have to specifically tell mongo each time where that alternate folder is, so we are not going to do that.

C:\mongodb\bin>mongod.exe

Now mongod.exe is running you won’t be able to type anything in the terminal window, let’s open another command line to C:\mongodb/bin again. Let’s now run mongo.exe which is the mongo shell. Keep our windows side by side to see what happens in each window.

C:\mongodb\bin>mongo.exe

In the command line running mongod.exe or the database shell, you will see that it just recognized your connection. Also notice that it tols us what version of mongo we are running over in the new window. Like I said, no reason to have a convoluted name for the mongodb folder, that is why we renamed it. It also shows that we connected to test. That is fine for right now as we are only testing the connection and functionality. Next, we could run a command that says show collections, but it would return nothing, because we have not done anything yet. So let’s run a command to insert something. Notice how we just have a command prompt now, that is because we are now inside the mongo shell, not our regular command line.

>db.names.insert({ name: "Eric"})
WriteResult({“nInserted” : 1})

the write result will show that we inserted a document. and if we look into the collection we just added called db.names we should find our document with an _id it will show a long id number and the name we inserted as Json.

. > db.names.find()
{ “_id” : ObjectId(“4253e635252626353a634c0da7a”), “name” : “Eric” }

Again if you were watching over on the other screen when you hit enter you would see that it added that item into a collection called names. In test.names we now have the name “Eric” that is in a sense what the shell said to us. I’m going to insert one more thing just for fun.

>db.names.insert({ occupation: "programmer"})
WriteResult({“nInserted” : 1})

now let’s issue a show collections command

>show collections
names
system.indexes

So above it basically shows us that we have a collection called names, let’s look inside it. Type

>db.names.find()

and it now shows us both items we inserted. Well that shows us that everything is working so lets hit Ctrl+C on the mongod shell to exit and  type >exit on the mongo shell to Exit. In all actuality you could use Ctr+C for both, but when you can’t type >exit because you have no command prompt, the easiest way to exit is to hit Ctrl+C while in any shell.

Remember that mongo.exe cannot be started without mongod.exe running in the first place.

 

Leave a Reply

You must be logged in to post a comment.