An introduction to using MongoDB with PHP.
Walking through the basics of schema design, connecting to a DB, performing CRUD operations and queries in PHP.
MongoDB runs great in the cloud, but there are some things you should know. In this session we'll explore scaling and performance characteristics of running Mongo in the cloud as well as best practices for running on platforms like Amazon EC2.
3. • 15+ years building the
internet
• Father, husband,
skateboarder
• Chief Solutions Architect @
10gen
• Author of upcoming O’Reilly
publication
“MongoDB and PHP”
19. MongoDB philosophy
• Keep functionality when we can (key/
value stores are great, but we need more)
• Non-relational (no joins) makes scaling
horizontally practical
• Document data models are good
• Database technology should run
anywhere VMs, cloud, metal, etc
20. MongoDB is:
Application Document
Oriented
{ author: “steve”,
High date: new Date(),
text: “About MongoDB...”,
Performance tags: [“tech”, “database”]}
Fully
Consistent
Horizontally Scalable
21. Under the hood
•Written in C++
•Runs on nearly anything
•Data serialized to BSON
•Extensive use of memory-mapped files
25. CMS / Blog
Needs:
• Business needed modern data store for rapid development and
scale
Solution:
• Use PHP & MongoDB
Results:
• Real time statistics
• All data, images, etc stored together, easy access, easy
deployment, easy high availability
• No need for complex migrations
• Enabled very rapid development and growth
26. Photo Meta-Data
Problem:
• Business needed more flexibility than Oracle could deliver
Solution:
• Use MongoDB instead of Oracle
Results:
• Developed application in one sprint cycle
• 500% cost reduction compared to Oracle
• 900% performance improvement compared to Oracle
27. Customer Analytics
Problem:
• Deal with massive data volume across all customer sites
Solution:
• Use MongoDB to replace Google Analytics / Omniture options
Results:
• Less than one week to build prototype and prove business case
• Rapid deployment of new features
28. Online Dictionary
Problem:
• MySQL could not scale to handle their 5B+ documents
Solution:
• Switched from MySQL to MongoDB
Results:
• Massive simplification of code base
• Eliminated need for external caching system
• 20x performance improvement over MySQL
29. E-commerce
Problem:
• Multi-vertical E-commerce impossible to model (efficiently) in
RDBMS
Solution:
• Switched from MySQL to MongoDB
Results:
• Massive simplification of code base
• Rapidly build, halving time to market (and cost)
• Eliminated need for external caching system
• 50x+ improvement over MySQL
42. Connecting to the DB
$connection = new Mongo();
$db = $connection->selectDB('blog');
$posts = $db->post;
If the DB or collection doesn’t exist,
mongoDB will create it on connect.
43. Documents
Blog Post Document
$p1 = array("author" => "roger",
"date" => new MongoDate(),
"text" => "about mongoDB...",
"tags" => array('tech', 'databases')
);
$posts->save($p1);
44. Querying
print_r($posts->findOne());
Array(
[_id] => MongoId Object(
[$id] => 4e9796764a18173a17000000
)
[author] => roger
[date] => MongoDate Object(
[sec] => 1318557302
[usec] => 581000
)
[text] => about mongoDB...
[tags] => Array(
[0] => tech
Note: _id is unique, but can be anything
[1] => databases
you’d like
)
)
45. Querying
> db.posts.findOne()
> { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author : "roger",
date : "Sat Jul 24 2010 19:47:11",
text : "About MongoDB...",
tags : [ "tech", "databases" ] }
Note: _id is unique, but can be anything
you’d like
46. Inserting Objects
class PostObj {
public $comments = array();
public function __construct($author, $title, $content)
{
$this->author = $author;
$this->title = $title;
$this->content = $content;
}
}
$posts->insert(new PostObj("Steve",
"My first blog post",
"Hello, World!"));
63. A better network FS
• GridFS files are seamlessly sharded & replicated.
• No OS constraints...
• No file size limits
• No naming constraints
• No folder limits
• Standard across different OSs
• MongoDB automatically generate the MD5 hash
of the file
72. Amazon EC2
Instance Types
32-bit = Don’t Use
Typical MongoD
ConfigD / Arbiter
Big MongoD
32-bit = Don’t Use
High CPU not
necessary
73. OS
• Amazon OS now an option
• Turn off atime
• Raise file descriptor limits
cat >> /etc/security/limits.conf << EOF
* hard nofile 65536
* soft nofile 65536
EOF
• DO NOT use large VM pages
• Use ext4, xfs
• Use RAID
– RAID10 on MongoD
– RAID1 on ConfigDB
• Warning! Known problems with Ubuntu
10.04 & EBS
• https://bugs.launchpad.net/ubuntu/+source/linux-ec2/+bug/
614853
– https://bugzilla.kernel.org/show_bug.cgi?id=16991
74. http://spf13.com
http://github.com/spf13
@spf13
Questions?
download at mongodb.org
PS: We’re hiring!! Contact us at jobs@10gen.com
Notas do Editor
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
Remember in 1995 there were around 10,000 websites. Mosiac, Lynx, Mozilla (pre netscape) and IE 2.0 were the only web browsers. \nApache (Dec &#x2019;95), Java (&#x2019;96), PHP (June &#x2019;95), and .net didn&#x2019;t exist yet. Linux just barely (1.0 in &#x2019;94)\n
Remember in 1995 there were around 10,000 websites. Mosiac, Lynx, Mozilla (pre netscape) and IE 2.0 were the only web browsers. \nApache (Dec &#x2019;95), Java (&#x2019;96), PHP (June &#x2019;95), and .net didn&#x2019;t exist yet. Linux just barely (1.0 in &#x2019;94)\n
Remember in 1995 there were around 10,000 websites. Mosiac, Lynx, Mozilla (pre netscape) and IE 2.0 were the only web browsers. \nApache (Dec &#x2019;95), Java (&#x2019;96), PHP (June &#x2019;95), and .net didn&#x2019;t exist yet. Linux just barely (1.0 in &#x2019;94)\n
\n
\n
\n
\n
By reducing transactional semantics the db provides, one can still solve an interesting set of problems where performance is very important, and horizontal scaling then becomes easier.\n\n