Tag Archives: db

mongodb: replica master-slave + creating of db and collection

Replica master-slave

master
add to mongod.conf:
master = true
client
add to mongod.conf:
slave = true
source = $masterHost:$masterPort #(for example 127.0.0.1:27017)

stop master
rsync folder with db from master to slave
start master
start slave

Creating of db and collection

# open mongo shell
mongo --host 127.0.0.1:27017
# use db name which you'd like to create
use newDbName
# data which you'd like to add to collection
j = { name : «mongo» }
# insert previous data to collection
db.testData.insert ( j )

# check that db/collection/data were created
show dbs
show collections
db.testData.find ()

check replica

# open mongo shell on slave
mongo --host 10.0.0.2:27017
# check that db/collection/data were created
show dbs
show collections
db.testData.find ()

PHP: DBConfig Class

This is a short note, my own bicycle:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
class DBConfig{
    private $hostname;
    private $port;
    private $username;
    private $password;
    private $dbname;
   
    public function &__get($property) {
        if(property_exists($this, $property)){
            return $this->$property;
        }
    }

    public function __set($property, $value) {
        if(property_exists($this, $property)){
            $this->$property = $value;
        }
    }
}
?>

How to use:

1
2
3
4
5
6
    $dbconf = new DBConfig();
    $dbconf->hostname = "127.0.0.1";
    $dbconf->port = 6432;
    $dbconf->username = "user_ro";
    $dbconf->password = "user_ro_pass";
    $dbconf->dbname = "test_db";

Then you could use it where do you want:
pgsql:

1
$dbconn = pg_connect("host = $dbconf->hostname port = $dbconf->port, dbname = $dbconf->dbname, user = $dbconf->username, password = $dbconf->password options='--client_encoding=UTF8'");