§2024-05-31

¶ Here's how you can set up syslog-ng to write logs to a MongoDB capped collection:

  1. Create a Capped Collection in MongoDB

First, ensure you have a MongoDB instance running. Then, create a capped collection in your MongoDB database:

lexlai@n2Bookworm:~/nodejs_project/syslog_ng_capped_collection/web_monitor$ mongosh "mongodb+srv://siteRootAdmin:b23258585@ys20220318.yushei.com.tw/YuSheiDB?replicaSet=ys20220318&authSource=admin&tls=false"
Current Mongosh Log ID:	665bf6fbfa8de844298db5fa
Connecting to:		mongodb+srv://<credentials>@ys20220318.yushei.com.tw/YuSheiDB?replicaSet=ys20220318&authSource=admin&tls=false&appName=mongosh+2.2.6
Using MongoDB:		5.0.8
Using Mongosh:		2.2.6

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

------
   The server generated these startup warnings when booting
   2024-05-09T17:37:42.790+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
------

ys20220318 [primary] YuSheiDB> show databases
YuSheiDB           3.17 GiB
YuSheiFAB        752.00 KiB
YuSheiReactTest    4.34 MiB
admin            236.00 KiB
config           332.00 KiB
local             29.81 GiB
yusheiDBTest     122.14 MiB
yusheiMotorDB     36.97 MiB
yusheiRedisTest   59.95 MiB


odroid01 [primary] YuSheiDBTest> use syslog_ng_db
switched to db syslog_ng_db

ys20220318 [primary] syslog_ng_db> db.createCollection("ys20220318Replication", { capped: true, size: 5242880, max: 5000 })
{ ok: 1 }
ys20220318 [primary] syslog_ng_db> { ok: 1 }
1
ys20220318 [primary] syslog_ng_db> show collections
ys20220318Replication



ys20220318 [primary] syslog_ng_db> db.createUser({
...   user: "alexlai",
...   pwd: "alex1765",
...   roles: [ { role: "readWrite", db: "syslog_ng_db" }]
... })
{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1717303175, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('QDiqmP2lm3JJzHtQ+kAjUX9qjzA=', 0),
      keyId: Long('7331330336128761862')
    }
  },
  operationTime: Timestamp({ t: 1717303175, i: 1 })
}


db.createUser({ user: "alexlai", pwd: "alex1765", roles: [ { role: "readWrite", db: "syslog_ng_db" }] })


ys20220318 [primary] syslog_ng_db> db.getUsers()
{
  users: [
    {
      _id: 'syslog_ng_db.alexlai',
      userId: UUID('434809c6-046b-409b-b3a1-e5ffb91305a0'),
      user: 'alexlai',
      db: 'syslog_ng_db',
      roles: [ { role: 'readWrite', db: 'syslog_ng_db' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1717305681, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('Jp3UwhIP/2XDyT3I2hOK4iOL24o=', 0),
      keyId: Long('7331330336128761862')
    }
  },
  operationTime: Timestamp({ t: 1717305681, i: 1 })
}


1.01. test
 

aexlai@n2Bookworm:~/nodejs_project/syslog_ng_capped_collection/web_monitor$ mongosh "mongodb+srv://alexlai:alex1765@ys20220318.yushei.com.tw/syslog_ng_db?replicaSet=ys20220318&authSource=syslog_ng_db&tls=false" Current Mongosh Log ID: 665bfa1f7b654acd3f8db5fa Connecting to: mongodb+srv://@ys20220318.yushei.com.tw/syslog_ng_db?replicaSet=ys20220318&authSource=syslog_ng_db&tls=false&appName=mongosh+2.2.6 Using MongoDB: 5.0.8 Using Mongosh: 2.2.6

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

ys20220318 [primary] syslog_ng_db> show collections ys20220823Replication