What is Strapi?
Strapi is a Node.js Content Management Framework which makes API creation easier. Halfway between a Node.js Framework and a Headless CMS, it has been designed to build APIs in seconds instead of weeks thanks to its built-in features: Admin Panel, Authentication & Permissions management, Content Management, API Generator, etc.
APIs built with Strapi need a database to store data. The default database management system is MongoDB. Obviously, you can use a MongoDB instance running on your own computer but, if you need to collaborate with other developers, or simply do not want to install MongoDB on your computer, you will probably want to setup a database on a server and use it remotely.
What is mLab?
Database management is hard. Even more harder than apps', you have to deal with highly important challenges, such as security, scalability and performances. To make developer's life easier, some companies decided to offer a new concept: Database as a Service (DBaaS). They all provide fully hosted databases access, so you can focus on your code without spending time on your database's infrastructure.
Tens of online DBaaS exist: Compose.io, MongoDB Atlas, etc. One of the first in the MongoDB as a Service market is mLab which is also one of the easiest to use. Its free plan lets you try it without having to enter your credit card informations. For all these reasons, we decided to explain you how to use mLab with Strapi.
First of all, we are going to setup the mLab database. Then, we will generate a brand new Strapi API connected to this database.
Let's start with the mLab database!
mLab is an online service, so, except if you already have an account, you need to create one.
Visit the register page, fill in the form and submit it.
Validate your email address
To complete the registration process, validate your email address by clicking on the link in the email you received from mLab.
Well done, your email has been validated. Let's make the magic happen!
To create a new database, click on the "Create new" button at the right of "MongoDB deployments".
The database can be hosted on Amazon Web Services, Google Cloud Platform or even Microsoft Azure. In this example, we are going to use AWS with a "Sandbox" database:
The closer your API is hosted to your database, the faster it will be. If you plan to use the database for production, it is best to host your database and API servers in the same cloud provider and region. If that's not possible, select the region closest to your API servers:
Your database needs a name. Give it one, ideally according to the name of your project:
Note: we recommend you to suffix the database's name with your current environment (eg. myproject-development
or myproject-production
).
The Order Confirmation page gives you a summary of your choices. If everything is correct, click on "Submit Order":
Easy-peasy: your database is created!
You are almost done. The only thing you need is a user in order to be able to connect to the database.
Click on the "Users" tab, then on "Add database a user":
A popup should appear. Choose a username and a password for your database user (do not forget these credentials, you will need them later):
Here we are, the user has been created:
On the screen above you can see the line which should be used "To connect using the mongo shell": mongo ds125048.mlab.com:25048/myapp-development -u <dbuser> -p <dbpassword>
.
In this line, three informations are going to be useful for the next section of the tutorial:
ds125048.mlab.com
25048
myapp-development
Congrats, you successfully set up a database and added a user!
You are now able to use your brand new database from a Strapi project.
Requirements: please make sure Node 8 (or higher) is installed on your machine.
Install Strapi using npm:
1$ npm i strapi@alpha -g
To create a new project, simply run the following command:
1$ strapi new my-app
This is the most important part: answer the questions with the informations provided by mLab.
⚠️ UPDATE: in latest version of Strapi you will have to set the config value:
Authentication database
the value has to be the database name.
This actually generates a new Strapi project and writes the configurations you specified in a file located at my-app/config/environment/development/database.json
:
1{
2 "defaultConnection": "default",
3 "connections": {
4 "default": {
5 "connector": "strapi-mongoose",
6 "settings": {
7 "client": "mongo",
8 "host": "ds125048.mlab.com",
9 "port": "25048",
10 "database": "myapp-development",
11 "username": "myusername",
12 "password": "mypassword"
13 },
14 "options": {
15 "authenticationDatabase": "myapp-development",
16 "ssl": false
17 }
18 }
19 }
20}
Note: a similar file containing the production settings exists (path: config/environments/production/database.json
)
Enter in your project's directory and start the server:
1$ cd my-app
2$ strapi start
From now, the Strapi admin panel should be at the following URL: http://localhost:1337/admin.
Register your first user to double check that your database is correctly connected:
At this point, you should see in mLab the first collections created by Strapi:
Later, or because you are already working on a Strapi project, you may want to update your database credentials.
To do so, please use the Settings Manager:
Note: you can also directly edit files (config/environments/development/database.json
and config/environments/production/database.json
).
Congratulations! You successfully configured a mLab database and generated a new Strapi project using it.
When you are going to production, we strongly recommend you to create another database to separate development and production data. Also, Sandbox plan (free) is great for development but is not adapted when your API is live.
We hope this tutorial has been useful for you. Please comment it to give a feedback and share your opinion about DBaaS solutions.
Pierre created Strapi with Aurélien and Jim back in 2015. He's a strong believer in open-source, remote and people-first organizations. You can also find him regularly windsurfing or mountain-biking!