我似乎无法理解http://strapi.io/documentation/configuration#databases
中的文档如何连接到MySqlDB?在databases.json中,我设置所有我的数据库设置,如user:root,pwd:secret123,host:192.12.2.123等?
答案 0 :(得分:4)
2020年解决方案(Strapi 3.1.3)
添加客户端
yarn add sails-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。答案 1 :(得分:2)
您应该添加如下新连接:
{
"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"
}
}
}
当前版本的Strapi基于Waterline,因此如果您无法在文档中找到正确的信息,请查看Waterline / Sails文档。
答案 2 :(得分:1)
用于Sails框架和Waterline ORM的MySQL适配器。允许你 通过模型使用MySQL来存储和检索数据。还提供了一个 query()方法,用于执行原始SQL命令的直接接口。
从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
}