§2023-08-08

ShardA [primary] admin> rs.status().members.forEach(member => print(member.name))
n2Mnjaro.yushei.net:27985
hc4Bookworm.yushei.net:27985
hc4Lunar.yushei.net:27985
ShardB [direct: secondary] admin> db.auth("siteRootAdmin", "b23258585")
{ ok: 1 }
ShardB [direct: secondary] admin> rs.status().members.forEach(member => print(member.name))
n2Mnjaro.yushei.net:27983
hc4Bookworm.yushei.net:27983
hc4Lunar.yushei.net:27983

n2Mnjaro-01.yushei.net has The server failed to respond to a ping and may be unavailable: MongoNetworkError: getaddrinfo EAI_AGAIN n2mnjaro-01.yushei.net

# mongodb-27985.conf for shard 
# replicationSet: ShardA

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /opt/xfs/mongodb/log/mongod-27985.log

# Where and how to store data.
storage:
  dbPath: /opt/xfs/mongodb/data-27985
  
# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27985
  bindIp: 0.0.0.0   # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
  
  # tls:
      # mode: requireTLS
      # certificateKeyFile: /opt/xfs/mongodb/x.509/n2mnjaro-01.yushei.net.pem
      # CAFile: /opt/xfs/mongodb/x.509/mongoCA.crt

# security:
  # authorization: enabled
  # keyFile: /opt/xfs/mongodb/x.509/MuneTakaHomeKey
  # clusterAuthMode: x509  

#operationProfiling:

replication:
  replSetName: "ShardA"

sharding:
  clusterRole: "shardsvr"

## Enterprise-Only Options

#auditLog:

#snmp:
[Unit]
Description=MongoDB Database Server, port 27985, replicationSet: ShardA
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
TimeoutStartSec=infinity
User=mongodb
Group=mongodb
Environment="OPTIONS=-f /etc/mongodb-27985.conf"
Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
ExecStart=/usr/local/bin/mongod $OPTIONS
RuntimeDirectory=mongodb
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target
 mongosh mongodb://n2Mnjaro-01.yushei.net:27985
Current Mongosh Log ID:	64d1863227b0c20050aa1af0
Connecting to:		mongodb://n2Mnjaro-01.yushei.net:27985/?directConnection=true&appName=mongosh+1.10.1
Using MongoDB:		undefined
Using Mongosh:		1.10.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> 
rs.initiate( {
   _id : "ShardA",
   members: [
      { _id: 0, host: "hc4Bookworm.yushei.net:27985" },
      { _id: 1, host: "n2Mnjaro.yushei.net:27985" },
      { _id: 2, host: "hc4Lunar.yushei.net:27985" }
   ]
});
#!/bin/bash
# this is to connect shardA:27985

mongosh "mongodb://\
hc4bookworm.yushei.net:27985,\
n2Mnjaro.yushei.net:27985,\
hc4Bookworm.yushei.net:27985"

# tls=true&tlsCertificateKeyFile=/opt/xfs/mongodb/x.509/hc4bookworm.yushei.net.pem\
# &tlsCAFile=/opt/xfs/mongodb/x.509/mongoCA.crt"

mongosh "mongodb://siteRootAdmin:b23258585@\
n2mnjaro-01.yushei.net:27995,hc4bookworm.yushei.net:27995,hc4lunar.yushei.net:27995/admin?\
tls=true&tlsCertificateKeyFile=/opt/xfs/mongodb/x.509/hc4bookworm.yushei.net.pem\
&tlsCAFile=/opt/xfs/mongodb/x.509/`mongoCA.crt"


# mongosh "mongodb://siteRootAdmin:b23258585@orgpi5arch.yushei.net:27999,n2mnjaro-01.yushei.net:27999/admin?\
# tls=true&tlsCertificateKeyFile=/opt/xfs/mongodb/x.509/n2mnjaro-01.yushei.net.pem\
# &tlsCAFile=/opt/xfs/mongodb/x.509/mongoCA.crt"

# no longer suppoert relicaSet=MuneTakaHome"
/*
 *  .mongoshrc.js
 *  db = connect("mongodb://127.0.0.1:27017/admin?replicaSet=ys20210904");
 *  db = connect("mongodb://x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/?authSource=admin")
 *  db = connect("mongodb://siteRootAdmin:b23258585@x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/?replicaSet=odroid01?authSource=admin");
 *  db.auth("siteRootAdmin", "b23258585");
 *  all the above lines failed
 */

db = connect("mongodb://hc4Bookworm.yushei.net:27985,n2mnjaro.yushei.net:27985,hc4lunar.yushei.net:27985/");

// let log =  rs.status().members;
// console.table(log);


function alert() {
	let log =  rs.status().members;
	let simpleLog = [];
	// console.log(log);
	for (let i = 0; i < log.length; i++){
	    // console.log("_id: ", log[i]._id, "name: ", log[i].name, "health: ", log[i].health, "state: ", log[i].state, "stateStr: ", log[i].stateStr);
    		simpleLog[i] = {
        		"_id": log[i]._id,
        		"name": log[i].name,
        		"health": log[i].health,
        		"state": log[i].state,
        		"stateStr": log[i].stateStr
    		}
	}
	console.log('\n');
	console.table(simpleLog);
	console.log(Date().toLocaleString());
}

alert();

