Connecting to MySql DB in strapi

codely picture codely · Aug 16, 2017 · Viewed 8.3k times · Source

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.?

Answer

Ilya Iksent picture Ilya Iksent · Aug 28, 2020

Solution for 2020 (Strapi 3.1.3)

  1. Add client yarn add sails-mysql

  2. 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.