Setting up mongodb config in Laravel with Compose.io - mongodb

Its my first time use compose.io as my mongodb hosting.
I was trying to configure compose.io mongodb with Laravel but ended up this error:
ConnectionTimeoutException in Collection.php line 432:
No suitable servers found (`serverSelectionTryOnce` set)
I was using https://github.com/jenssegers/laravel-mongodb package to add mongodb support to Laravel.
My mongodb config:
'mongodb' => [
'driver' => 'mongodb',
'host' => ['aws-us-east-1-portal.25.dblayer.com:20020/admin', 'aws-us-east-1-portal.26.dblayer.com:20020/admin'],
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [
'ssl' => true,
'database' => 'admin', // sets the authentication database required by mongo 3
'replicaSet' => 'set-5939226a8aab5300121d0ef2',
'readPreference' => 'primary',
],
'driver_options' => [
'context' => stream_context_create( [
'ssl' => [
'local_cert' => base_path('mongo.pem'),
'cafile' => base_path('mongo.pem'),
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
'verify_expiry' => false,
'allow_invalid_certificates' => true
]
])
]
]
I am not also sure what is the value for MONGO_REPLICA_SET
Anyone experienced something similar?
Thanks

It works by removing replicaSet option
Final configuration:
'mongodb' => [
'driver' => 'mongodb',
'host' => ['aws-us-east-1-portal.25.dblayer.com', 'aws-us-east-1-portal.26.dblayer.com'],
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [
'ssl' => true,
'database' => env('MONGO_DB_DATABASE'), // sets the authentication database required by mongo 3
],
'driver_options' => [
'context' => stream_context_create( [
'ssl' => [
'cafile' => base_path('mongo.pem'),
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
'verify_expiry' => false,
]
])
]
]

Related

Laravel 8 using graylog - unicode problem

I need to integrate Graylog packet. I found this 'https://github.com/hedii/laravel-gelf-logger'
This is my config file
config/logging.php
'default' => 'stack',
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'gelf'],
],
'gelf' => [
'driver' => 'custom',
'via' => \Hedii\LaravelGelfLogger\GelfLoggerFactory::class,
'processors' => [
\Hedii\LaravelGelfLogger\Processors\NullStringProcessor::class,
// another processor...
],
'level' => 'debug',
'name' => 'bms-admin',
'system_name' => null,
'transport' => 'udp',
'host' => '192.168.41.112',
'port' => '5141',
'path' => null,
'max_length' => null,
'context_prefix' => null,
'extra_prefix' => null,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
]
When Laravel logged something, I received

Connection to mongodb atlas in codeigniter framework getting error

What i am trying to do is to connect mongodb atlas with my codeigniter framework.
I have used this library for mongodb
https://github.com/verkhoumov/codeigniter-mongodb-library
Below is my connection code
$config['mongo_db']['default'] = [
'settings' => [
'auth' => TRUE,
'debug' => TRUE,
'return_as' => 'array',
'auto_reset_query' => TRUE
],
'connection_string' => '',
'connection' => [
'host' => 'host link',
'port' => '27017',
'user_name' => 'username',
'user_password' => 'password',
'db_name' => 'dbname',
'db_options' => []
],
'driver' => []
];
The main problem is mongodb atlas doesnt provide you the port its query string is like mongodb+srv not mongodb:27017
MongoDb Atlas provides a connection string.
You can find it under Clusters -> Connect Your Application -> Copy a connection string.
Make sure to change "test" with your database name.
Then set it in 'connection_string' in your config file. Other parameters keep default.
$config['mongo_db']['default'] = [
'settings' => [
'auth' => TRUE,
'debug' => TRUE,
'return_as' => 'array',
'auto_reset_query' => TRUE
],
'connection_string' => 'mongodb://************#cluster0-shard-00-00-5ehze.gcp.mongodb.net:27017,cluster0-shard-00-01-5ehze.gcp.mongodb.net:27017,cluster0-shard-00-02-5ehze.gcp.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true',
'connection' => [
'host' => '',
'port' => '',
'user_name' => '',
'user_password' => '',
'db_name' => '',
'db_options' => []
],
'driver' => []
];

