'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', 'redis'),
'password' => env('REDIS_PASSWORD', 'secret'),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('REDIS_HOST', 'redis'),
'password' => env('REDIS_PASSWORD', 'secret'),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
Can anyone explain the database parameter in config/database.php file , I am clueless regarding this parameter as it is not stated in the docs .
According to another website , to set up session to use the redis driver , need to add another redis database and this parameter is set to 1 but it points to the same redis instance. I'm quite confused .
Thank you
EDIT
Based on more digging through redis docs I've come to a conclusion . Please do correct me if i'm wrong .
The database parameter indicates that to place in which redis database . According to the docs , Redis has by default 16 databases.
CONFIG GET databases
1) "databases"
2) "16"
And by placing the parameter database , we are indicating which database to use , for example place all the session keys to database 1 instead of database 0 which we can access by using the
SELECT db_number E.g SELECT 1
Please let me know if my conclusion is correct or wrong . Thank you =)
A redis instance has multiple databases as you stated.
The database parameter tells redis which database to use inside the instance.
The instance is defined in "host".
You don't need to select database in code level with "select" since you have declared the database in config.
If you omit the "database" param, then the default "0" database will be used.
If you want to use multiple databases (e.g cache to one db, sessions to another) then you need to create multiple connections in your config.
Related
I have two web Applications. I will login in to one Web Application and will navigate to another by links or redirection from the first Application. Lastly after completing some steps in Application two, I will be redirected to Application one. How can I implement this?
Create a new database called sessions.
Configure a connection profile for the session database secondary to your apps primary databases for both apps.
And then they should be syncing up in storing the data for sessions, being able to share them etc...
config/database.php
'app_main_database' => [
'driver' => env('DB_CONNECTION'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
'sessions_database' => [
'driver' => env('DB_CONNECTION_SESSION'),
'host' => env('DB_HOST_SESSION'),
'port' => env('DB_PORT_SESSION'),
'database' => env('DB_DATABASE_SESSION'),
'username' => env('DB_USERNAME_SESSION'),
'password' => env('DB_PASSWORD_SESSION'),
],
Configure session.connection to the name for your session driver
config/session.php
<?php
use Illuminate\Support\Str;
return [
'driver' => env('SESSION_DRIVER', 'database'),
'connection' => env('SESSION_CONNECTION', 'sessions_database'),
Your question starts with "Laravel - How do..." which leads me to believe both of your applications are using Laravel. You said they are both on the same server and same domain so you could simply expand the default PHP Session cookie from the SLD (Second Level Domain) scope of yourdomain.com to be a cross subdomain cookie: *.yourdomain.com and then set the same application key and your applications will magically start using the and sharing the data. Just make sure that you did the right work in sharing the user database because Laravel default Auth will take the User ID column and log the user into the same ID.
Make sure the environment variables in the .env for both installations have the same identical settings:
#APP_KEY is used to encrypt the session data so must be the same
APP_KEY=base64:'YOUR KEY '
#SESSION_COOKIE must be the same, this is normally not set and defaults to APP_NAME+"_session' which is a problem if your apps have different names
SESSION_COOKIE=laravel_session
#The SESSION_DOMAIN defaults to null which causes the CORS scope to limit to the subdomain level, in my testing must start with leading dot.
SESSION_DOMAIN=.rootdomain.com
i need implement mongodb and have a question;
at queue.php we have
'failed' => [
'database' => env('DB_CONNECTION', 'mysql'),
'table' => 'failed_jobs',
],
but I need to put MongoDB too at the same time, would have problems if duplicate 'failed' position?
Exist another way to do this?
You should only use one database to store your failed jobs.
If you use laravel-mongodb you can use MongoDB to store your failed jobs. Here is the documentation for that.
This would be a sample configuration for using MongoDB to save failed jobs:
'failed' => [
'driver' => 'mongodb',
// You can also specify your jobs specific database created on config/database.php
'database' => 'mongodb-job',
'table' => 'failed_jobs',
],
Using laravel 6.8, php7.4. I'm using redis sessions, inside database.php it looks like this:
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
And then in .env:
REDIS_DB=0
Everything seems to be working fine with this setup, I can see session stored in redis and it matches \Session::getId(). Problem is when I update REDIS_DB param in .env to something else, example REDIS_DB=1, there is no session stored in redis, i.e. I can get the session id using \Session::getId() but it's not present when I do print_r(Redis::keys('*')
Any help or hint would be highly appreciated.
Default redis and session may use different redis, or different database even in the same redis. When you set database 1 to session connection, it will always read, write from database 1. But since your redis's default database connection is set to 0, you can't get your session keys from by just calling Redis::keys('*') since the data is on the two different databases.
There are two ways to solve it;
Setting to database name while querying;
Redis::select(1);
return Redis::keys('*');
Updating the default redis config by setting REDIS_DB to 1.
I have MongoDB Active Records (models) and I'm wondering if it's possible to use Redis to automatically set/get/delete the models from Redis's storage.
For example, if I was to run:
MyModel::find()->where(["id" => 1])->one();
is there a way to make Redis store the result and return it the next time I run that same code?
Also, if I was to update the model with id = 1, I'd expect Redis to invalidate the cache.
Is all that possible?
It doesn't matter which DB to use. It is about how to implement them. Yii have those two components to set in the config file:
db: a database connection to be used where needed like an Active Record class to represent a model or Query Builders or ...
cache: designed to cache things from HTML pages and HTTP requests to database queries related data.
The good thing about MongoDB and Redis is that both can be used as a database connection or as a cache component. You may have those configs for example:
'components' => [
'db' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
],
],
Here, while MongoDB is set as your default database, Redis is only used as a cache component and because all cache components have the same base class yii\caching\Cache they only support those APIs. Which should be fine if you are using it for caching proposes only.
Check the Yii2 Caching Guide to see all what you can do with a cache component. A quick example of what you are trying to do may be seen within #Blizz answer here where he did set an SQL query as a dependency to check if cached data should be used or invalidated instead.
In case you need to use the Redis database for more than just caching then you may have those configs instead:
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => 'redis' // id of the connection application component
],
],
Here we defined 2 databases and selected one of them to be also used as a cache component. It should work the exact same way except that you can also use the Redis database inside your app as a Redis ActiveRecord or a Redis ActiveQuery class. You will just need to set which DB to be used inside each model class as it is done in this example.
I am in the process of migrating a cakephp 3.0 database from mysql to postgress. I used this tool for the database migration and it worked beautifully. After that I changed the config file as shown below.
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'username' => 'postgres',
'password' => 'mypass',
'database' => 'cake_bookmarks',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
],
The root folder in localhost also shows "successfully connected to database". However when i run my application, it shows an error:
Cannot describe mytable. It has 0 columns. Cake\Database\Exception
I can't make sure if this is because of not connecting to the database (which i think is unlikely as the root page shows as connected) or cakephp being unable to use my database.
If so, how can I fix the issue. I am quite new to cakephp too, just confguring and doing basic stuff.
Try the following (test after each step):
Check if the table is present in the database
Check if the expected columns are defined into the table
Clear the Cake cache (if is FileCache is enough to delete files under tmp/cache/persistent tmp/cache/models and tmp/cache/views
Check the permissions of the specific user on the database cake_bookmarks (maybe via phppgadmin)
Hope to help!