MongoDB Academy · บทเรียน

การเข้ารหัสขณะจัดเก็บและ TLS ขณะรับส่งข้อมูล

ผู้เรียนจะเปิดใช้ TLS สำหรับการเชื่อมต่อของไคลเอนต์ และศึกษาเกี่ยวกับการเข้ารหัสขณะจัดเก็บของ WiredTiger รวมถึงพื้นที่จัดเก็บข้อมูลที่เข้ารหัสในตัวของ Atlas

บทเรียน 3 จาก 413 ขั้นตอน

การเข้ารหัสขณะจัดเก็บและ TLS ขณะรับส่งข้อมูล เป็นบทเรียน MongoDB Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MongoDB Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Two Layers of Database Encryption

Securing MongoDB data requires protection at two distinct points: encryption in transit (data moving between clients and MongoDB, or between replica set members) and encryption at rest (data stored on disk). Authentication controls who can connect, but even an authenticated user operating over an unencrypted connection exposes credentials and queries to network sniffing. Together, TLS and encryption at rest provide defence-in-depth.

TLS: Encrypting Data in Transit

TLS (Transport Layer Security) encrypts the network channel between MongoDB clients, drivers, and mongod/mongos instances. Enabling TLS prevents eavesdropping and man-in-the-middle attacks on the wire. MongoDB 4.2+ supports only TLS 1.1 and higher, with TLS 1.3 available on modern platforms. Always use TLS in any environment where network traffic could be intercepted — which includes cloud VPCs despite VPC-level isolation.

Configuring TLS in mongod.conf

TLS is configured in the net.tls section of mongod.conf. Set mode: requireTLS to reject all unencrypted connections. You must provide a PEM file containing the server's certificate and private key. Optionally, provide a CAFile to require clients to present certificates signed by your CA (mutual TLS).

# mongod.conf — require TLS for all connections
net:
  port: 27017
  tls:
    mode: requireTLS
    certificateKeyFile: /etc/ssl/mongodb-server.pem
    CAFile: /etc/ssl/ca.pem          # optional: require client certs
    disabledProtocols: TLS1_0,TLS1_1 # enforce TLS 1.2+

Connecting With TLS in the Node.js Driver

The official MongoDB Node.js driver enables TLS when the connection URI uses mongodb+srv:// (Atlas always uses TLS) or when the tls: true option is set. In self-hosted deployments, you may need to provide the CA file path so the driver can verify the server certificate. Use environment variables for certificate paths so they are not hardcoded in source code.

const { MongoClient } = require('mongodb')

// Self-hosted MongoDB with TLS
const client = new MongoClient('mongodb://myUser:myPass@dbhost:27017', {
  tls: true,
  tlsCAFile: process.env.MONGO_TLS_CA_FILE,
  // For mutual TLS (client certificate):
  // tlsCertificateKeyFile: process.env.MONGO_TLS_CERT_FILE
})

await client.connect()

// Atlas — TLS is enabled by default via SRV record
const atlasClient = new MongoClient(process.env.ATLAS_CONNECTION_STRING)

TLS Modes: disabled, allowTLS, preferTLS, requireTLS

MongoDB offers four TLS modes during migration phases. disabled accepts only plain connections (development only). allowTLS accepts both TLS and plain (for backward compatibility). preferTLS defaults to TLS but accepts unencrypted connections. requireTLS rejects all non-TLS connections and is the only mode acceptable for production deployments.

# Migration path: gradually enforce TLS
# Step 1: allowTLS (accept both)
# Step 2: preferTLS (default TLS, allow plain)
# Step 3: requireTLS (reject plain) <- production target
net:
  tls:
    mode: requireTLS

Encryption at Rest: WiredTiger

MongoDB's default storage engine, WiredTiger, supports Encrypted Storage Engine (available in MongoDB Enterprise). It encrypts data files on disk using AES-256-CBC or AES-256-GCM. Even if an attacker gains access to the physical disk or a disk snapshot, they cannot read the data without the encryption key. This addresses compliance requirements such as GDPR, HIPAA, and PCI-DSS that mandate protection of data at rest.

# mongod.conf — WiredTiger encryption at rest (Enterprise only)
security:
  enableEncryption: true
  encryptionKeyFile: /etc/mongodb/encryption-key  # AES-256 key

