§2023-08-08
-
hc4Bookworm.yushei.net , show ShardA
-
hc4Lunar.yushei;net , show ShardB
-
n2MnJaro.yushei.net , show ConfigSercver
-
Run on orgpi5Arch.yushei.net as Mongos-27981.srvice
[Unit]
Description=MongoDB Sharding Router (mongos)
After=network.target
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/local/bin/mongos --bind_ip_all --port 27981 \
--configdb "ConfigServer/orgpi5jammy.yushei.net:27987,n2mnjaro.yushei.net:27987,hc4lunar.yushei.net:27987" \
--logpath /opt/xfs/mongodb/log/mongos-27981.log
[Install]
WantedBy=multi-user.target
[alexlai@orgpi5Arch ~]$ mongosh mongodb://orgpi5Arch.yushei.net:27981
Current Mongosh Log ID: 64d1b059330c609cd45dbd09
Connecting to: mongodb://orgpi5Arch.yushei.net:27981/?directConnection=true&appName=mongosh+1.10.1
Using MongoDB: 7.0.0-rc8
Using Mongosh: 1.10.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting
2023-08-08T11:02:18.683+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
[[direct: mongos] test> sh.status()
shardingVersion
{ _id: 1, clusterId: ObjectId("64d17cee5659ccac70b105c0") }
---
shards
[]
---
active mongoses
[]
---
autosplit
{ 'Currently enabled': 'yes' }
---
balancer
{ 'Currently enabled': 'yes', 'Currently running': 'no' }
---
databases
[
{
database: { _id: 'config', primary: 'config', partitioned: true },
collections: {}
}
]
To add the first shard to the cluster, execute the following command. In this example, shard1 is the replica set name of the first shard, and mongo_shard1_ip is the IP address of the server on which that shard, mongo-shard1, is running:
sh.addShard("ShardA/mongo_shard1_ip:27017")
If you don't know which member is the primary in a replica set and you want to add a shard to your mongos instance, you can simply provide any valid hostname and port from the replica set. MongoDB will automatically detect the primary node and add the shard to the cluster. Here's how you can do it:
javascript Copy code sh.addShard("ShardA/n2Mnjaro.yushei.net:27985"); sh.addShard("ShardB/n2Mnjaro.yushei.net:27983");
¶Test
$ mongos --bind_ip_all \
--configdb "ConfigServer/orgpi5jammy.yushei.net:27987,n2mnjaro.yushei.net:27987,hc4lunar.yushei.net:27987" --port 27981
Connet to mongos
[alexlai@orgpi5Arch ~]$ mongosh mongodb://orgpi5Arch.yushei.net:27981
Current Mongosh Log ID: 64d1ab711d20a73422c14ae5
Connecting to: mongodb://orgpi5Arch.yushei.net:27981/?directConnection=true&appName=mongosh+1.10.1
Using MongoDB: 7.0.0-rc8
Using Mongosh: 1.10.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting
2023-08-08T10:26:22.917+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
[direct: mongos] admin> use Learn
switched to db Learn
[direct: mongos] Learn> db.unicorns.insertOne({name: 'Aurora',gender: 'f', weight: 450});
{
acknowledged: true,
insertedId: ObjectId("64d1d759d275f01e8aedb444")
}
[direct: mongos] Learn> db.unicorns.insertOne({name: 'Leto', gender: 'm', home: 'Arrakeen', worm: false});
{
acknowledged: true,
insertedId: ObjectId("64d1d769d275f01e8aedb445")
}
[direct: mongos] Learn> db.unicorns.insertMany(
... [
... {name: 'Horny', dob: new Date(1992,2,13,7,47),loves: ['carrot','papaya'],weight: 600,gender: 'm',vampires: 63},
... {name: 'Aurora', dob: new Date(1991, 0, 24, 13, 0), loves: ['carrot', 'grape'], weight: 450, gender: 'f', vampires: 43},
... {name: 'Unicrom', dob: new Date(1973, 1, 9, 22, 10), loves: ['energon', 'redbull'], weight: 984, gender: 'm', vampires: 182},
... {name: 'Roooooodles', dob: new Date(1979, 7, 18, 18, 44), loves: ['apple'], weight: 575, gender: 'm', vampires: 99},
... {name: 'Solnara', dob: new Date(1985, 6, 4, 2, 1), loves:['apple', 'carrot', 'chocolate'], weight:550, gender:'f', vampires:80},
... {name:'Ayna', dob: new Date(1998, 2, 7, 8, 30), loves: ['strawberry', 'lemon'], weight: 733, gender: 'f', vampires: 40},
... {name:'Kenny', dob: new Date(1997, 6, 1, 10, 42), loves: ['grape', 'lemon'], weight: 690, gender: 'm', vampires: 39},
... {name: 'Raleigh', dob: new Date(2005, 4, 3, 0, 57), loves: ['apple', 'sugar'], weight: 421, gender: 'm', vampires: 2},
... {name: 'Leia', dob: new Date(2001, 9, 8, 14, 53), loves: ['apple', 'watermelon'], weight: 601, gender: 'f', vampires: 33},
... {name: 'Pilot', dob: new Date(1997, 2, 1, 5, 3), loves: ['apple', 'watermelon'], weight: 650, gender: 'm', vampires: 54},
... {name: 'Nimue', dob: new Date(1999, 11, 20, 16, 15), loves: ['grape', 'carrot'], weight: 540, gender: 'f'},
... {name: 'Dunx', dob: new Date(1976, 6, 18, 18, 18), loves: ['grape', 'watermelon'], weight: 704, gender: 'm', vampires: 165}
... ]
... );
{
acknowledged: true,
insertedIds: {
'0': ObjectId("64d1d782d275f01e8aedb446"),
'1': ObjectId("64d1d782d275f01e8aedb447"),
'2': ObjectId("64d1d782d275f01e8aedb448"),
'3': ObjectId("64d1d782d275f01e8aedb449"),
'4': ObjectId("64d1d782d275f01e8aedb44a"),
'5': ObjectId("64d1d782d275f01e8aedb44b"),
'6': ObjectId("64d1d782d275f01e8aedb44c"),
'7': ObjectId("64d1d782d275f01e8aedb44d"),
'8': ObjectId("64d1d782d275f01e8aedb44e"),
'9': ObjectId("64d1d782d275f01e8aedb44f"),
'10': ObjectId("64d1d782d275f01e8aedb450"),
'11': ObjectId("64d1d782d275f01e8aedb451")
}
}
[direct: mongos] Learn> db.unicorns.count()
DeprecationWarning: Collection.count() is deprecated. Use countDocuments or estimatedDocumentCount.
14
[direct: mongos] Learn> db.unicorns.countDocuments();
14
[direct: mongos] Learn> db.unicorns.find();
[
{
_id: ObjectId("64d1d759d275f01e8aedb444"),
name: 'Aurora',
gender: 'f',
weight: 450
},
{
_id: ObjectId("64d1d769d275f01e8aedb445"),
name: 'Leto',
gender: 'm',
home: 'Arrakeen',
worm: false
},
{
_id: ObjectId("64d1d782d275f01e8aedb446"),
name: 'Horny',
dob: ISODate("1992-03-12T23:47:00.000Z"),
loves: [ 'carrot', 'papaya' ],
weight: 600,
gender: 'm',
vampires: 63
},
{
_id: ObjectId("64d1d782d275f01e8aedb447"),
name: 'Aurora',
dob: ISODate("1991-01-24T05:00:00.000Z"),
loves: [ 'carrot', 'grape' ],
weight: 450,
gender: 'f',
vampires: 43
},
{
_id: ObjectId("64d1d782d275f01e8aedb448"),
name: 'Unicrom',
dob: ISODate("1973-02-09T14:10:00.000Z"),
loves: [ 'energon', 'redbull' ],
weight: 984,
gender: 'm',
vampires: 182
},
{
_id: ObjectId("64d1d782d275f01e8aedb449"),
name: 'Roooooodles',
dob: ISODate("1979-08-18T09:44:00.000Z"),
loves: [ 'apple' ],
weight: 575,
gender: 'm',
vampires: 99
},
{
_id: ObjectId("64d1d782d275f01e8aedb44a"),
name: 'Solnara',
dob: ISODate("1985-07-03T18:01:00.000Z"),
loves: [ 'apple', 'carrot', 'chocolate' ],
weight: 550,
gender: 'f',
vampires: 80
},
{
_id: ObjectId("64d1d782d275f01e8aedb44b"),
name: 'Ayna',
dob: ISODate("1998-03-07T00:30:00.000Z"),
loves: [ 'strawberry', 'lemon' ],
weight: 733,
gender: 'f',
vampires: 40
},
{
_id: ObjectId("64d1d782d275f01e8aedb44c"),
name: 'Kenny',
dob: ISODate("1997-07-01T02:42:00.000Z"),
loves: [ 'grape', 'lemon' ],
weight: 690,
gender: 'm',
vampires: 39
},
{
_id: ObjectId("64d1d782d275f01e8aedb44d"),
name: 'Raleigh',
dob: ISODate("2005-05-02T16:57:00.000Z"),
loves: [ 'apple', 'sugar' ],
weight: 421,
gender: 'm',
vampires: 2
},
{
_id: ObjectId("64d1d782d275f01e8aedb44e"),
name: 'Leia',
dob: ISODate("2001-10-08T06:53:00.000Z"),
loves: [ 'apple', 'watermelon' ],
weight: 601,
gender: 'f',
vampires: 33
},
{
_id: ObjectId("64d1d782d275f01e8aedb44f"),
name: 'Pilot',
dob: ISODate("1997-02-28T21:03:00.000Z"),
loves: [ 'apple', 'watermelon' ],
weight: 650,
gender: 'm',
vampires: 54
},
{
_id: ObjectId("64d1d782d275f01e8aedb450"),
name: 'Nimue',
dob: ISODate("1999-12-20T08:15:00.000Z"),
loves: [ 'grape', 'carrot' ],
weight: 540,
gender: 'f'
},
{
_id: ObjectId("64d1d782d275f01e8aedb451"),
name: 'Dunx',
dob: ISODate("1976-07-18T10:18:00.000Z"),
loves: [ 'grape', 'watermelon' ],
weight: 704,
gender: 'm',
vampires: 165
}
]