Chapter 21 Database Connection

The goal in this section is to open the connection between your Stock Analyzer application and the “user data” storage. We will focus on two configurations:

  1. Local Storage (Architecture Not Shown) - Using an RDS file to store the user data for your Stock Analyzer application
  2. MongoDB Atlas Cloud Storage (Architecture Shown) - Using the MongoDB Atlas cloud database to store the user data for your Stock Analyzer application

21.1 Prerequisites

You must have:

  1. EC2 Server with the 3 shiny applications: Stock Analyzer (Local Data), Stock Analyzer (MongoDB Atlas), and Test App. This requirement will be satisfied if you’ve completed the GitHub Setup and Cloning Instructions in the previous chapters.
  2. MongoDB Atlas database that the Stock Analyzer (MongoDB Atlas) connects to: This requirements will be satisfied if you’ve completed Part 3 of the Shiny Developer with AWS course.

Use the following command to run the 3 apps in detached mode:

sudo docker container run -d -p 80:3838 \
-v /home/ubuntu/business_science_apps/:/srv/shiny-server/ \
-v /home/ubuntu/log/shiny-server/:/var/log/shiny-server/ \

21.2 Local Storage

Local storage is local to your application. An example is RDS file that is setup for the Stock Analyzer (Local Data). In this section, we will open the permissions of the user_base_tbl.rds file to permit the Stock Analyzer App to read/write to the file.

  1. We need to locate the user_base_tbl.rds file that contains our user data inside the application to adjust permissions.

  1. Adjust the user data file to read/write access by using chmod 777 [FILE]. Verify the file permissions have been changed with ls -lh.

  2. Navigate to the URL corresponding to the application, and it will work.

21.3 MongoDB Atlas Cloud & Elastic IP

Next, we’ll connect the Stock Analyzer (MongoDB Atlas) application with the MongoDB Atlas Cloud Database. The architecture is shown below. The database is outside of the application and AWS so we will need to set permissions inside of MongoDB Atlas to allow the EC2 Instance to connect to the MongoDB Atlas database. This process is similar to how we “Whitelisted” our Local IP address when developing the Stock Analyzer on our local computer.

MongoDB Atlas Website:

21.3.1 Verify Configuration File and App are Setup

Use cat to check that the app.R file is set to “production” and the config.yml file has the correct settings.

The config.yml parameters must correspond to the correct MongoDB Database connection string, database and collection.

    host:       ""
    username:   "YOUR_USERNAME"
    password:   "YOUR_PASSWORD"
    database:   "stock_analyzer"
    collection: "user_base"

Verify the host.

Verify the database and collection.

21.3.3 Whitelist EC2 Server inside of MongoDB Atlas

  1. Navigate to the MongoDB Atlas ( Inside the cluster that has been created, we can add the EC2 Server’s IP Address.

  2. Elastic IP Address - It’s a good idea to use the Elastic IP address. If your server ever has an irrecoverable issue, you can quickly set up a new EC2 Instance and apply the Elastic IP Address.

21.3.4 Verify MongoDB Atlas Connection Works

Navigate to your EC2 Server address for the stock analyzer mongo atlas directory.

Verify that any updates are propagated to the MongoDB Atlas Cloud database.

21.4 Wrapup

Congratulations! You’ve successfully connected Stock Analyzer applications to (1) a local RDS file for storage, and (2) the MongoDB Atlas Cloud Database for storage.

Become a Expert Shiny Developer with AWS

Business Science

Have a question? Leave a comment.