Look at the MongoDB cluster configuration, but never mind how the client is connected. You can also use MONGOs and config server when you are not using fragmentation.
When testing the configuration, see, this is not the case. Config server is not configurable when there is no share.
At the same time in this configuration, also encountered access rights issues, the main still did not understand the use of Cluster Server keyfile. (added keyfile, must log in with user/password, function similar to-auth)
Make a note here.
MongoDB Configuration 2 Data node +1 quorum node cluster
1 establishment of the relevant directory:
Mkdir/opt/mongodb/key
Mkdir/opt/mongodb/config
Mkdir/opt/mongodb/db1
Mkdir/opt/mongodb/db2
Mkdir/opt/mongodb/db3
2. Create key file
2.1 Adding files
echo "This is key file" >> R1
echo "This is key file" >> R2
echo "This is key file" >> R3
2.2 Configure permissions, set to read-only
chmod R1 R2 R3
3. Start process
/opt/mongodb/mongodb-linux-x86_64-2.5.5/bin/mongod--replset rs1--keyfile/opt/mongodb/key/r1-dbpath=/opt/mongodb /db1-logpath=/opt/mongodb/logs/mongodb_rep1.log--logappend--directoryperdb--fork--journal--port 28011
/opt/mongodb/mongodb-linux-x86_64-2.5.5/bin/mongod--replset rs1--keyfile/opt/mongodb/key/r2-dbpath=/opt/mongodb /db2-logpath=/opt/mongodb/logs/mongodb_rep2.log--logappend--directoryperdb--fork--journal--port 28012
/opt/mongodb/mongodb-linux-x86_64-2.5.5/bin/mongod--replset rs1--keyfile/opt/mongodb/key/r3-dbpath=/opt/mongodb /db3-logpath=/opt/mongodb/logs/mongodb_rep3.log--logappend--directoryperdb--fork--journal--port 28013
(Note: The--DIRECTORYPERDB parameter has been added and will not succeed if it is not added)
4. Initialize replica set
Connect one of the Mongod (for example:./mongo localhost:28011) with MONGO:
> config = {_id: "Rs1", Members:[{_id:0,host: ' localhost:28011 '},{_id:1,host: ' localhost:28012 '}]}
--, {_id:2,host: ' localhost:28013 ', ' arbiteronly ': true}
> rs.initiate (config);
You can also configure it by adding a third node in a different way
Because the cluster is configured on the same server, the 3rd node is set to be a quorum node to conserve disk space (no data)
Rs.add ({_id:2, "host": "localhost:28013", "Arbiteronly": true});
View status after completion:
Rs1:primary> Rs.status ()
{
"Set": "Rs1",
"Date": Isodate ("2014-06-04t15:24:37z"),
"MyState": 1,
"Members": [
{
"_id": 0,
"Name": "localhost:28011",
"Health": 1,
"State": 1,
"Statestr": "PRIMARY",
"Uptime": 454,
"Optime": Timestamp (1401895370, 1),
"Optimedate": Isodate ("2014-06-04t15:22:50z"),
' Self ': true
},
{
"_id": 1,
"Name": "localhost:28012",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 140,
"Optime": Timestamp (1401895370, 1),
"Optimedate": Isodate ("2014-06-04t15:22:50z"),
"Lastheartbeat": Isodate ("2014-06-04t15:24:35z"),
"Lastheartbeatrecv": Isodate ("2014-06-04t15:24:35z"),
"Pingms": 0,
"Syncingto": "localhost:28011"
},
{
"_id": 3,
"Name": "localhost:28013",
"Health": 1,
"State": 7,
"Statestr": "Arbiter",
"Uptime": 107,
"Lastheartbeat": Isodate ("2014-06-04t15:24:36z"),
"Lastheartbeatrecv": Isodate ("2014-06-04t15:24:35z"),
"Pingms": 0
}
],
"OK": 1
}
Test:
First insert a record in the test table
> Db.test.insert (_id:1,userid:1,name: ' Tang ');
2014-06-04t11:35:24.335+0000 syntaxerror:unexpected Token:
> Db.test.insert ({_id:1,userid:1,name: ' Tang '});
Singlewriteresult ({
"Writeerrors": [],
"Writeconcernerrors": [],
"Ninserted": 1,
"nupserted": 0,
"nupdated": 0,
"Nmodified": 0,
"Nremoved": 0,
"upserted": []
})
> Db.test.find ()
{"_id": ObjectId ("5302c433b5b61e345817fb07"), "userid": 1, "name": "Maketion Ltd", "email": "admin@maketion.com", "PA ssWOrd ": PSD"}
{"_id": ObjectId ("53871082ed9422a9e22abc0c"), "Company": ""}
{"_id": ObjectId ("53871098ed9422a9e22abc0d"), "Company": {"_mongo": {"Slaveok": false, "host": "localhost:28001", ' Authstatus ': {' authrequired ': true, ' replsetgetstatus ': false, ' IsMaster ': true}, ' _usewritecommands ': true}, ' _d B ": {" _mongo ": {" Slaveok ": false," host ":" localhost:28001 "," Authstatus ": {" authrequired ": true," Replsetgetstat US: false, "IsMaster": true}, "_usewritecommands": true}, "_name": "MDSP"}, "_shortname": "A.name", "_fullname": "Mdsp.a.name"}, "name": "Maketion Ltd"}
{"_id": 1, "userid": 1, "name": "Tang"}
>
The following data file copies are based on the parameters (--DIRECTORYPERDB)
1. Close all nodes,
2. After the completion of the cluster configuration, the stand-alone environment of the database directory, copy to the node database directory, and protect the properties of the data file.
CP-PR DB/MDSP db1/
CP-PR DB/MDSP db2/
3. Reboot each node and check if the database is consistent with the original single node after entry.
4. If the MongoDB server starts with a--auth or--keyfile parameter, you must authenticate before any action is made. You can authenticate at the time of connection. method is to specify the username password in the link string.
Alternatively, specify "username" and "password" in the Mongoclient::__construct () constructor.
The above is an official statement. I didn't know this before. KeyFile must be logged in with a user/password. I've been doing this for a long time.
MONGO Localhost:28011-u root-p Root login, see no hint error.
The following error message is not logged in using a user/password:
Description of the problem------------------------------encountered------------------------
The server did not add--auth boot,
When MONGO log in, there is also a hint that there is no permission, which may be related to the authorization above. Contact the above topic, presumably this command is this function.
The following are the error prompts you have encountered. I want to inspire you.
In the cluster environment, start to participate in also did not add auth. But when logging on, say no permissions. The error is as follows:
[Mongo@localhost bin]$./mongo-port 28011/admin
MongoDB Shell version:2.5.5
2014-06-05t10:53:18.292+0000 error:invalid Port number "28011/admin" in connection string "127.0.0.1:28011/admin/test" At src/mongo/shell/mongo.js:135
Exception:connect failed
[Mongo@localhost bin]$./mongo localhost:28011/admin
MongoDB Shell version:2.5.5
Connecting To:localhost:28011/admin
Error while trying to show server startup Warnings:not authorized in Admin to execute command {getLog: "startupwarnings" }
Rs1:secondary> exit
Bye
[Mongo@localhost bin]$./mongo localhost:28012/admin
MongoDB Shell version:2.5.5
Connecting To:localhost:28012/admin
Error while trying to show server startup Warnings:not authorized in Admin to execute command {getLog: "startupwarnings" }
rs1:primary> Use admin
Switched to DB admin
Rs1:primary> Show Collections;
2014-06-05t10:54:43.063+0000 Error: {
' $err ': ' Not authorized to query on Admin.system.namespaces ',
"Code": 13
} at src/mongo/shell/query.js:131
Rs1:primary>
See the log has a user: "__system", Key: "XXX" This is the default or what.
2014-06-05t10:57:22.688+0000 [conn14] End Connection 127.0.0.1:34299 (3 connections now OPEN)
2014-06-05t10:57:22.689+0000 [Initandlisten] connection accepted from 127.0.0.1:34301 #15 (4 connections now OPEN)
2014-06-05t10:57:22.689+0000 [CONN15] Authenticate db:local {authenticate:1, nonce: "xxx", User: "__system", Key: "xxx " }
2014-06-05t10:57:52.696+0000 [conn15] End Connection 127.0.0.1:34301 (3 connections now OPEN)
2014-06-05t10:57:52.696+0000 [Initandlisten] connection accepted from 127.0.0.1:34303 #16 (4 connections now OPEN)
2014-06-05t10:57:52.698+0000 [Conn16] Authenticate db:local {authenticate:1, nonce: "xxx", User: "__system", Key: "xxx " }
2014-06-05t10:58:22.704+0000 [Conn16] End Connection 127.0.0.1:34303 (3 connections now OPEN)
2014-06-05t10:58:22.705+0000 [Initandlisten] connection accepted from 127.0.0.1:34305 #17 (4 connections now OPEN)
2014-06-05t10:58:22.706+0000 [CONN17] Authenticate db:local {authenticate:1, nonce: "xxx", User: "__system", Key: "xxx " }
2014-06-05t10:58:27.666+0000 [Clientcursormon] Mem (MB) res:69 virt:9643
2014-06-05t10:58:27.666+0000 [Clientcursormon] Mapped (incl Journal view): 8924
----------------End-------------------------------
5.php Connection Clustered Server example: a bit like Oracle TNS, a list of multiple server addresses for clients to access processing.
<?php
Using multiple servers as the seed list (prefered)
$m = new Mongoclient ("Mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaset=myreplsetname"));
Using one server as the seed list
$m = new Mongoclient ("mongodb://rs1.example.com:27017", Array ("Replicaset" => "Myreplsetname"));
Using multiple servers as the seed list
$m = new Mongoclient ("mongodb://rs1.example.com:27017,rs2.example.com:27017", Array ("Replicaset" =>) Myreplsetname "));
?>
You can also specify a read-optimized policy: Read Preferences
Can specify tag sets with the following read preference modes:
Mongoclient::rp_primary_preferred
Mongoclient::rp_secondary
Mongoclient::rp_secondary_preferred
Mongoclient::rp_nearest
Prefer the nearest server in the ' East ' data Center also used for reporting,
But fall back to a server in the ' West ' data center
$uri = ' mongodb://rs1.example.com,rs2.example.com/';
$uri. = ' Readpreference=nearest ';
$uri. = ' &readpreferencetags=dc:east,use:reporting ';
$uri. = ' &readpreferencetags=dc:west ';
$m = new Mongoclient ($uri, Array (' Replicaset ' => ' rs '));
More references are as follows:
http://www.php.net/manual/zh/mongo.connecting.rs.php