Laravel Redis Error - Cannot use SAVE with a cluster of connections - php

I am new in Redis. Is anyone know, why these error showing?
I am using Laravel.
Cannot use SAVE with a cluster of connections
Cannot use FLUSHDB with a cluster of connections
$redis = Redis::connection();
$redis->set('name', 'Harry');
echo $name = $redis->get('name'); //It is working fine
$redis->save();

www/projectname/app/config/database.php
Find,
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
)
);
If ‘cluster’ is true, make it false. May be your issue will solve.

Related

Laravel 5.5 How to change DB_HOST on request

Ok, my system connects to remote databases on which I consistently change depending on what data I want to access.
The setup is multiple identical structure databases resides on different IP addresses, so I always update the .env DB_HOST line every time I want to access different database.
Now I'm planning to create a view on my system that will accepts an IP address and set it as the current database to use.
Referencing this answer on Laracasts:
Set two database connections in your config (one for your unchanging main connection, the other one for your custom dynamic connection):
'main' => array(
'driver' => 'mysql',
'host' => 'hostname',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
'dynamic' => array(
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'prefix' => '',
),
Then change your database connection parameters for the second connection as needed:
Config::set('database.connections.dynamic.host', $newHost);
Config::set('database.connections.dynamic.username', $newUsername);
Config::set('database.connections.dynamic.password', $newPassword);
Config::set('database.connections.dynamic.database', $newDatabase);
Config::set('database.default', 'dynamic');
And reconnect the database:
DB::reconnect('mysql');

Fusio api cache configuration

Googled and tried,but failed.
How to configure Fusio to use redis.?
//'psx_cache_factory' => null,
This doesnt work:
'psx_cache_factory' => 'redis'
This also doesnt work:
'psx_cache_factory' => 'redis://127.0.0.1:6379',
Answered by developer.
Github
'psx_cache_factory' => function($config, $namespace){
$client = new \Predis\Client([
'scheme' => 'tcp',
'host' => '10.0.0.1',
'port' => 6379,
]);
return new \Doctrine\Common\Cache\PredisCache($client);
},

Laravel + Redis Cache via SSL?

I am trying to connect to Redis with predis 1.1 and SSL, using information https://github.com/nrk/predis, where in the example the following configuration is used:
// Named array of connection parameters:
$client = new Predis\Client([
'scheme' => 'tls',
'ssl' => ['cafile' => 'private.pem', 'verify_peer' => true],
]);
My Laravel configuration looks like below:
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'options' => [
'cluster' => 'redis',
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
'scheme' => 'tls',
],
],
Unfortunately I am getting the following error:
ConnectionException in AbstractConnection.php line 155:
Error while reading line from the server. [tcp://MY_REDIS_SERVER_URL:6380]
Suggestions are appreciated :)
I was able to get it to work!
You need to move 'scheme' from 'options' to 'default':
My working config:
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
'default' => [
'scheme' => 'tls',
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'options' => [
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
],
],
Note: I had also removed the 'cluster' option from 'options', but I don't suspect this to be the make-or-break with this problem.
In my final-final config, I changed it to: 'scheme' => env('REDIS_SCHEME', 'tcp'), and then defined REDIS_SCHEME=tls in my env file instead.
Tested with AWS ElastiCache with TLS enabled.
Edit:
The above config only works with single-node redis. If you happen to enable clustering and TLS then you'll need a different config entirely.
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
// Note! for single redis nodes, the default is defined here.
// keeping it here for clusters will actually prevent the cluster config
// from being used, it'll assume single node only.
//'default' => [
// ...
//],
// #pro-tip, you can use the Cluster config even for single instances!
'clusters' => [
'default' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
],
'options' => [ // Clustering specific options
'cluster' => 'redis', // This tells Redis Client lib to follow redirects (from cluster)
]
],
'options' => [
'parameters' => [ // Parameters provide defaults for the Connection Factory
'password' => env('REDIS_PASSWORD', null), // Redirects need PW for the other nodes
'scheme' => env('REDIS_SCHEME', 'tcp'), // Redirects also must match scheme
],
]
]
Explaining the above:
'client' => 'predis': This specifies the PHP Library Redis driver to use (predis).
'cluster' => 'redis': This tells Predis to assume server-side clustering. Which just means "follow redirects" (e.g. -MOVED responses). When running with a cluster, a node will respond with a -MOVED to the node that you must ask for a specific key.
If you don't have this enabled with Redis Clusters, Laravel will throw a -MOVED exception 1/n times, n being the number of nodes in Redis cluster (it'll get lucky and ask the right node every once in awhile)
'clusters' => [...] : Specifies a list of nodes, but setting just a 'default' and pointing it to the AWS 'Configuration endpoint' will let it find any/all other nodes dynamically (recommended for Elasticache, because you don't know when nodes are comin' or goin').
'options': For Laravel, can be specified at the top-level, cluster-level, and node option. (they get combined in Illuminate before being passed off to Predis)
'parameters': These 'override' the default connection settings/assumptions that Predis uses for new connections. Since we set them explicitly for the 'default' connection, these aren't used. But for a cluster setup, they are critical. A 'master' node may send back a redirect (-MOVED) and unless the parameters are set for password and scheme it'll assume defaults, and that new connection to the new node will fail.
Thank you CenterOrbit!!
I can confirm the first solution does allow Laravel to connect to a Redis server over TLS. Tested with Redis 3.2.6 on AWS ElastiCache with TLS, configured as single node and single shard.
I can also confirm the second solution does allow Laravel to connect to a Redis Cluster over TLS. Tested with Redis 3.2.6 on AWS ElastiCache with TLS, configured with "Cluster Mode Enabled", 1 shard, 1 replica per shard.
I was receiving the following error when I first tried to implement the cluster solution:
Error: Unsupported operand types
I missed the additional set of array brackets when I moved the "default" settings into the "clusters" array.
INCORRECT
'clusters' => [
'default' => [
'scheme' ...
]
]
CORRECT
'clusters' => [
'default' => [
[
'scheme' ...
]
]
]
I hope this saves someone else a bit of troubleshooting time.
The accepted solution by CenterOrbit worked for me, as I was using AWS I had to add tls:// in my .env
Laravel
tls://username:password#URL:PORT?database=0
Try it. It will work

Is It Possible to use Laravel 4 without a Default Database

I'm creating an application that reads information from a number of different databases, but doesn't actually have its own database, as there is no information being written anywhere.
Basically, a user selects a record and a type, and the application will generate a .pdf file based on their choices. I have multiple connections defined in app/config/database.php but I don't want to connect to any of them by default. Is there a way to tell Laravel not to connect to a database? I've tried a few things (all in app/config/database.php), first being:
'default' => NULL,
// and
//'default' => '',
Which both return:
Undefined index: driver
I've also tried:
'default' => 'none',
'connections' => array(
'none' => array(
'driver' => '',
'host' => '',
...
),
),
which in turn returns:
Unsupported driver []
Unsupported host[]
...
And lastly setting 'default' => '', which returns:
Database [] not configured.
I've found ways to use Laravel's models without a database connection, but not actually Laravel itself.
Edit:
Setting the connection to an existing mysql connection and not selecting a database "works":
'default' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '****',
'password' => '****',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
but I'm not sure if that's the right way to do it. It feels like a work-around and not an actual solution.
I would do the following:
'driver' => 'sqlite',
and for the sqlite connection settings
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]
That way, it'll use an in-memory database and the database will cease to exist when the database connection is closed, and since you won't be opening a connection, you'll never have a local database open.
You could also remove the database service provider from your app.php config file:
'Illuminate\Cookie\CookieServiceProvider',
//'Illuminate\Database\DatabaseServiceProvider',
'Illuminate\Encryption\EncryptionServiceProvider',
and the facades for Fluent and Eloquent in the same file:
'Crypt' => 'Illuminate\Support\Facades\Crypt',
//'DB' => 'Illuminate\Support\Facades\DB',
//'Eloquent' => 'Illuminate\Database\Eloquent\Model',
'Event' => 'Illuminate\Support\Facades\Event',
which will prevent you from being able to connect to local databases and not even boot the sqlite database connection you've just set up.
What you need to do define the connection in the app/config/database.php file and specify the connection when running a query with laravel's DB::connection('connection_name').
More information here --> http://laravel.com/docs/4.2/database#accessing-connections
Simply define your db driver as 'sqlite' in the database config.
'default' => 'sqlite',
As long as you haven't changed anything in the default sqlite config section, all should work. You don't necessarily have to use the database, but it will alleviate those errors you received.
In case you have changed the default sqlite config for whatever reason, you can follow the below steps to configure the sqlite db.
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
Also, make sure this 'production.sqlite' file exists if it doesn't already. You can do this easily from the command line:
touch app/database/production.sqlite

Laravel 4: Oracle Connection is failing suddenly

I have a Laravel 4 website on my localhost and i was using Oracle database.
I tested it perfectly for weeks, no problem occured.
Suddenly when it started giving the error: ORA-01017: invalid username/password; logon denied
In my app/config/database.php i have,
'oracle' => array(
'driver' => 'pdo-via-oci8',
'host' => 'localhost',
'port' => '1521',
'database' => 'orcl', //xe Service ID
'username' => 'system', //schema
'password' => '123',
'charset' => '',
'prefix' => '',
),
My SQL PLUS and Sql Developer is running fine with the same username-password.
Please help .
Try ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON = FALSE;

Categories