-
Notifications
You must be signed in to change notification settings - Fork 52
Connections
To create a new connection that may connect to a remote host or require authentication, initialise an instance of Shanty_Mongo_Connection and pass the connection string to the constructor. For more information about connection strings see php’s mongo manual.
$connection = new Shanty_Mongo_Connection('mongodb://user:[email protected]:port');
Shanty_Mongo::addMaster($connection);
If no slaves are provided then reads will go to the masters.
Replica Sets
$connections = array
hosts' => array(
array(
'host' => 'server1',
'username' => 'USERNAME',
'password' => 'PASSWORD',
),
array(
'host' => 'server2',
),
array(
'host' => 'server3'
)
),
'replicaSet' => true
);
Shanty_Mongo::addConnections($connections);
$connections = array
hosts' => array(
array(
'host' => 'server1',
'username' => 'USERNAME',
'password' => 'PASSWORD',
),
array(
'host' => 'server2',
),
array(
'host' => 'server3'
)
),
'replicaSet' => true
);
Shanty_Mongo::addConnections($connections);
Now we’ll create a typical master slave setup.
$master = new Shanty_Mongo_Connection('mongo-master.domain.local');
$slave1 = new Shanty_Mongo_Connection('mongo-slave1.domain.local');
$slave2 = new Shanty_Mongo_Connection('mongo-slave2.domain.local');
Shanty_Mongo::addMaster($master);
Shanty_Mongo::addSlave($slave1);
Shanty_Mongo::addSlave($slave2);
Connections are lazy loaded. Shanty_Mongo_Connection’s will only connect to the server the first time they are used. This is to prevent all connections connecting even if they are never used
By the way master-master setups are also possible but not recommended.
Say for example you have a supa dupa server that can process 3x the requests of the other servers. You can weight that connection. eg
Shanty_Mongo::addSlave($supaDupaServer, 3);
By default connections are given a weight of 1.
Sometimes you may wish to connect to multiple mongodb servers that are not in a cluster, most likely because they store different data to one and other. To do this you can use connection groups.
By default all connections get added to the ‘default’ group. We can easily add connections to other groups by providing the group name as the third parameter.
Shanty_Mongo::addMaster($connection, 1, 'users');
Here we added a connection to the ‘users’ connection group. Next we’ll have to tell our documents to use that connection group instead of the default connection group. This is done by specifying the static property $_connectionGroup in the document.
class User extends Shanty_Mongo_Document
{
protected static $_connectionGroup = 'users';
}
The first time a read request is made, a connection will be selected from the pool of slaves and cached. The same connection will then be used in subsequent requests. The same applies to write requests.
If you would like a new connection selected for every request set the ‘cacheConnectionSelection’ to false on the connection stack of your choice. Eg
Shanty_Mongo::getConnectionGroup('default')->getSlaves()->cacheConnectionSelection(false);
The above sets the ‘cacheConnectionSelection’ flag to false for the slaves connection stack in the default connection group
To do this call Shanty_Mongo::addConnections supplying either an instance of Zend_Config or an array
host = 'mongohost.local'
port = '27017'
username = 'coolapp'
password = 'sexytime'
database = 'coolapp'
You do not have to provide all of the options above, only the options you need.
master.host = 'mongomaster.local'
slave.host = 'mongoslave.local'
masters.0.host = 'mongomaster1.local'
masters.1.host = 'mongomaster2.local'
slaves.0.host = 'mongoslave1.local'
slaves.1.host = 'mongoslave1.local'
users.host = 'usersdb.local'
assets.master.host = 'assetsmaster.local'
assets.slaves.0.host = 'assetsslave1.local'
assets.slaves.1.host = 'assetsslave2.local'
assets.slaves.2.host = 'assetsslave3.local'
The above will create 2 connection groups ‘users’ and ‘assets’. ‘users’ only has one mongodb server while ‘assets’ has one master and 3 slaves.
assets.slaves.0.weight = 3
The first slave now has a weight of 3.
assets.slaves.cacheConnectionSelection = false
Now a new slave will be selected for each request