Error configuring multiples mongodb servers in Symfony 2.8 - mongodb

I already have my app ( Symfony 2.8) hosted in Heroku. I have a mongoDB plugin to store session and it's working fine but when I try to add a replica server I face some problems. Replica is providaded by default for mongodb plugin. I mean
I am going to explain my environment and configuration:
ENV
I want to use this url ( it's which mongodb plugin provide me in Heroku)
MONGOLAB_URI = "mongodb://heroku_user:heroku_password#ds245975-a0.mlab.com:45975,ds245975-a1.mlab.com:45975/heroku_3b4l8zhl?replicaSet=rs-ds245975"
If I use this one is working:
MONGOLAB_URI = mongodb://heroku_user:heroku_password#ds245975-a0.mlab.com:45975/heroku_3b4l8zhl
CONF
In parameters.php I get this info:
$mongoLabUri = getenv('MONGOLAB_URI');
$mongoLabUri = getenv('MONGOLAB_URI');
if (false !== $mongoLabUri) {
$matches = [];
preg_match(
'#mongodb://(?P<user>.*?):(?P<password>.*?)#(?P<server>.*)/(?P<database>[^?]*)#',
$mongoLabUri,
$matches
);
$container->setParameter('mongo_server', 'mongodb://'.$matches['user'].':'.$matches['password'].'#'.$matches['server']."/".$matches['database']);
$container->setParameter('mongo_database', $matches['database']);
}
And in the configuration file, I configured the session handler to work with below parameters:
config.yml
doctrine_mongodb:
connections:
default:
server: %mongo_server%
options: {}
default_database: %mongo_database%
document_managers:
default:
auto_mapping: true
services:
session.handler.mongo:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler
arguments: [#mongo, %mongo.session.options%]
mongo.connection:
class: MongoDoctrine\MongoDB\Connection
factory_service: doctrine.odm.mongodb.document_manager
factory_method: getConnection
calls:
- [initialize, []]
mongo:
class: Mongo
factory_service: mongo.connection
factory_method: getMongo
parameters:
mongo.session.options:
database: %mongo_database%
collection: session
Things that I tried
Configured the mongo_server with ?replicaSet=rs-ds245975
Remove ?replicaSet=rs-ds245975
I connect from mongo console to both server and it works.
Modified symfony configuration:
-https://symfony.com/doc/current/doctrine/mongodb_session_storage.html
-https://symfony.com/doc/master/bundles/DoctrineMongoDBBundle/config.html
Anything is working. I face different problem when I try to set the replica. It seems that there are some misunderstood that right now I am not able to see.
Any help/tip is welcome.

I need to modified the configuration of doctrine_mongodb in this way:
doctrine_mongodb:
connections:
default:
server: %mongo_server%
options:
connect: true
replicaSet: true
slaveOkay: true
default_database: %mongo_database%
document_managers:
default:
auto_mapping: true
And now is working

Related

connecting to mongodb replicaSet with nestjs and typeorm is not working

problem
I'm trying to connect to mongodb with nestjs(^8.2.3) and typeorm(^0.2.28)
In test environment, connecting to mongodb standalone server is working. For your information, node mongodb library version is ^3.6.2.
production sample code(nestjs server)
I referred the typeorm code to write mongodb options
import { TypeOrmModule } from '#nestjs/typeorm';
import { MongoConnectionOptions } from 'typeorm/driver/mongodb/MongoConnectionOptions';
export const configForOrmModule = TypeOrmModule.forRootAsync({
imports: [],
useFactory: async () => {
const mongodbConfig: MongoConnectionOptions = {
type: 'mongodb',
username,
// for replicaSet (production)
hostReplicaSet: 'server1.example.com:20723,server2.example.com:20723,server.example.com:20723',
replicaSet: 'replicaSetName'
port: Number(port),
password: encodeURIComponent(password),
database,
authSource,
synchronize: true,
useUnifiedTopology: true,
entities: [Something],
};
return mongodbConfig;
},
inject: [],
});
But in production environment, when nestjs server try to connect to mongodb replicaSet, the server get this server selection loop error message over and over again like below. Interesting thing was the domain that the server tried to connect was different from replicaSet hosts(ex. another-hostname not included in server1.example.com:20723,server2.example.com:20723,server.example.com:20723). (+ edited: the another hostname is actual physical server indicated by the dns(server.example.com))
[39m01/28/2022, 2:39:16 AM [31m ERROR[39m [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (3)...[39m
MongoServerSelectionError: getaddrinfo ENOTFOUND <another-hostname>
at Timeout._onTimeout (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:430:30)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
what I’ve tried but these not worked
remove useUnifiedTopology: true option
downgrade mongodb library version to 3.5.11 (I've read in mongodb community there are something bug with topology after 3.6 version)
use host option not the hostReplicaSet
if you need more information, please tell me. thank you for your helping.
It was kubernates DNS issue. The hostReplicaSet server1.example.com:20723,... is resolved to host1 (physical server name. without example.com) but, k8s doesn't know it. so connection was failed.
there are two options
update kubernates /etc/hosts setting to add host1 -> host1.example.com
or update mongodb hostname host1 -> host1.example.com

I could not connect Strapi with MongoDB using Visual Studio Code. Please assist me in this

And I am repeatedly getting the following error message:
Where is the problem? How do I connect strapi to MongoDD?
You need to first connect to MongoDB locally. Keep everything blank, or enter the values for your locally running instance of MongoDB. Then, you can retrieve the connection URL from Atlas or wherever, and then you need to edit your config/database.js:
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'mongoose',
settings: {
uri: env('DATABASE_URI')
},
options: {
ssl: true
}
}
}
});
Then, in your .env file:
DATABASE_URI=[CONNECTION_STRING_HERE]
Restart the server and you will be connected with your cloud instance of MongoDB.
If you still have issues, you can see the strapi docs: https://strapi.io/documentation/developer-docs/latest/guides/databases.html#install-on-atlas-mongodb-atlas.

I can not connect to Postgres DB with Strapi on Heroku

Trying to deploy Strapi on Heroku with Postgres as described here
https://strapi.io/documentation/v3.x/deployment/heroku.html
But I get this error
error: no pg_hba.conf entry for host "84.212.51.43", user "ssqqeaz***", database "d6gtu***", SSL off
I use Heroku Postgres add-on.
My database config:
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'postgres',
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 27017),
database: env('DATABASE_NAME', 'strapi'),
username: env('DATABASE_USERNAME', ''),
password: env('DATABASE_PASSWORD', ''),
},
options: {
ssl: true
},
},
},
});
Why? Please help!
try to change ssl : true into ssl : false
The current configuration you've posted will not work with a Heroku Postgres database. The primary concern here is that you're reading components of your postgres database url out of manually set config vars. This is very much recommended against by Heroku because they may need to move the database to a new host in the case of disasters. DATABASE_URL is set by Heroku when you create a database on an app and it's the one config var you can rely on to stay up-to-date. Moving on...
You will need to parse the username, password, host, port and database name out of the DATABASE_URL config var and supply those to the attributes of the settings block. Based on the error you provided, I can tell you're not presently doing this because Heroku databse usernames all start with a 'u', so something is very wrong if you get the error user "ssqqeaz***". As a first step you might try hard coding these values in the settings block to make sure it works (make sure to rotate the credentials after you do it, or otherwise clean up your git history to prevent leaked creds). The pattern for a postgres connection url is something like this: postgres:// $USERNAME : $PASSWORD # $HOSTNAME : $PORT / $DATABASE_NAME.
Not sure if it will help moving your config around...
remove ssl from option Key
insert ssl after password inside of settings Key
eg.
ssl: env.bool('DATABASE_SSL', false),
also check your app config vars inside of Heroku and make sure you have the required postgres config vars setup and they match the heroku generated DATABASE_URL config var.
lastly check your ./config/server.js file and make sure your host is 0.0.0.0
eg.
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
admin: {
auth: {
secret: env('ADMIN_JWT_SECRET', '**********************************'),
},
},
});

