Sails database configuration through environment variables - configuration

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

Related

Use multiple databases in Strapi v4

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.

Slaves not used in Symfony 4 with Raw Queries

I'm using Symfony 4 to interface with an existing Master/Slave MySQL setup and am executing queries against the server using raw sql. Raw SQL is the only option at the moment.
I'm using show full processlist; on the DB server to monitor which DB is used, and I am only seeing connections to the master server. It doesn't appear that any of the slaves are ever used.
For reference, I have two dbal connections setup, the default is NOT master/slave, and uses orm mapping. The second is the master/slave which I'm having issues with, and this is the server I'm executing raw sql queries against.
Below is my doctrine.yml:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: "%env(DATABASE_HOST)%"
dbname: "db1"
user: "%env(DATABASE_USER)%"
password: "%env(DATABASE_PASS)%"
charset: UTF8
ds:
driver: pdo_mysql
host: "%env(DS_DATABASE_HOST)%"
dbname: "db2"
user: "%env(DS_DATABASE_USER)%"
password: "%env(DS_DATABASE_PASS)%"
slaves:
slave1:
host: "%env(DS_DATABASE_SLAVE1_HOST)%"
user: "%env(DS_DATABASE_USER)%"
password: "%env(DS_DATABASE_PASS)%"
dbname: "db2"
slave2:
host: "%env(DS_DATABASE_SLAVE2_HOST)%"
user: "%env(DS_DATABASE_USER)%"
password: "%env(DS_DATABASE_PASS)%"
dbname: "db2"
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
ds:
connection: ds
I have configured my entity managers in my services.yml as follows:
# Entity managers
App\Service\Database\MainEntityManager:
arguments:
$wrapped: '#doctrine.orm.default_entity_manager'
App\Service\Database\DSEntityManager:
arguments:
$wrapped: '#doctrine.orm.ds_entity_manager'
The entity manager (in this case DSEntityManager) is injected into the constructor of a class, then the query is executed as such:
$result = $this->em->getConnection()->prepare($sql);
$result->execute($args);
Please let me know if I'm missing any helpful configuration.
Thanks a lot for the help.
Thanks #Cerad for the tip, that got me in the correct direction. Since I was no longer trying to use an entity manager for raw queries that were not mapped to entities, I could work with the connection directly.
I Created a wrapper class which extended MasterSlaveConnection. That worked as long as I was using executeQuery(). Per the docs, that must be used to query the slaves. However, my query required the use of prepare() and query() which both force the master connection.
So inside my new wrapper class I created two new methods, prepareSlave() and querySlave() which do the same as the original; however, they do $this->connect('slave'); instead of $this->connect('master');
Now all my read queries hit slave and everything else hits master.
So here are the following updates I've made to the configuration above to achieve this:
doctrine.yml
ds:
driver: pdo_mysql
host: "%env(DS_DATABASE_HOST)%"
dbname: "db2"
user: "%env(DS_DATABASE_USER)%"
password: "%env(DS_DATABASE_PASS)%"
wrapper_class: "%env(DS_DATABASE_PASS)%"
slaves: App\Service\Database\DSWrapper
slave1: ...
services.yml
# DBAL connections
App\Service\Database\DSWrapper: '#doctrine.dbal.ds_connection'
My new wrapper class
class DSWrapper extends MasterSlaveConnection
{
public function prepareSlave($statement)
{
$this->connect('slave');
try {
$stmt = new Statement($statement, $this);
} catch (\Exception $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
}
$stmt->setFetchMode($this->defaultFetchMode);
return $stmt;
}
public function querySlave()
{
$this->connect('slave');
$args = func_get_args();
$logger = $this->getConfiguration()->getSQLLogger();
if ($logger) {
$logger->startQuery($args[0]);
}
$statement = $this->_conn->query(...$args);
if ($logger) {
$logger->stopQuery();
}
return $statement;
}
}
So now if I need to execute a query which would normally require the use of prepare() and query(), I instead use prepareSlave() and querySlave().

