I want to modify the file config/database.js to use multiple databases. I need to use sqlite for local and MySQL for dev and prod. Is there any way to do this? I’m using Strapi 4.6.0
This is what I have in database.js:
module.exports = ({ env }) => ({
connection: {
client: 'mysql',
connection: {
host: env('DATABASE_HOST'),
port: env.int('DATABASE_PORT'),
database: env('DATABASE_NAME'),
user: env('DATABASE_USERNAME'),
password: env('DATABASE_PASSWORD'),
ssl: env.bool('DATABASE_SSL', false),
},
},
});
but I need to use MySQL only for dev and prod, and use sqlite for local
There's this section in the docs that could assist your use case: https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/optional/environment.html#environment-configurations
Basically, you can create several config files that target different environments following the convention: ./config/env/{environment}/{filename}.
E.g.
./config/env/development/database.js
./config/env/production/database.js.
Related
I've a MySQL database storing decimal values (DECIMAL(32, 12)).
When I select the values using HeidiSQL, the values are shown correctly (e.g. 15922.638440778302). But when I do the same in NodeJS using the MySQL binding (https://github.com/mysqljs/mysql), it shows me 15922.638440778303.
My SQL-Query is pretty simple:
SELECT SUM(`amount`) FROM `balances`;
Any idea what can cause the difference? Maybe do I need to specify the precision during the MySQL initialization in NodeJS? At the moment, I don't specify anything other than the login credentials:
let mysql = require('mysql').createPool({
host: global.dbconfig['dbhost'],
database: global.dbconfig['dbname'],
user: global.dbconfig['dbuser'],
password: global.dbconfig['dbpass']
})
Add below lines to your mysql config.
supportBigNumbers: true
bigNumberStrings: true
Then it becomes:
let mysql = require('mysql').createPool({
host: global.dbconfig['dbhost'],
database: global.dbconfig['dbname'],
user: global.dbconfig['dbuser'],
password: global.dbconfig['dbpass'],
supportBigNumbers: true,
bigNumberStrings: true,
})
More info on connection options: docs
I used to configure Sails connection to my database in config/env/development.js and config/env/production.js like that:
module.exports = {
connections: {
'postgres': {
host: 'localhost',
user: 'myUser',
password: 'myPassword',
database: 'myDatabase'
}
}
};
What if I would like to replace my environment config files by environment variables as explained here?
I expected to use those variables but it doesn't work:
sails__connections_postgres_host
sails__connections_postgres_user
sails__connections_postgres_password
sails__connections_postgres_database
Your underscores are backwards. Looking at the docs you linked, your variables should look like:
sails_connections__postgres__host
etc. (one underscore after 'sails', two underscores in between each key after that).
Additionally, it is worth noting that you can reference environment variables in your code, so the option exists to refer to an environment variable, 'dbHostname' in your config/env/development.js (or production) as such:
module.exports = {
connections: {
'postgres': {
host: process.env.dbHostname,
user: process.env.dbUser,
password: process.env.dbPassword,
database: process.env.db
}
}
}
and then create these environment variables upon lifting your server, i.e.
dbHostname="http://something" dbUser="Your_User" dbPassword="password" db="database" sails lift
I'm trying to connect to a MAMP MySQL database from Wakanda 11.
I tried connecting to the localhost database using Connect to Remote Datastore but I keep getting a Connection failed response. I've also tried using port 127.0.0.1:8081 which it connects to but not when attempting to use port:3306 which is where I have the MySQL database configured on the MAMP server. What am I doing wrong?
I've tried the following script...
model.addSQLCatalog("mysqldb", {
hostname: '127.0.0.1',
port: 3306,
user: 'root',
password: 'xxxxxxxx',
database: 'my_database_name',
ssl: false,
dbType: 'mysql'
});
...but this gives me the following error:
TypeError: JSON.stringify cannot serialize cyclic structures.
How can I fix and make it work on port:3306 so I can see my database structures?
If you are using Wakanda Enterprise Edition 11 or higher there is a MySQL Connector Pro.
Here is an example of connecting with this:
model.mergeSQLCatalog(localName, {
hostname: string 'host name',
port: number remote_port_number,
user: string 'userName',
password: string 'password',
database: string 'SQL database name',
jsFile: string 'configuration JavaScript file',
ssl: boolean true or false,
dbType: string 'mysql' } )
If you are on an older version of Wakanda Enterprise Edition but still using version 7 or higher then there is a Wakanda/MySQL connector.
Here is an example of connecting to MySQL with the connector:
var sql = require('waf-sql');
//use port 3306 and do not use SSL
var dbconn = sql.connect('mysql','192.168.0.21', 'john', 'x54?hsf5x!','arts',3306,false);
var rs = dbconn.update("people", {
name: "smith",
age: 42
}, {
id: 1
});
var rs = dbconn.select("*", "people", {
id: 1
});
var row = rs.getNextRow(); // get the first row
dbcon.close(); // close connection
Here is an example of the available parameters:
var params = {
hostname: [your host name or IP address],
user: [the user name of your DB],
password: [the user password],
database: [the DB name],
port: [the port number of the MySQL Server, by default 3306],
ssl: false,
dbType : 'mysql'
};
If I'm correct your issue has been solved? Does it work for both Windows & Mac ?
To resume , to use ProCOnnector you'll need :
The Enterprise Version of Wakanda
For the current v11 version of Wakanda, you need to use in the model.js file the addSQLCatalog() API.
The parameters accepted are described in the doc
Please note in the next version of Wakanda we 'll provide to wizzard to connect to other DBs instead of the addSQLCatalog(). This will help and ease the process.
I'm trying to use mysql as my database, but I can't figure out how to get my config/adapters.js to use mysql information in config/local.js to connect. What's the correct way to store the connection information so that sails-mysql can connect?
config/local.js is merged on top of all other configuration. So you can just put your own adapters key in there:
{
adapters: {
default: 'myLocalAdapter',
myLocalAdapter: {
module: 'sails-mysql',
host: 'localhost',
user: 'root',
password: 'password',
database: 'database'
}
}
}
and it'll be picked up and used by Sails.
EDIT In sails v0.10.x, use the key connections instead of adapters as the two are now differentiated and connections now hold authentication info. The original answer was for an earlier version of sails.
As of latest version of sails (i.e 0.12), while writing this answer, there is no Adapters.js. The list of connections are specified in the config/connections.js and default connection to use for all models are specified under config/models.js. So, going by this change the way to override the database configuration for local is specifying as below in config.local.js.
module.exports = {
connections: {
localMongoServer: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'YOUR_DB_USER',
password: 'YOUR_DB_PASSWORD',
database: 'YOUR_DB_NAME'
}
},
models: {
connection: 'localMongoServer'
}
}
NOTE: As in accepted answer, just changing adapters to connections won't work, since there is no default attribute under connections.
How to use mysql with Sails ? Also, if I change the database to a mySQL one, do I lose all my current model data ?
I spent some time looking for tutorials and demos but haven't found any.
In order to use mySQL with sails, you will have to:
Define an adapter in config/adapters
Example adapters file:
module.exports.adapters = {
'default': 'disk',
disk: {
module: 'sails-disk'
},
'mysql-adapter': {
module: 'sails-mysql',
host: 'HOST',
user: 'USER',
password: 'PASSWORD',
database: 'DATABASE'
}
};
Make any model use the new mysql adapter (ex. api/models/Contact.js)
module.exports = {
tableName: 'contacts',
adapter: 'mysql-adapter',
migrate: 'safe',
attributes: { ... }
}
Then it should work.