Skip to content

Latest commit

 

History

History
85 lines (64 loc) · 3.22 KB

Mongo_DB_Atlas.md

File metadata and controls

85 lines (64 loc) · 3.22 KB

Setting Up MongoDB Atlas and Getting a Connection URL

This guide will help you set up MongoDB Atlas, create a database, and configure it to connect to your application using environment variables.


Step 1: Create a MongoDB Atlas Account

  1. Go to MongoDB Atlas.
  2. Sign up if you don’t have an account, or Log in if you already have one.

Step 2: Create a New Project

  1. After logging in, you’ll be directed to the MongoDB Atlas dashboard.
  2. Click on New Project on the left or Create a New Project if it’s your first time.
  3. Name your project (e.g., MyProject) and click Next.

Step 3: Build a New Cluster

  1. In your new project, select Build a Cluster.
  2. Choose the Shared option (this is free and suitable for small projects).
  3. Select a cloud provider and region. You can choose any region close to you.
  4. Click Create Cluster.

Note: MongoDB will take a few minutes to set up your cluster.


Step 4: Create a Database User

  1. While the cluster is being set up, click on Database Access in the left menu.
  2. Click on Add New Database User.
  3. Create a username and password for your database.
    • Save these credentials securely, as you’ll need them later to connect.
  4. Set the User Privileges to “Read and Write to Any Database” (default).
  5. Click Add User.

Step 5: Whitelist Your IP Address

  1. Go to Network Access in the left menu.
  2. Click Add IP Address.
  3. Select Allow Access from Anywhere for development.
    • This setting is convenient for development. For production, it’s recommended to restrict access to specific IPs.
  4. Click Confirm.

Step 6: Get the MongoDB Connection URL

  1. Once your cluster is ready, go back to the Clusters section in your project.
  2. Click Connect next to your cluster name.
  3. Choose Drivers to connect MongoDB to your application (e.g., Node.js, Python).

Step 7: Select Driver and Version

  1. After selecting Drivers, choose the driver for your project (e.g., Node.js) and the version you’re using.
  2. MongoDB will display a connection string that looks like this:
    mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority
    
  3. Replace <username> and <password> with the database user credentials you created earlier.

Step 8: Add the Connection String to Your .env File

  1. In your project’s root directory, create a .env file (if you don’t already have one).
  2. Add the following line to the .env file, replacing DB_URL with your connection string:
    DB_URL=mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority
    
  3. Make sure to replace <username> and <password> with your MongoDB database username and password.

Example of .env File

Your .env file might look like this:

DB_URL=mongodb+srv://yourUsername:[email protected]/?retryWrites=true&w=majority
SESSION_SECRET=your_session_secret
CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secret