2019-05-06 16:17:16 +02:00
# Databases
Strapi gives you the option to choose the most appropriate database for your project. It currently supports **PostgreSQL** , **MongoDB** , **SQLite** , **MySQL** and
**MariaDB**. The following documentation covers how to install these databases locally (for development purposes) and on various hosted or cloud server solutions (for staging or production purposes).
2019-11-25 19:16:42 +01:00
::: tip
2019-10-23 10:11:53 +02:00
Deploying **Strapi** itself is covered in the [Deployment Guide ](deployment.md ).
2019-09-23 18:41:48 +02:00
:::
2019-05-06 16:17:16 +02:00
## SQLite Installation
2019-10-23 10:11:53 +02:00
SQLite is the default ([Quick Start ](../getting-started/quick-start.md )) and recommended database to quickly create an app locally.
2019-05-06 16:17:16 +02:00
### Install SQLite locally
2019-09-23 18:41:48 +02:00
Simply use one of the following commands.
2019-11-07 12:05:39 +01:00
:::: tabs
2019-09-23 18:41:48 +02:00
2019-11-07 12:05:39 +01:00
::: tab yarn
2019-05-06 16:17:16 +02:00
2019-09-24 12:22:48 +02:00
```bash
2019-11-26 01:50:07 +08:00
yarn create strapi-app my-project --quickstart
2019-05-06 16:17:16 +02:00
```
2019-09-23 18:41:48 +02:00
:::
2019-05-06 16:17:16 +02:00
2019-11-07 12:05:39 +01:00
::: tab npx
2019-05-06 16:17:16 +02:00
2019-09-24 12:22:48 +02:00
```bash
2019-09-23 18:41:48 +02:00
npx create-strapi-app my-project --quickstart
```
:::
2019-05-06 16:17:16 +02:00
2019-09-23 18:41:48 +02:00
::::
2019-05-06 16:17:16 +02:00
2019-09-23 18:41:48 +02:00
This will create a new project and launch it in the browser.
2019-05-06 16:17:16 +02:00
2019-11-07 12:05:39 +01:00
::: tip
2019-10-23 10:11:53 +02:00
The [Quick Start Guide ](../getting-started/quick-start.md ) is a complete step-by-step tutorial
2019-09-23 18:41:48 +02:00
:::
## MongoDB Installation
### Install MongoDB locally
#### 1. Install MongoDB on your development environment
2019-05-06 16:17:16 +02:00
If you already have MongoDB installed locally and running as a background service, you may skip to [Install Strapi locally with MongoDB ](#install-strapi-locally-with-mongodb ). (If you have additional questions, please see the official [MongoDB documentation ](https://docs.mongodb.com/manual/installation/#tutorial-installation ). )
Please complete the installation steps appropriate to your operating system.
2019-11-07 12:05:39 +01:00
:::: tabs
2019-05-06 16:17:16 +02:00
2019-11-07 12:05:39 +01:00
::: tab "Windows 10"
2019-05-06 16:17:16 +02:00
2019-09-23 18:41:48 +02:00
#### Install MongoDB on Windows 10
2019-05-06 16:17:16 +02:00
Follow these steps to [install MongoDB onto your Windows 10 ](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ ) environment (The Windows Sub-System for Linux (WSL) is unsupported by MongoDB):
1. Download the `MongoDB Community Edition Server` for Windows [here ](https://www.mongodb.com/download-center/community?jmp=docs ). Select `MongoDB Community Server` and verify the options that match your computer. Then `Download` the package and follow the installation instructions to complete the process.
2. After successfully installing MongoDB, the MongoDB service is started. To begin using MongoDB, connect a mongo.exe shell to the running MongoDB instance.
```bash
"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"
```
You can exit the MongoDB shell with `CTRL + C` .
You have now installed MongoDB for _Windows 10_ . You are now ready to [install Strapi with MongoDB locally ](#install-strapi-with-mongodb ).
:::
2019-11-07 12:05:39 +01:00
::: tab "Mac O/S"
2019-05-06 16:17:16 +02:00
2019-09-23 18:41:48 +02:00
#### Install MongoDB on Mac
2019-05-06 16:17:16 +02:00
Follow these steps to [install MongoDB onto your Mac ](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ ) developer environment:
1. Use `brew` to tap the official MongoDB formula repository and add it to the formula list:
```bash
brew tap mongodb/brew
```
2. Now install MongoDB
```bash
brew install mongodb-community@4 .0
```
3. Get the mongod process running in order to connect and use MongoDB:
```bash
mongod --config /usr/local/etc/mongod.conf
```
You have now installed MongoDB for _Mac_ . You are now ready to [install Strapi with MongoDB locally ](#install-strapi-with-mongodb ).
:::
2019-11-07 12:05:39 +01:00
::: tab "Ubuntu 18.04"
2019-05-06 16:17:16 +02:00
2019-09-23 18:41:48 +02:00
#### Install MongoDB on Ubuntu
2019-05-06 16:17:16 +02:00
Follow these steps to [install MongoDB onto your Ubuntu ](https://docs.mongodb.com/manual/administration/install-on-linux/ ) environment:
1. Import a public key to ensure your MongoDB is authentic:
```bash
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
```
2. Next, add the repository for 18.04. Repositories for other versions of Ubuntu are found [here ](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ ).
```bash
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
```
3. Reload the local package database:
```bash
sudo apt-get update
```
4. Install the stable release MongoDB package
```bash
sudo apt-get install -y mongodb-org
```
5. Get the `mongod` process running in order to connect and use MongoDB:
```bash
sudo service mongod start
```
6. Confirm the MongoDB status:
```bash
service mongod status
```
7. The last step is to enable automatically starting MongoDB when your computer boots:
```bash
sudo systemctl enable mongod
```
You have now installed MongoDB for _Linux_ . You are now ready to [install Strapi with MongoDB locally ](#install-strapi-with-mongodb ).
:::
::::
2019-09-23 18:41:48 +02:00
#### 2. Install Strapi locally with MongoDB
2019-05-06 16:17:16 +02:00
Follow these steps to create a Strapi project locally using the MongoDB database.
2019-11-07 12:05:39 +01:00
::: tip
2019-09-23 18:41:48 +02:00
MongoDB must already be running in the background.
:::
2019-05-06 16:17:16 +02:00
1. Create a new Strapi project
`Path: ./`
2019-11-07 12:05:39 +01:00
:::: tabs
2019-09-23 18:41:48 +02:00
2019-11-07 12:05:39 +01:00
::: tab yarn
2019-09-23 18:41:48 +02:00
```
yarn create strapi-app new my-project
```
:::
2019-11-07 12:05:39 +01:00
::: tab npx
2019-09-23 18:41:48 +02:00
```
npx create-strapi-app my-project
2019-05-06 16:17:16 +02:00
```
2019-09-23 18:41:48 +02:00
:::
::::
2019-05-06 16:17:16 +02:00
- Use your `down arrow` key and select `Custom (manual settings)` and press `enter` :
```bash
? Choose your installation type
Quickstart (recommended)
❯ Custom (manual settings)
```
- Select `MongoDB` and press `enter` :
```bash
? Choose your installation type Custom (manual settings)
? Choose your main database:
SQLite
❯ MongoDB
MySQL
Postgres
```
- Press `enter` to select the remaining default options. It will look something like this:
```bash
? Choose your installation type Custom (manual settings)
? Choose your main database: MongoDB
? Database name: my-project
? Host: 127.0.0.1
? +srv connection: false
? Port (It will be ignored if you enable +srv): 27017
? Username:
? Password:
? Authentication database (Maybe "admin" or blank):
? Enable SSL connection: false
⏳ Testing database connection...
The app has been connected to the database successfully!
🏗 Application generation:
✔ Copy dashboard
2019-05-16 11:14:32 +02:00
✔ Installed dependencies.
2019-05-06 16:17:16 +02:00
👌 Your new application my-project is ready at /Users/david/Desktop/Projects/my-project.
⚡️ Change directory:
$ cd my-project
⚡️ Start application:
2019-05-16 11:14:32 +02:00
$ strapi develop
2019-05-06 16:17:16 +02:00
```
2019-10-23 10:11:53 +02:00
You have successfully installed Strapi with MongoDB on your local development environment. You are now ready to [create your first user ](../getting-started/quick-start.md#_3-create-an-admin-user ).
2019-05-06 16:17:16 +02:00
---
### Install on Atlas: MongoDB Atlas
Follow these steps to configure a local Strapi project to use a [MongoDB Atlas ](https://www.mongodb.com/cloud/atlas ) free 512 MB account in production. (Please see [MongoDB Atlas Documentation ](https://docs.atlas.mongodb.com/getting-started/ ) if you have any questions.)
2019-10-23 10:11:53 +02:00
- You must have already [created your Strapi project using MongoDB ](databases.md#install-strapi-locally-with-mongodb ).
2019-05-06 16:17:16 +02:00
- You must have already created a [free MongoDB Atlas account ](https://www.mongodb.com/cloud/atlas ).
2019-09-23 18:41:48 +02:00
#### 1. Log in to your account to create a **Project** and a **Cluster**
2019-05-06 16:17:16 +02:00
- First you need to `Create a new Project` .
- Then click `Build a Cluster` , from the options page:
- Choose **AWS** as your **Cloud Provider & Region** .
- Select a **Region** . (Note: some **Regions** do not have a _free tier_ .)
- In **Cluster Tier** , select **Shared Sandbox** , _Tier_ `MO` .
- In **Cluster Name** , name your cluster.
- Click the green `Create Cluster` button. You will get a message that says, "_Your cluster is being created..._"
2019-09-23 18:41:48 +02:00
#### 2. Next, click on the `Database Access` in the left menu (under `Overview`):
2019-05-06 16:17:16 +02:00
- Click the green `+ ADD NEW USER` button:
- Enter a `username` .
- Enter a `password` .
- Under `User Privileges` ensure ** `Read and write to any database` ** is selected. Then click `Add User` to save.
2019-09-23 18:41:48 +02:00
#### 3. Then `whitelist` your IP address. Click into `Network Access`, under `Security` in the left menu:
2019-05-06 16:17:16 +02:00
- Click the green `+ ADD IP ADDRESS`
2019-07-16 16:18:04 +02:00
- Click `ADD CURRENT IP ADDRESS` or **manually** enter in an IP address to `whitelist` .
- Leave a comment to label this IP Address. E.g. `Office` .
- Then click the green `Confirm` button.
- Delete the `0.0.0.0/0` configuration after testing the connection.
2019-11-07 12:05:39 +01:00
::: tip
2019-09-23 18:41:48 +02:00
If for any reason you need to test the configuration or other aspect of your connection to the database, you may want to set back the `Allow Access from Anywhere` . Follow this steps:
:::
2019-07-16 16:18:04 +02:00
- Click the green `+ ADD IP ADDRESS`
- Next add `0.0.0.0/0` in the `Whitelist Entry` . **Note:** In permanent projects you would configure this with the appropriate IP addresses.
- Leave a comment to label this IP Address. E.g. `Anywhere` .
- Click `Confirm` . Then wait until the status turns from `Pending` to `Active` .
2019-11-07 12:05:39 +01:00
::: tip
2019-09-23 18:41:48 +02:00
If you are serving you Strapi project from a known IP Address then follow the following steps to `allow Network Access` .
:::
2019-07-16 16:18:04 +02:00
- **Manually** enter in an IP address to `whitelist` , for your Strapi server.
- Leave a comment to label this IP Address. E.g. `Heroku Server`
- Then click the green `Confirm` button.
2019-05-06 16:17:16 +02:00
2019-09-23 18:41:48 +02:00
#### 4. Retrieve database credentials
2019-05-06 16:17:16 +02:00
MongoDB Atlas automatically exposes the database credentials into a single environment variable accessible by your app. To locate it, follow these steps:
2019-07-16 16:18:04 +02:00
- Under `Atlas` in the left-hand, click on `Clusters` . This should take you to your `cluster` . Next, click `CONNECT` and then `Connect Your Application` .
2019-09-23 18:41:48 +02:00
- Under `1. Choose your driver version` , select **DRIVER** as `Node.js` and **VERSION** as `2.2.12 or later` .
::: warning
You **must** use `Version: 2.2.12 or later` .
:::
2019-05-06 16:17:16 +02:00
- This should show a **Connection String Only** similar to this:
2019-07-18 17:54:34 +02:00
`mongodb://paulbocuse:<password>@strapi-heroku-shard-00-00-oxxxo.mongodb.net:27017,strapi-heroku-shard-00-01-oxxxo.mongodb.net:27017,strapi-heroku-shard-00-02-oxxxo.mongodb.net:27017/test?ssl=true&replicaSet=Strapi-Heroku-shard-0&authSource=admin&retryWrites=true&w=majority`
2019-05-06 16:17:16 +02:00
2019-09-23 18:41:48 +02:00
::: warning
Please note the `<password>` after your `username` . In this example, after `mongodb://paulbocuse:` . You will need to replace the `<password>` with the password you created earlier for this user in your **MongoDB Atlas** account.
:::
2019-05-06 16:17:16 +02:00
2019-09-23 18:41:48 +02:00
#### 5. Update and replace your existing `/database.json` config file for the appropriate environment (development | production).
2019-05-06 16:17:16 +02:00
2019-07-18 17:54:34 +02:00
Replace the contents of `/database.json` with the following and replace ** < password > ** with the password of the user of your database you created earlier:
2019-05-06 16:17:16 +02:00
`Path: ./config/environments/(development|production)/database.json` .
```json
{
"defaultConnection": "default",
"connections": {
"default": {
2019-09-20 12:44:24 +02:00
"connector": "mongoose",
2019-05-06 16:17:16 +02:00
"settings": {
2019-07-18 17:54:34 +02:00
"uri": "mongodb://paulbocuse:< password > @strapidatabase -shard-00-00-fxxx6c.mongodb.net:27017,strapidatabase-shard-00-01-fxxxc.mongodb.net:27017,strapidatabase-shard-00-02-fxxxc.mongodb.net:27017/test?ssl=true& replicaSet=strapidatabase-shard-0& authSource=admin& retryWrites=true& w=majority"
2019-05-06 16:17:16 +02:00
},
"options": {
"ssl": true
}
}
}
}
```
2019-09-23 18:41:48 +02:00
::: warning NOTE
2019-07-22 17:40:35 +02:00
The above configuration will create a database called `strapi` , the _default database_ Strapi sets for any **MongoDB** database. If you would like to name your database something else, add the following **key:value pair** into your ** "settings":** located in your `database.json` file.
2019-07-18 17:54:34 +02:00
2019-07-22 17:40:35 +02:00
`"database": "my-database-name"`
2019-07-18 17:54:34 +02:00
:::
2019-09-23 18:41:48 +02:00
::: danger WARNING
2019-10-23 10:11:53 +02:00
We recommend replacing sensitive (eg. "URI string" above) information in your database.json files before uploading your project to a public repository such as GitHub. For more information about using environment variables, please read [dynamic configurations ](../concepts/configurations.md#dynamic-configurations ).
2019-07-18 17:54:34 +02:00
:::
2019-05-06 16:17:16 +02:00
You are now ready use Strapi locally or to deploy your project to an external hosting provider and use MongoDB Atlas as your database server.