我似乎无法理解http://strapi.io/documentation/configuration#databases中的文档
如何连接到 MySqlDB?在 databases.json 中,我在哪里设置我的所有数据库设置,例如用户:root、密码:secret123、主机:192.12.2.123 等?
我似乎无法理解http://strapi.io/documentation/configuration#databases中的文档
如何连接到 MySqlDB?在 databases.json 中,我在哪里设置我的所有数据库设置,例如用户:root、密码:secret123、主机:192.12.2.123 等?
2020年解决方案
yarn add sails-mysql
或npm install sails-mysql
yarn add mysql
或npm install mysql
<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,
},
},
},
});
yarn build
以重建您的 CMS。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.
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
}