How to store mongo db backups to google drive using Symfony 3.4

I am trying to upload mongo db backup to google drive
I am installing following bundles dizda/cloud-backup-bundle and Happyr
/
GoogleSiteAuthenticatorBundle for adapters I am using cache/adapter-bundle
configuration:
dizda_cloud_backup:
output_file_prefix: '%dizda_hostname%'
timeout: 300
processor:
type: zip # Required: tar|zip|7z
options:
compression_ratio: 6
password: '%dizda_compressed_password%'
cloud_storages:
google_drive:
token_name: 'AIzaSyA4AE21Y-YqneV5f9POG7MPx4TF1LGmuO8' # Required
remote_path: ~ # Not required, default "/", but you can use path like "/Accounts/backups/"
databases:
mongodb:
all_databases: false # Only required when no database is set
database: '%database_name%'
db_host: '%mongodb_backup_host%'
db_port: '%mongodb_port%'
db_user: '%mongodb_user%'
db_password: '%mongodb_password%'
cache_adapter:
providers:
my_redis:
factory: 'cache.factory.redis'
happyr_google_site_authenticator:
cache_service: 'cache.provider.my_redis'
tokens:
google_drive:
client_id: '85418079755-28ncgsoo91p69bum6ulpt0mipfdocb07.apps.googleusercontent.com'
client_secret: 'qj0ipdwryCNpfbJQbd-mU2Mu'
redirect_url: 'http://localhost:8000/googledrive/'
scopes: ['https://www.googleapis.com/auth/drive']
when I use factory: 'cache.factory.mongodb' getting
You have requested a non-existent service "cache.factory.mongodb" this while running server and while running backup command getting
Something went terribly wrong. We could not create a backup. Read your log files to see what caused this error
I verified logs getting Command "--env=prod dizda:backup:start" exited with code "1" {"command":"--env=prod dizda:backup:start","code":1} []
I am not sure which adapter needs to use and what's going on here.
Can someone help me? Thanks in advance

