Installing MongoDB 1.6 on Ubuntu 10.04 and PHP 5
These steps should help you quickly install the latest stable version of MongoDB on Ubuntu and PHP:
* Note: this guide assumes you already have LAMP installed and functional.
1. Run command: sudo gedit /etc/apt/sources.list
2. Paste this at bottom of file: deb http://downloads.mongodb.org/distros/ubuntu 10.4 10gen
3. Run command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
4. Run command: sudo mkdir -p /data/db/
5. Run command: sudo chown `id -u` /data/db
* Note: that's a ` (keyboard key to left of the 1 key) not a ' (keyboard key to left of Enter key)
6. Run command: sudo apt-get update
* Note: for some reason the first update didn't work so I had to run the command a second time
7. Run command: sudo apt-get install mongodb-stable
8. Wait for the installation to finish. To test installation was successful, run command: mongo
9. Run command: sudo apt-get install php-pear
10. Run command: sudo apt-get install php5-dev
11. Run command: sudo pecl install mongo
12. Run command: sudo gedit /etc/php.ini
13. At bottom of the php.ini file (or first line if file is empty), add this line: extension=mongo.so
14. Restart your web server for the change to take effect (e.g. sudo service apache2 restart)
15. You can now start using MongoDB with PHP!
P.S. An important security discussion can be found here.
* Note: this guide assumes you already have LAMP installed and functional.
1. Run command: sudo gedit /etc/apt/sources.list
2. Paste this at bottom of file: deb http://downloads.mongodb.org/distros/ubuntu 10.4 10gen
3. Run command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
4. Run command: sudo mkdir -p /data/db/
5. Run command: sudo chown `id -u` /data/db
* Note: that's a ` (keyboard key to left of the 1 key) not a ' (keyboard key to left of Enter key)
6. Run command: sudo apt-get update
* Note: for some reason the first update didn't work so I had to run the command a second time
7. Run command: sudo apt-get install mongodb-stable
8. Wait for the installation to finish. To test installation was successful, run command: mongo
You should now be connected to test (if not, run command: sudo service mongodb restart)
Type help for a list of options
Run command: db.myTestCollection.save({test:1});
Now we have a new collection named myTestCollection. It has one document in it called test, which is expressed as an object with one attribute (1):
{
test: 1
}
Run command: db.myTestCollection.save({test:2});
Now our test document has two attributes:
{
test: 1
test: 2
}
You can see these values by running this command: db.myTestCollection.find(); This will return all documents within the myTestCollection collection:
{ "_id" : ObjectId("4c5b8ebd13ba33c8ab9d0052"), "test" : 1 }
{ "_id" : ObjectId("4c5b8f7a13ba33c8ab9d0053"), "test" : 2 }
* Note: Mongo requires an unique ObjectId '_id' value for each attribute and will auto-create it for you if not provided (easiest and recommended).
If you want a specific attribute, you can identify it in the query. For example: db.myTestCollection.find({test:1});
{ "_id" : ObjectId("4c5b8ebd13ba33c8ab9d0052"), "test" : 1 }
I know... I know... The query syntax is a little weird at first because it's JavaScript-based and will probably seem foreign to traditional relational database admins. A nice comparison between familiar SQL and Mongo commands can be found here. Perhaps someone will eventually create a JavaScript library to convert traditional SQL queries into Mongo-style queries.
Type exit to quit the mongo console.
Now that MongoDB is functional, you need to configure PHP to use it:
9. Run command: sudo apt-get install php-pear
10. Run command: sudo apt-get install php5-dev
11. Run command: sudo pecl install mongo
12. Run command: sudo gedit /etc/php.ini
13. At bottom of the php.ini file (or first line if file is empty), add this line: extension=mongo.so
14. Restart your web server for the change to take effect (e.g. sudo service apache2 restart)
15. You can now start using MongoDB with PHP!
P.S. An important security discussion can be found here.
Great guide, thank you!
ReplyDeleteHello there,
ReplyDeleteI have tried installed as you suggested, it installed successfully, also ran command successfully as you specified for "mongo" in console.
but if I try running this code http://www.php.net/manual/en/mongo.tutorial.php I am getting Fatal error.
Please help. how to fix this issue ?
NOTE : I installed it successfully, can run from console, but if I search "mongo" in phpinfo, I can't see it.
ReplyDeletePlease help
oops, my mistake,
ReplyDeleteI was updating
/etc/php5/apache2/php.ini
but actually
/etc/php5/apache2filter/php.ini
was called through my apache2
Regards for such post.