let timerId =  setInterval(() => alert(), 60000); // 単位 is milli-second, 1 sec = 1000

/*

// The following codes are to tail on inLaneCapped

// Replace 'your_capped_collection_name' with the name of your capped collection
const collection = db.getCollection('inLaneCapped');

// Watch the capped collection for changes
const changeStream = collection.watch();

// Function to process each change and print specific fields
const processChange = (change) => {
  if (change.operationType === 'insert') {
   // _id: , plateText: 'NEF6953', cameraSource: '002',  inTime: '2023-07-25T17:17:04',
    const { _id, plateText, cameraSource, inTime } = change.fullDocument; // Add the specific fields you want to print
    print(`Change ID: ${_id}`);
    print(`plateText: ${plateText}`);
    print(`camerSource: ${cameraSource}`);
    print(`inTime: ${inTime}`);
  }
};

// Start the change stream and monitor for changes
const monitorChanges = async () => {
  while (true) {
    const change = await changeStream.tryNext();
    if (change) {
      processChange(change);
    } else {
      // Sleep for a short interval before checking again
      // alert();
      sleep(100);
    }
  }
};

let timerId =  setInterval(() => alert(), 60000); // 単位 is milli-second, 1 sec = 1000
*/

§ ShardB

# mongodb-27983.conf for shard 
# replicationSet: ShardB

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /opt/xfs/mongodb/log/mongod-27983.log

# Where and how to store data.
storage:
  dbPath: /opt/xfs/mongodb/data-27983
  
# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27983
  bindIp: 0.0.0.0   # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
  
  # tls:
      # mode: requireTLS
      # certificateKeyFile: /opt/xfs/mongodb/x.509/n2mnjaro-01.yushei.net.pem
      # CAFile: /opt/xfs/mongodb/x.509/mongoCA.crt

# security:
  # authorization: enabled
  # keyFile: /opt/xfs/mongodb/x.509/MuneTakaHomeKey
  # clusterAuthMode: x509  

#operationProfiling:

replication:
  replSetName: "ShardB"

sharding:
  clusterRole: "shardsvr"

## Enterprise-Only Options

#auditLog:

#snmp:
[Unit]
Description=MongoDB Database Server, port 27983, replicationSet: ShardB
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
TimeoutStartSec=infinity
User=mongodb
Group=mongodb
Environment="OPTIONS=-f /etc/mongodb-27983.conf"
Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
ExecStart=/usr/local/bin/mongod $OPTIONS
RuntimeDirectory=mongodb
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target
rs.initiate( {
   _id : "ShardB",
   members: [
      { _id: 0, host: "hc4Bookworm.yushei.net:27983" },
      { _id: 1, host: "n2Mnjaro.yushei.net:27983" },
      { _id: 2, host: "hc4Lunar.yushei.net:27983" }
   ]
});
/*
 *  .mongoshrc.js
 *  db = connect("mongodb://127.0.0.1:27017/admin?replicaSet=ys20210904");
 *  db = connect("mongodb://x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/?authSource=admin")
 *  db = connect("mongodb://siteRootAdmin:b23258585@x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/?replicaSet=odroid01?authSource=admin");
 *  db.auth("siteRootAdmin", "b23258585");
 *  all the above lines failed
 */

db = connect("mongodb://hc4Bookworm.yushei.net:27983,n2mnjaro.yushei.net:27983,hc4lunar.yushei.net:27983/");

// let log =  rs.status().members;
// console.table(log);


function alert() {
	let log =  rs.status().members;
	let simpleLog = [];
	// console.log(log);
	for (let i = 0; i < log.length; i++){
	    // console.log("_id: ", log[i]._id, "name: ", log[i].name, "health: ", log[i].health, "state: ", log[i].state, "stateStr: ", log[i].stateStr);
    		simpleLog[i] = {
        		"_id": log[i]._id,
        		"name": log[i].name,
        		"health": log[i].health,
        		"state": log[i].state,
        		"stateStr": log[i].stateStr
    		}
	}
	console.log('\n');
	console.table(simpleLog);
	console.log(Date().toLocaleString());
}

alert();

let timerId =  setInterval(() => alert(), 60000); // 単位 is milli-second, 1 sec = 1000

/*

// The following codes are to tail on inLaneCapped

// Replace 'your_capped_collection_name' with the name of your capped collection
const collection = db.getCollection('inLaneCapped');

// Watch the capped collection for changes
const changeStream = collection.watch();

// Function to process each change and print specific fields
const processChange = (change) => {
  if (change.operationType === 'insert') {
   // _id: , plateText: 'NEF6953', cameraSource: '002',  inTime: '2023-07-25T17:17:04',
    const { _id, plateText, cameraSource, inTime } = change.fullDocument; // Add the specific fields you want to print
    print(`Change ID: ${_id}`);
    print(`plateText: ${plateText}`);
    print(`camerSource: ${cameraSource}`);
    print(`inTime: ${inTime}`);
  }
};

// Start the change stream and monitor for changes
const monitorChanges = async () => {
  while (true) {
    const change = await changeStream.tryNext();
    if (change) {
      processChange(change);
    } else {
      // Sleep for a short interval before checking again
      // alert();
      sleep(100);
    }
  }
};

let timerId =  setInterval(() => alert(), 60000); // 単位 is milli-second, 1 sec = 1000
*/