Recently pumped time to build a simple MongoDB shard, the whole process is quite smooth, but in the user authentication this piece encountered some problems, fortunately finally fix.
first, the server construction process:
1, install four mongodb: one as config, one as MONGOs, the other two as the main data storage server (machine ip192.168.0.201),
Mongoconf, MONGOs, Mongo1, Mongo2 (decompression installation, installation process omitted) corresponding to the red box in the diagram respectively.
2, set up the data, conf, logs folder, and in the logs folder to establish the Mongodb.log file, conf (boot configuration parameter) file. The contents of the Conf file in mongoconf are as follows:
dbpath=/home/admin/mongoconf/datalogpath=/home/admin/mongoconf/logs/mongodb.loglogappend=truefork=trueport= 30000
The contents of the Conf file in MONGOs are as follows (the DBPath designation is less than the top):
logpath=/home/admin/mongos/logs/mongodb.loglogappend=truefork=trueport=40000
The contents of conf in Mongo1 are as follows (basically the same as in mongoconf, but the path points to their own):
dbpath=/home/admin/mongo1/datalogpath=/home/admin/mongo1/logs/mongodb.loglogappend=truefork=trueport=37117
Mongo2 in the Conf is the same, just change mongo1 to Mongo2 (omitted).
3. Start mongoconf, MONGOs, and Mongo1, Mongo2: Start mongoconf, and execute the mongod command in the bin directory:
./mongod-f. /conf/mongodb.conf
To start MONGOs, execute the mongos command in the bin directory:
./mongos-f. /conf/mongodb.conf--configdb=192.168.0.201:30000
Start Mongo1 and Mongo2 and execute the mongod command in the respective bin directory:
./mongod-f. /conf/mongodb.conf
4, connect the MONGOs server, and add Shards: (1), connect MONGOs: In any of the MongoDB bin directory to execute the following command:
./mongo 192.168.0.201:40000/admin
(2) After entering MONGOs, add mongo1 and Mongo2 bit shard data server:
Db.runcommand ({"Addshard": "192.168.0.201:37117"}) Db.runcommand ({"Addshard": "192.168.0.201:47117"})
5. Turn on the Shard:
Db.runcommand ({"enablesharding": "Test"}) Db.runcommand ({"Shardcollection": "Test.user", "key": {"name": 1}})
6. Test: Add 10w data to the user table in the test database:
Use Testfor (var i=0;i<100000;i++) { Db.user.insert ({"Name": "Test" +i, "age": i});}
7. View Shard Results:
Db.printshardingstatus ()
Second, open user authentication:
1. Switch database to admin to create root user:
Db.createuser ({"User": "Root", "pwd": "123456", "Roles": ["Root"]})
The user who then switches to the test database to create read and Write permissions (unable to create cluster management related users in the non-admin database):
Db.createuser ({"User": "Test", "pwd": "123456", "roles": [{"DB": "Test", "role": "ReadWrite"}, "ReadWrite"]})
2, exit MONGO shell, create keyfile (file name can pick up) file, and give 600 permissions, must have 600 permissions: Enter into the MONGO1 data directory to execute the following command:
OpenSSL rand-base64 753 > Keyfilechmod keyfile
3. Copy the generated keyfile file to a few other MongoDB data directories:
4. Modify mongodb.conf Boot profile: Except MONGOs, add the following (Turn on user authentication and keyfile authentication):
Auth=truekeyfile=/home/admin/mongo2/data/keyfile
There is no auth parameter in MONGOs, only keyfile is required.
5. Start again in turnmongoconf, MONGOs and Mongo1, Mongo2 (refer to the starting instructions above);
Third, Java Code Connection test:
1. The Java Connection code is as follows:
public class Montest {public static void main (string[] args) {mongodbfind2 (); public static void Mongodbfind2 () {serveraddress sa = new ServerAddress ("192.168.0.201", 40000); list<mongocredential> mongocredentiallist = new arraylist<mongocredential> (); Mongocredentiallist.add (mongocredential.createcredential ("Test", "Test", "123456". ToCharArray ())); Mongoclient client = new Mongoclient (SA, mongocredentiallist); Mongodatabase database = client.getdatabase ("test"); Mongocollection Collection2 = database.getcollection ("user"); finditerable<document> finditerable = Collection2.find (); mongocursor<document> Cursor2 = Finditerable.iterator (); while (Cursor2.hasnext ()) {Document doc = Cursor2.next (); System.out.println (Doc.get ("_id") + "," + doc.get ("name") + "," + doc.get ("age"); }//Close database connection Client.close (); }}
2. Operation Result:
Finish it!
mongodb3.0 Shard and Java Code Connection Operation test (turn on user authentication)