Slide 1

Slide 1 text

PHP + MongoDB by @laurenceputra for Singapore PHP User Group

Slide 2

Slide 2 text

Instasyncer HA Database, with automatic fallover

Slide 3

Slide 3 text

Geekcamp.SG HA Database, with automatic fallover

Slide 4

Slide 4 text

Including freelance work, and personal projects Other random projects

Slide 5

Slide 5 text

What is HA?

Slide 6

Slide 6 text

HA for WHAT? Doesn’t it cost more?

Slide 7

Slide 7 text

What if AWS goes down?

Slide 8

Slide 8 text

If you want web-scale db’s

Slide 9

Slide 9 text

Go and use /dev/null It has the fastest write speeds, and is replicated on ALL unix based systems all over the world. Instant replication :D

Slide 10

Slide 10 text

So why should YOU use MongoDB?

Slide 11

Slide 11 text

It’s AWESOME It’s FLEXIBLE It’s FAST

Slide 12

Slide 12 text

And it’s really easy to do all those hardcore DevOps stuff that you’ve been hearing about. Stuff like High Availability, Sharding, and Backups ...

Slide 13

Slide 13 text

... Add in the fact that you can add in new fields any time. Fast. ...

Slide 14

Slide 14 text

... and backup your entire database without facing performance issues

Slide 15

Slide 15 text

Unsafe writes? "We try to make it clear in the documentation, but some people never notice that there’s a “safe” option for writes (that defaults to false), and then get very pissed when something wasn’t written."

Slide 16

Slide 16 text

Plus, does this look familiar? $mysqli->query("INSERT blah blah blah"); $mysqli->query("UPDATE blah blah blah");

Slide 17

Slide 17 text

So... Are you convinced?

Slide 18

Slide 18 text

MongoDB mongodb.org

Slide 19

Slide 19 text

Installing and using MongoDB is as simple as downloading the file and typing /path/to/mongod in your terminal (starts the mongo server) Of course there's a whole lot of other ways to do it (RPM, Brew, ...)

Slide 20

Slide 20 text

And to use it you simply need to type /path/to/mongo  

Slide 21

Slide 21 text

And MongoDB is now set up on your server. Tons of awesome documentation over at http://www.mongodb.org/display/DOCS/ Home *they have some of the best documentation I’ve ever seen

Slide 22

Slide 22 text

PHP Drivers (sudo)  pecl  install  mongo Plus add in extension=mongo.so in php.ini

Slide 23

Slide 23 text

That’s it. Your PHP Webapp can now use MongoDB Setup done.

Slide 24

Slide 24 text

That wasn’t that long right?

Slide 25

Slide 25 text

It gets better.

Slide 26

Slide 26 text

Basic syntax $conn  =  new  Mongo(); $db  =  $conn-­‐>php_meetup; $collection  =  $db-­‐>members;

Slide 27

Slide 27 text

$member  =  array( 'name'  =>  'Laurence',   'age'  =>  22 ); $settings  =  array('safe'  =>   MONGO_SAFE_LEVEL); $collection-­‐>insert($member,  $settings);

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Oops, I forgot a field! And I now have a billion members, which will make "Alter   TABLE" on mySQL take forever to run, making my webapp super laggy for the next hour or so.

Slide 30

Slide 30 text

$query  =  array('name'  =>  'Laurence'); $update  =  array( '$set'  =>  array( 'twitter'  =>  '@laurenceputra' )); $settings  =  array('safe'  =>  MONGO_SAFE_LEVEL); $collection-­‐>update($query,  $update,   $settings); With MongoDB

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Now to Scale!

Slide 33

Slide 33 text

Architecture of a production system mongod -configsvr

Slide 34

Slide 34 text

In MongoDB, sharding is the tool for scaling a system, and replication is the tool for data safety, high availability, and disaster recovery.

Slide 35

Slide 35 text

What is sharding?

Slide 36

Slide 36 text

Splitting of data into logical Chunks based on a predefined Shard Key. Max 64 MB per chunk Choose your shard key VERY carefully

Slide 37

Slide 37 text

