7

我似乎无法理解http://strapi.io/documentation/configuration#databases中的文档

如何连接到 MySqlDB?在 databases.json 中,我在哪里设置我的所有数据库设置,例如用户:root、密码:secret123、主机:192.12.2.123 等?

4

3 回答 3

9

2020年解决方案

  1. 添加客户端:
  • Strapi < 3.2.5:yarn add sails-mysqlnpm install sails-mysql
  • Strapi >= 3.2.5:yarn add mysqlnpm install mysql
  1. 更改配置 <project>/config/database.js(用于开发)或<project>/config/env/production/database.js(用于生产)
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. 也可能您需要运行yarn build以重建您的 CMS。
于 2020-08-28T12:26:44.010 回答
3

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.

于 2017-08-16T17:33:57.237 回答
2

Sails 框架和 Waterline ORM 的 MySQL 适配器。允许您通过模型使用 MySQL 来存储和检索数据。还为执行原始 SQL 命令的直接接口提供了 query() 方法。

从 NPM 安装。

在您的应用中:

$ npm install sails-mysql

帆配置

将 mysql 配置添加到 config/connections.js 文件中。基本选项:

module.exports.connections = {
  mysql: {
    module    : 'sails-mysql',
    host      : 'localhost',
    port      : 3306,
    user      : 'username',
    password  : 'password',
    database  : 'MySQL Database Name'

    // OR (explicit sets take precedence) 
    module    : 'sails-mysql',
    url       : 'mysql2://USER:PASSWORD@HOST:PORT/DATABASENAME'

    // Optional 
    charset   : 'utf8',
    collation : 'utf8_swedish_ci'
  }
};

然后将默认模型配置更改为 config/models.js:

module.exports.models = {
  connection: 'mysql'
};

运行测试

您可以设置环境变量来覆盖测试的默认数据库配置,例如:

$ WATERLINE_ADAPTER_TESTS_PASSWORD=yourpass npm test
Default settings are:

{
  host: process.env.WATERLINE_ADAPTER_TESTS_HOST || 'localhost',
  port: process.env.WATERLINE_ADAPTER_TESTS_PORT || 3306,
  user: process.env.WATERLINE_ADAPTER_TESTS_USER || 'root',
  password: process.env.WATERLINE_ADAPTER_TESTS_PASSWORD || '',
  database: process.env.WATERLINE_ADAPTER_TESTS_DATABASE || 'sails_mysql',
  pool: true,
  connectionLimit: 10,
  waitForConnections: true
}

参考:https ://www.npmjs.com/package/sails-mysql

于 2017-08-16T18:05:48.163 回答