Node.js connecting through ssh

I have a node.js server that works but needs to be set up for ssh connections:
var mysql = require('mysql')
var io = require('socket.io').listen(3000)
var db = mysql.createConnection({
host: 'hostname',
user: 'username',
password: '12345',
database: '12345',
port: 3306,
socket: '/var/run/mysqld/mysqld.sock'
})
db.connect(function(err){
if (err) console.log(err)
})
I'm aware that there are ssh npm libraries for this purpose, however the options available (ssh2, node-sshclient, etc) appear to deal with pretty intricate features that may overcomplicate things. I'm looking for the simplest way to connect to my mysql db through ssh. What would be the best way to accomplish this?
If you are running a linux/unix system do the following:
Connect to your mysql server via ssh and proxy the mysql port (default is 3306) via this ssh tunnel.
This works as follows:
1 Type in screen (to start a screen session which is permanent even if the shell gets closed).
2 Type into screen shell:
ssh -L 3306:127.0.0.1:3306 your_servers_domain_or_ip -lyour_login_name
3 Enter your ssh password / or use a PKI auth to avoid manual steps
4 Done... now it’s possible to connect MySQL like you would do when it’s installed on the same machine as your application.
Connect to MySQL from node.js like below:
var db = mysql.createConnection({
host: '127.0.0.1', // Important to connect to localhost after connecting via ssh in screen
user: 'username',
password: '12345',
database: '12345',
port: 3306
});
Sometimes it's preferrable to instantiate the SSH tunnel connection dynamically (in code) rather than separately using OS libraries. For example, it makes it easier to automatically close the connection, share the environment with other developers, or conditionally use an SSH tunnel depending on the environment.
With packages such as tunnel-ssh, this is easy. Building on the example provided, the connection code would look like:
import { createSSHTunnel } from "./sshTunnel";
const { srcAddr, srcPort } = await createSSHTunnel();
var db = mysql.createConnection({
host: srcAddr,
port: srcPort,
user: 'username',
password: '12345',
database: '12345'
});
With all logic cleanly abstracted away in the sshTunnel module, that could look like:
// sshTunnel.js
import { createTunnel } from "tunnel-ssh";
export async function createSSHTunnel(srcAddr = "127.0.0.1", srcPort = 12345) {
const tunnelOptions = {
autoClose: true,
};
const serverOptions = {
port: srcPort,
};
const sshOptions = {
host: process.env.SSH_HOST,
port: parseInt(process.env.SSH_PORT),
username: process.env.SSH_TUNNEL_USER,
password: process.env.SSH_TUNNEL_PASSWORD,
};
const forwardOptions = {
srcAddr: srcAddr,
srcPort: srcPort,
dstAddr: process.env.DB_HOST,
dstPort: parseInt(process.env.DB_PORT),
};
try {
await createTunnel(
tunnelOptions,
serverOptions,
sshOptions,
forwardOptions
);
} catch (error) {
if (error.code === "EADDRINUSE") {
// Assume port is uniquely used by SSH tunnel, so existing connection can be reused
console.log(`Returning existing SSH tunnel on ${srcAddr}:${srcPort}.`);
return { srcAddr, srcPort };
} else {
throw error;
}
}
console.log(`SSH tunnel successfully created on ${srcAddr}:${srcPort}.`);
return { srcAddr, srcPort };
}
Remarks:
The SSH tunnel arbitrarily uses local port 12345
The environment variables involved are:
DB_HOST: the database hostname
DB_PORT: the database port, 3306 in the original MySQL example, 5432 for Postgres etc.
SSH_HOST: the hostname of the machine serving the SSH tunnel
SSH_PORT: the port of the machine serving the SSH tunnel
SSH_TUNNEL_USER: the username for the SSH tunnel
SSH_TUNNEL_PASSWORD: the password for the SSH tunnel

Using local.js to store sails-mysql password

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.

Sailsjs - How to use mySQL

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.