How to connect Mongodb with Yii2

This is what I have in common/config/main-local:
'mongodb' => [
'class' => 'yii\mongodb\Connection',
'dns' => 'mongodb://localhost:27017/test',
],
What's wrong with that?
Replace "localhost" to "127.0.0.1"
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://127.0.0.1:27017/test',
],
There's a typo. It should be
'dsn' => 'mongodb://localhost:27017/test'
instead of
'dns' => 'mongodb://localhost:27017/test'

CakePHP 3 Rest Api JWT plugin Form login Not working

I am using ADmad/cakephp-jwt-auth plugin for Json web token authentication for rest api.
For the token generation we i am sending email and password to login via form . but it fails i don't what is issue.
Here is the my settings. in AppController.php
$this->loadComponent('Auth', [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
],
'authenticate' => [
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => [
'id' => 'id'
]
]
],
'unauthorizedRedirect' => false,
// Config below is available since CakePHP 3.1.
// It makes user info available in controller's beforeFilter() which is not possible in CakePHP 3.0.
'checkAuthIn' => 'Controller.initialize',
]);
UsersController Token generation method script;
public function token(){
$user = $this->Auth->identify();
if (!$user) {
throw new UnauthorizedException('Invalid email or password');
}
$this->set([
'success' => true,
'data' => [
'token' => $token = \JWT::encode([
'id' => $user['id'],
'exp' => time() + 604800
],
Security::salt())
],
'_serialize' => ['success', 'data']
]);
}
The json data i am posting is
{
'email' : 'muni#smart.com',
'password': '123'
}
Please say me what is mistake?
Form must be in the authenticate
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
],
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'fields' => [
'id' => 'id'
]
]
]
]);

ZF 2.4 File Validator Required False Doesn't Work

Today I updated to ZF 2.4 to use float validator but unfortunately i realized that my file upload form field gives unexpected error messages.
Here is my form object
$this->add([
'name' => 'profileimage',
'type' => '\Zend\Form\Element\File',
'attributes' => [
'id' => 'profileimage',
'class' => 'styled',
],
]
);
And Here is my validator
$inputFilter->add([
'name' => 'profileimage',
'required' => false,
'allow_empty' => true,
'priority' => 300,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => '\Zend\Validator\File\IsImage',
],
[
'name' => '\Zend\Validator\File\UploadFile',
],
[
'name' => '\Zend\Validator\File\ImageSize',
'options' => [
'minWidth' => 300,
'minHeight' => 300,
]
],
[
'name' => '\Zend\Validator\File\Size',
'options' => [
'max' => '20MB',
]
],
]
]);
As you see the image upload field is not required and may be empty. But in my form I get these errors:
array (size=1)
'profileimage' =>
array (size=4)
'fileIsImageNotReadable' => string 'File is not readable or does not exist' (length=38)
'fileUploadFileErrorNoFile' => string 'File was not uploaded' (length=21)
'fileImageSizeNotReadable' => string 'File is not readable or does not exist' (length=38)
'fileSizeNotFound' => string 'File is not readable or does not exist' (length=38)
How can I handle this issue? I need to this field to be optional.
change your filter
$inputFilter->add([
'name' => 'profileimage',
'type' => '\Zend\InputFilter\FileInput',
'required' => false,
'allow_empty' => true,
'priority' => 300,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => '\Zend\Validator\File\IsImage',
],
[
'name' => '\Zend\Validator\File\UploadFile',
],
[
'name' => '\Zend\Validator\File\ImageSize',
'options' => [
'minWidth' => 300,
'minHeight' => 300,
]
],
[
'name' => '\Zend\Validator\File\Size',
'options' => [
'max' => '20MB',
]
],
]
]);
read about it here: http://framework.zend.com/manual/current/en/modules/zend.input-filter.file-input.html