Hướng dẫn cấu hình mongodb

Ở bài số 1, Tui đã hướng dẫn các bạn cách tải và cài đặt MongoDB Enterprise Server và Compass rồi, tuy nhiên nó chưa chạy được. Ta cần cấu hình Service cho MongoDB để nó tự động chạy khi máy tính mở, cũng như luôn start 24/7. Các bạn thực hiện y xì theo 4 bước như Tui hướng dẫn trong này là OK á.

Bước 1:
Tạo 2 thư mục trong ổ C (ổ nào là tùy ý ta):

C:/data/db

C:/data/log

Hình minh họa:

Hướng dẫn cấu hình mongodb
Bước 2: Tạo file cấu hình tên là mongod.cfg, lưu vào nơi cài đặt MongoDB Server (C:\Program Files\MongoDB\Server\3.6), xem hình chụp dưới đây (ta cứ tạo 1 file txt bình thường, nhập liệu xong đổi đuôi thành cfg):

Hướng dẫn cấu hình mongodb

Nội dung file cấu hình như dưới đây. Chú ý port mặc định là 27017,

bindIp lấy localhost, 127.0.0.1 đồng thời lấy Ip của máy server hiện tại đang cài đặt MongoDB (10.11.35.45 là địa chỉ IP của máy Server mà bạn cài Mongo, cấu hình như thế này thì mới Remote connection được, vì mặc định Mongo cấm remote connection. Lưu ý nếu bạn chỉ cấu hình trên laptop, máy tính để bàn thì chỉ được phép chứa localhost, 127.0.0.1). Chú ý nếu nhập không đúng Ip, start Service sẽ bị báo lỗi:

# network interfaces
net:
  port: 27017
  bindIp: localhost,127.0.0.1,10.11.35.45
systemLog:
    destination: file
    path: c:\data\log\mongod.log
storage:
    dbPath: c:\data\db

path và dbpath ta đã tạo ở bước 1.

Nếu không biết gõ lệnh thì có thể tải mẫu script ở đây rồi sửa lại IP Mongod.cfg

Bước 3: Cài đặt MongoDB Service

Mở Command line dưới quyền Admistrator, chạy lệnh sau:

Hướng dẫn cấu hình mongodb
Chi tiết lệnh (các bạn copy vào):

“C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe” –config “C:\Program Files\MongoDB\Server\3.6\mongod.cfg” –install

Bước 4: Chạy Service MongoDB

gõ lệnh: net start MongoDB

Các bận thấy nó báo như bên dưới là thành công

Hướng dẫn cấu hình mongodb
Sau khi chạy Service thành công, bạn có thể kiểm tra lại bằng cách vào Service của Windows:

Hướng dẫn cấu hình mongodb

Như vậy ta đã cấu hình chạy thành công Service cho MongoDB.

Bây giờ có thể kết nối vào MongoDB bằng công cụ Compass:

Hướng dẫn cấu hình mongodb
Nhập đúng Hostname (localhost hoặc 127.0.0.1 hoặc IP của Public server của bạn) + port (27017) rồi bấm CONNECT, kết quả (vì máy Tui có sẵn một số CSDL nên bạn thấy như bên dưới, còn của bạn lần đầu thì nó trống trơn chỗ đó):

Hướng dẫn cấu hình mongodb

Như vậy ta cấu hình và chạy Service MongoDB thành công

Bài học sau Tui sẽ hướng dẫn cách sử dụng Compass cơ bản, để các bạn có thể biết cách tạo Cơ sở dữ liệu, collection, document…. cũng như một số thao tác tìm kiếm.

{ "set" : "replica0", "date" : ISODate("2022-07-12T09:08:54.094Z"), "myState" : 1, "term" : NumberLong(1), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 3, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2022-07-12T09:08:37.945Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "lastAppliedWallTime" : ISODate("2022-07-12T09:08:37.945Z"), "lastDurableWallTime" : ISODate("2022-07-12T09:08:37.945Z") }, "lastStableRecoveryTimestamp" : Timestamp(1657616913, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2022-07-12T09:07:34.375Z"), "electionTerm" : NumberLong(1), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(1657616854, 1), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1657616854, 1), "t" : NumberLong(-1) }, "numVotesNeeded" : 1, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "newTermStartDate" : ISODate("2022-07-12T09:07:34.401Z"), "wMajorityWriteAvailabilityDate" : ISODate("2022-07-12T09:07:34.413Z") }, "members" : [ { "_id" : 0, "name" : "mongodb0.replset.member:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 108, "optime" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2022-07-12T09:08:37Z"), "lastAppliedWallTime" : ISODate("2022-07-12T09:08:37.945Z"), "lastDurableWallTime" : ISODate("2022-07-12T09:08:37.945Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "Could not find member to sync from", "electionTime" : Timestamp(1657616854, 2), "electionDate" : ISODate("2022-07-12T09:07:34Z"), "configVersion" : 5, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "mongodb1.replset.member:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 38, "optime" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2022-07-12T09:08:37Z"), "optimeDurableDate" : ISODate("2022-07-12T09:08:37Z"), "lastAppliedWallTime" : ISODate("2022-07-12T09:08:37.945Z"), "lastDurableWallTime" : ISODate("2022-07-12T09:08:37.945Z"), "lastHeartbeat" : ISODate("2022-07-12T09:08:53.960Z"), "lastHeartbeatRecv" : ISODate("2022-07-12T09:08:53.978Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "mongodb0.replset.member:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 5, "configTerm" : 1 }, { "_id" : 2, "name" : "mongodb2.replset.member:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 20, "optime" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1657616917, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2022-07-12T09:08:37Z"), "optimeDurableDate" : ISODate("2022-07-12T09:08:37Z"), "lastAppliedWallTime" : ISODate("2022-07-12T09:08:37.945Z"), "lastDurableWallTime" : ISODate("2022-07-12T09:08:37.945Z"), "lastHeartbeat" : ISODate("2022-07-12T09:08:53.958Z"), "lastHeartbeatRecv" : ISODate("2022-07-12T09:08:52.972Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "mongodb1.replset.member:27017", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 5, "configTerm" : 1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1657616917, 1), "signature" : { "hash" : BinData(0,"5aFVC6b+kGvrgwtEf/rGMPRyepM="), "keyId" : NumberLong("7119410177228406790") } }, "operationTime" : Timestamp(1657616917, 1) }