# Or use KMIP key management:
security:
  enableEncryption: true
  kmip:
    serverName: kmip.example.com
    port: 5696
    clientCertificateFile: /etc/ssl/kmip-client.pem

Atlas Encryption at Rest

MongoDB Atlas provides encryption at rest using cloud provider key management: AWS KMS, Azure Key Vault, or Google Cloud KMS. Customers can bring their own Customer Master Key (CMK), giving them full control — if you delete your CMK, Atlas cannot decrypt your data. Atlas's encryption at rest is enabled per project in the Security settings and applies to all clusters in that project.

Key Management Best Practices

Encryption is only as strong as the security of its keys. Follow these key management best practices: store encryption keys in a dedicated Key Management Service (KMS) rather than on the same server as the database; rotate encryption keys regularly (MongoDB supports key rotation without re-encrypting all data); use separate keys for development, staging, and production; and enable KMS audit logs to track all key access events.

TLS Certificate Rotation Without Downtime

TLS certificates expire, typically after 1–2 years. MongoDB supports online certificate rotation: you can deploy a new PEM file and send the rotateCertificates command to mongod, which picks up the new certificate for all new connections without restarting the process. Plan certificate renewal at least 30 days before expiry to avoid last-minute scrambles and production outages.

// Trigger online certificate rotation (mongosh)
db.adminCommand({ rotateCertificates: 1 })
// MongoDB reloads the certificate file specified in mongod.conf
// without dropping existing connections

Network-Level Security: IP Allowlisting and VPC

TLS and encryption at rest protect data on the wire and on disk, but network-level controls add another layer. In MongoDB Atlas, use IP Access List entries to whitelist only your application servers and developer IPs. For production, use VPC Peering or AWS PrivateLink so traffic between your application and Atlas never traverses the public internet, eliminating a large class of network-based attacks even before TLS is considered.

Auditing Encrypted Connections

Once TLS is enabled, verify that all connections are encrypted by checking the connPoolStats or the MongoDB connection log. MongoDB logs TLS handshake failures so you can identify any legacy client still attempting plaintext connections. In Atlas, the Monitoring tab shows active connections and their TLS status, making it easy to confirm full enforcement before removing the allowTLS mode.

// Check current connections (mongosh)
db.adminCommand({ currentOp: true }).inprog
  .filter(op => op.client)
  .map(op => ({ client: op.client, tls: op.clientMetadata }))

// Server status — TLS stats
db.serverStatus().network.serviceExecutorTaskStats

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: requireTLS in mongod.conf enforces encrypted client connections and should be the only mode used in production, WiredTiger Encrypted Storage Engine protects data files on disk (Enterprise) while Atlas provides CMK-based cloud encryption, and network-level controls like IP allowlists and VPC peering add layers of protection beyond TLS. Next up we cover Client-Side Field Level Encryption.

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “การเข้ารหัสขณะจัดเก็บและ TLS ขณะรับส่งข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเข้ารหัสขณะจัดเก็บและ TLS ขณะรับส่งข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MongoDB Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเข้ารหัสขณะจัดเก็บและ TLS ขณะรับส่งข้อมูล”

ผู้เรียนจะเปิดใช้ TLS สำหรับการเชื่อมต่อของไคลเอนต์ และศึกษาเกี่ยวกับการเข้ารหัสขณะจัดเก็บของ WiredTiger รวมถึงพื้นที่จัดเก็บข้อมูลที่เข้ารหัสในตัวของ Atlas คุณปฏิบัติ MongoDB Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MongoDB Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MongoDB Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การเข้ารหัสขณะจัดเก็บและ TLS ขณะรับส่งข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน MongoDB Academy นี้ได้ไหม

ได้ บทเรียน MongoDB Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. กลไกการยืนยันตัวตน: SCRAM และ x.509
  2. การควบคุมการเข้าถึงตามบทบาท: บทบาทในตัวและบทบาทกำหนดเอง
  3. การเข้ารหัสขณะจัดเก็บและ TLS ขณะรับส่งข้อมูล
  4. การเข้ารหัสระดับฟิลด์ฝั่งไคลเอนต์
← กลับไปที่ MongoDB Academy