Database
1. Local Mongo
Setup
- follow mongo instructions to install locally.
- run
mongod
2. Docker Mongo
Setup
```shell
docker run -d \
--name store-db \
-v /storage-location-on-host:/data/db \
mongo:latest
```
Implementation
Docker You are required to link to the database image and set it to the DB_HOST.
--link store-db:db \
-e DB_HOST=db \Full Example
docker run -d \
--name cezerin2 \
--link store-db:db \
-p 3001:80 \
-e DB_HOST=db \
-v /content-on-host:/var/www/cezerin2/public/content \
cezerin2/cezerin2:latestpm2 You will be required to expose the port in the docker command.
-p 27017:27017
Then you can Deloy normally with pm2. here
pm2 start app.js
3. Mongo Atlas
Setup
I'll use Mongo Altas to deploy database.
Click Build Cluster
- Choose an provider:
AWS
- Choose a region:
N. Virginia (us-east-1)
- Choose a tier region:
M0
(Sandbox Free) - Choose a cluster name:
Cezerin-db
- Choose an provider:
Create User
- Click Customer Security
- Click Add New User Role
- Choose Username:
cezerin
- Choose Password:
cezerin
- Choose User Privileges:
Atlas Admin
Add WhiteList IP
- Click IP Whitelist
- Click Add IP ADDRESS
- Click Add current IP Address
Click Connect
- Select:
Connect Your Application
- Select Driver:
nodejs
- Select Version:
3.0 or later
- Copy connection string:
mongodb+srv://cezerin:<password>@cezerin-db-0ga32.mongodb.net/test?retryWrites=true
- Select:
Update string url
- change
<password>
to provided password - remove
?retryWrites=true
(doesn't support ?) - change
test
to database nameshop
- should be like
mongodb+srv://cezerin:cezerin@cezerin-db-0ga32.mongodb.net/shop
- change
Implementation
Docker You are required to referernce the database url in the docker configuration
-e DB_URL=mongodb+srv://cezerin:cezerin@cezerin-db-0ga32.mongodb.net/shop
Full Example
docker run -d \
--name cezerin2 \
--link store-db:db \
-p 3001:80 \
-e DB_URL=mongodb+srv://cezerin:cezerin@cezerin-db-0ga32.mongodb.net/shop \
-v /content-on-host:/var/www/cezerin2/public/content \
cezerin2/cezerin2:latestpm2
DB_URL=mongodb+srv://cezerin:cezerin@cezerin-db-0ga32.mongodb.net/shop \
pm2 start process.json