Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to MySql DB in strapi

Tags:

strapi

I can't seem to understand the documentation in http://strapi.io/documentation/configuration#databases

How to connect to MySqlDB? Where in databases.json do i set all my db settings like user: root, pwd: secret123, host: 192.12.2.123, etc.?

like image 990
codely Avatar asked Aug 16 '17 17:08

codely


People also ask

Can I use MySQL on Strapi?

To connect Strapi with MySQL, you have to make sure that the authentication plugin for our root user is mysql_native_password . Currently, Strapi doesn't support the auth plugin that comes with MySQL 8.0.

Which DB to use with Strapi?

By default, Strapi uses the SQLite for content storage, but Strapi is not only limited to using SQLite as the database. It can be configured to use other databases like MySQL, MariaDB, PostgreSQL, etc.


2 Answers

You should add a new connection like this:

{
  "orm": {
    "adapters": {
      "mysql": "sails-mysql"
    },
    "defaultConnection": "default",
    "connections": {
      "someMysqlServer": {
        "adapter": "mysql",
        "host": "YOUR_MYSQL_SERVER_HOSTNAME_OR_IP_ADDRESS",
        "user": "YOUR_MYSQL_USER",
        "password": "YOUR_MYSQL_PASSWORD",
        "database": "YOUR_MYSQL_DB"
      }
    }
  }
}

The current version of Strapi is based on Waterline so if you can't find the right info in the documentation, take a look at the Waterline/Sails documentation as well.

like image 41
Aurélien Georget Avatar answered Oct 05 '22 22:10

Aurélien Georget


Solution for 2020

  1. Add client:
  • Strapi < 3.2.5: yarn add sails-mysql or npm install sails-mysql
  • Strapi >= 3.2.5: yarn add mysql or npm install mysql
  1. Change config <project>/config/database.js (for development) or <project>/config/env/production/database.js (for production)
module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: "mysql",
        host: env('DATABASE_HOST', 'localhost'),
        port: env('DATABASE_PORT', 3306),
        database: env('DATABASE_NAME', 'default'),
        username: env('DATABASE_USERNAME', 'root'),
        password: env('DATABASE_PASSWORD', ''),
      },
      options: {
        useNullAsDefault: true,
      },
    },
  },
});
  1. Also may be you'll need to run yarn build to rebuild your CMS.
like image 110
Ilya Iksent Avatar answered Oct 05 '22 22:10

Ilya Iksent