Sails v0.10.x doesn't respect connection config in production.js when in production mode

What is the correct way to set a db connection in Sails v.0.10.x for production use? I expected Sails to use the connection I referred to in production.js when I start my app in production mode (environment), but it doesn't. It seems to always always use the default connection - 'localDiskDb'.
However, when I start sails in development mode (environment), it does use the connection specified in config/development.js, as I would have expected.
UPDATED
Note: I was mistaken when I wrote the location of production.js. It was in /config/env/production.js just like sgress454 said it should be. Actually, this file was created by the generator and put in the right place and I didn't change that.
config/env/production.js looks like this:
// config/env/production.js
module.exports = {
connection: 'mongo_production'
};
config/models.js looks like this:
// config/models.js
module.exports.models = {
// connection: 'localDiskDb'
};
config/connections.js looks like this:
// config/connections.js
module.exports.connections = {
mongo_development: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'my_local_db'
},
mongo_production: {
adapter: 'sails-mongo',
url: 'mongodb://me:mypw#foobar.mongohq.com:10052/my_production_db'
}
};
Couple of issues here:
Per-environment configuration files need to go in the config/env subdirectory, or else they'll be treated the same as regular config files (i.e., not given precedence). If you have multiple files trying to set the same key, results will be unpredictable.
You're attempting to change the default connection for models by setting the connection config key; it needs to be models.connection.
Putting the two together, you need a config/env/production.js file that looks like this:
module.exports = {
models: {
connection: 'mongo_production'
}
};
Then when lifting in production mode, models will use the mongo_production connection by default.
In sails version 0.12 there will be two env files under config/env folder.
So we can write mode specific (production/development) configuration in those two files.
In order to run our sails app in specific mode, we must follow the below steps:
Step 1)
In config/local.js file
module.exports = {
// port: process.env.PORT || 1337, // comment this line if you want to set the different ports for different modes
environment: process.env.NODE_ENV || 'development'
};
Step 2) In env/developement.js file
Write the development specific configurations.
module.exports = {
port: 8080, // will change from default port 1337 to 8080
models: {
connection: 'developement_db',
migrate: 'alter'
}
};
Step 3) In env/production.js file
Write the production specific configurations.
module.exports = {
port: 9090, // will change from default port 1337 to 9090
models: {
connection: 'production_db',
migrate: 'safe'
}
};
Step 4) To run sails app in specific mode,
To run in production mode
$ NODE_ENV=production npm start
Sails will be running on port 9090
To run in developement mode
$ NODE_ENV=developement npm start
Sails will be running on port 8080