ยง2023-07-30
So far we configured configsrv and shardsrv, we have to configure a mongos query router
. This will be responsible for communicating with the config server and managing the shard servers.
We will use orgpi5Arch.yushei.net as mongos server
- /etc/mongos-27993.conf
systemLog:
destination: file
path: /opt/xfs/mongodb/log/mongos-27993.log
logAppend: true
net:
bindIp: 0.0.0.0 # Replace with the IP where you want `mongos` to listen for client connections.
port: 27993
sharding:
configDB: configServer/n2Mnjaro.yushei.net:27997,hc4Jammy.yushei.net:27997
- /etc/systemd/system/Mongos-27993.service
[Unit]
Description=MongoDB Sharding Router (mongos) - Instance 27993
After=network.target
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongos --config /etc/mongos-27993.conf
Restart=always
[Install]
WantedBy=multi-user.target
- test
[alexlai@orpi5Arch src]$ sudo -H -u mongodb bash -c 'mongos --config /etc/mongos-27993.conf'
{"t":{"$date":"2023-07-30T09:09:24.252Z"},"s":"W", "c":"SHARDING", "id":24132, "ctx":"main","msg":"Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production."}
then from hc4Jammy.yushei.net
alexlai@hc4Jammy:~$ mongosh mongodb://orgpi5Arch.yushei.net:27993
Current Mongosh Log ID: 64c6326af11e4f9b7d086aad
Connecting to: mongodb://orgpi5Arch.yushei.net:27993/?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-07-30T17:41:22.125+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
Hello
[direct: mongos] test> use admin
switched to db admin
[direct: mongos] admin> show tables
system.keys
system.users
system.version
[direct: mongos] admin>
db.createUser( {
user: "siteRootAdmin",
pwd: "bnnnnnn",
roles: [ { role: "root", db: "admin" } ]
});
[direct: mongos] admin> db.createUser( {
... user: "siteRootAdmin",
... pwd: "bnnnnnn",
... roles: [ { role: "root", db: "admin" } ]
... });
MongoServerError: User "siteRootAdmin@admin" already exists
[direct: mongos] admin> db.createUser(
{
user: "alexlai",
pwd: "alex1765",
roles: [ { role: "readWrite", db: "unicorns" }]
}
)
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1690711168, i: 1 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1690711168, i: 1 })
}
[direct: mongos] Learn> db.unicorns.insertOne({name: 'Aurora',gender: 'f', weight: 450})
MongoServerError: Database Learn could not be created :: caused by :: No shards found
[direct: mongos] Learn>
$ sudo -H -u mongodb bash -c 'mongos --config /etc/mongos-27993.conf' seems working.
But using /etc/systemd/system/Mongos--27993.service as,
[Unit]
Description=MongoDB Sharded Router
After=network.target
[Service]
User=mongodb # Replace with the user under which you want to run `mongos`
Group=mongodb # Replace with the group for the `mongos` user
ExecStart=/usr/local/bin/mongos --config /etc/mongos-27993.conf # Update the path to the mongos.conf file
PIDFile=/var/run/mongos/mongos.pid
Restart=always
# If your MongoDB binaries are located in a different directory, update the line below accordingly.
# Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
[Install]
WantedBy=multi-user.target
with systemctl start Mongos--27993.service
will fail? why?
mongos --configdb configServer/n2Mnjaro.yushei.net:27997,hc4Jammy.yushei.net:27997 --bind_ip 127.0.0.1 --port 27993
??OK??
[alexlai@orpi5Arch ~]$ sudo -H -u mongodb /usr/local/bin/mongos --config /etc/mongos-27993.conf
{"t":{"$date":"2023-07-30T13:03:07.783Z"},"s":"W", "c":"SHARDING", "id":24132, "ctx":"main","msg":"Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production."}
about to fork child process, waiting until server is ready for connections.
forked process: 2914
child process started successfully, parent exiting
[alexlai@orpi5Arch ~]$ ps aux|grep 2914
mongodb 2914 1.8 0.2 2526748 44748 ? Sl 21:03 0:00 /usr/local/bin/mongos --config /etc/mongos-27993.conf
alexlai 2957 0.0 0.0 7728 2256 pts/0 S+ 21:03 0:00 grep --color=auto 2914
[alexlai@orpi5Arch ~]$
alexlai@hc4Jammy:~$ mongosh mongodb://orgpi5Arch.yushei.net:27993
Current Mongosh Log ID: 64c65fd172eec69b1c65ce52
Connecting to: mongodb://orgpi5Arch.yushei.net:27993/?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-07-30T21:03:07.811+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
Hello
[direct: mongos] test>