Machine 1 Machine 2 Machine 3 Alabama → Arizona Colorado → Florida Arkansas → California Indiana → Kansas Idaho → Illinois Georgia → Hawaii Maryland → Michigan Kentucky → Maine Minnesota → Missouri Montana → Montana Nebraska → New Jersey Ohio → Pennsylvania New Mexico → North Dakota Rhode Island → South Dakota Tennessee → Utah Vermont → West Virgina Wisconsin → Wyoming

Slide 38

Slide 38 text

mongos : to determine which mongod to send request to mongod --configsvr : stores information on what each shard contains (consumes little resources) mongod : a shard (normally a part of a replica set) containing the data

Slide 39

Slide 39 text

Want to know more?

Slide 40

Slide 40 text

RTFM!

Slide 41

Slide 41 text

Replica Sets?

Slide 42

Slide 42 text

Simply creates multiple copies of the data on multiple servers/nodes, such that if any server/node goes down, the database is still up.

Slide 43

Slide 43 text

Of course, if 2012 happens, you’re out of luck ;)

Slide 44

Slide 44 text

mongod  -­‐-­‐port  27017  -­‐-­‐dbpath  $HOME/mongodb/ data/  -­‐-­‐logpath  $HOME/mongodb/logs/ mongodb.log  -­‐-­‐logappend  -­‐-­‐fork  -­‐-­‐replSet   demo1  -­‐-­‐oplogSize  50  -­‐-­‐keyFile  $HOME/ mongokey

Slide 45

Slide 45 text

rs.initiate() rs.add('mongodb2.cloudapp.net:27017') rs.add('mongodb3.cloudapp.net:27017') var  c  =  rs.config() c.members[0].priority  =  7 rs.reconfig(c)

Slide 46

Slide 46 text

db.addUser('admin','demopassword') db.auth('admin','demopassword')

Slide 47

Slide 47 text

mongo -u admin -p demopassword --host YOUR_URL_HERE --port 27017 admin

Slide 48

Slide 48 text

Demo Servers provided by They actually have customer support

Slide 49

Slide 49 text

Architecture of a production system (recap) mongod -configsvr

Slide 50

Slide 50 text

The whole stack (mine) Request App server mongod App server mongod Server Server Server specs Space: 100GB RAM: 256MB Bandwidth: 600GB RAM quota excludes OS/ Apache/mySQL/PgSQL Sign up here: http://bit.ly/Nz252v mongod

Slide 51

Slide 51 text

Automated tasks 0  0  *  *  *  $HOME/webapps/mongodb/mongo/ mongodump  -­‐u  backup  -­‐p  MYPASSWORD!  -­‐-­‐oplog   -­‐-­‐port  19904  -­‐o  $HOME/dump;  git  -­‐-­‐git-­‐dir= $HOME/dump/.git  -­‐-­‐work-­‐tree=$HOME/dump  add   -­‐A;  git  -­‐-­‐git-­‐dir=$HOME/dump/.git  -­‐-­‐work-­‐ tree=$HOME/dump  commit  -­‐m  "updated  backup";   git  -­‐-­‐git-­‐dir=$HOME/dump/.git  -­‐-­‐work-­‐tree= $HOME/dump  push

Slide 52

Slide 52 text

Automated tasks 5,15,25,35,45,55  *  *  *  *  $HOME/webapps/ mongodb/mongo/mongod  -­‐-­‐port  19904  -­‐-­‐dbpath   $HOME/webapps/mongodb/data/  -­‐-­‐logpath   $HOME/webapps/mongodb/logs/mongodb.log  -­‐-­‐ logappend  -­‐-­‐fork  -­‐-­‐replSet  laurenceputra1   -­‐-­‐oplogSize  50  -­‐-­‐keyFile  $HOME/mongokey

Slide 53

Slide 53 text

Automated tasks 0  0  *  *  *  killall  -­‐SIGUSR1  mongod

Slide 54

Slide 54 text

Automated tasks 0  1  1  *  *  rm  $HOME/webapps/mongodb/logs/ mongodb.log.*

Slide 55

Slide 55 text

limitations of MongoDB No full text search More than 100 shards untested No random sorting

Slide 56

Slide 56 text

GeekcampSG - 18th August Website - Geekcamp.SG

Slide 57

Slide 57 text

The End @laurenceputra geeksphere.net