Does yii2 have persistent connection options? - yii2

I can't find documentation about Yii2 persistent connection.
I have problems with Yii2 behavior. It always opens the connection and closes it after executing the query. I think creating a persistent connection is the answer to my problems.
How to do that?

See the following github issue:
'db' => array(
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'root',
'password' => '',
'tablePrefix' => '',
'charset' => 'utf8',
'attributes'=>[
PDO::ATTR_PERSISTENT => true
]
),

Related

Yii2 Read Write splitting couldn't connect slave server in master slave configuration

Have done Master Slave configuration as per official Yii2 documentation. Below is actual configuration look like,
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=1.1.1.1;dbname=master_db',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'enableSchemaCache' => true,
'schemaCacheDuration' => 10,
'schemaCache' => 'cache',
'slaveConfig' => [
'username' => 'slave_user',
'password' => 'slave_password',
'charset' => 'utf8',
'attributes' => [
// use a smaller connection timeout
PDO::ATTR_TIMEOUT => 10,
],
'enableSchemaCache' => true,
'schemaCacheDuration' => 10,
'schemaCache' => 'cache',
],
'slaves' => [
['dsn' => 'mysql:host=2.2.2.2;dbname=slave_db']
],
],
It always connect master database even if slave server is up and reachable.
Surprisingly replacing current master config with slave one works, moreover if try to connect slave database from command line it get connected in a moment but unable to achieve same with above configuration.
Wondering if there is any parameters missing in configuration or any other way to get things working like ideal read write splitting?
issue was resolved by adding connection class in the slaveConfig,
'class' => 'yii\db\Connection'

How can I use an existing database with the advanced template of Yii2?

I installed the yii2 advanced template, and I'm now at migration point where I should create a new database and migrate the app to it.
but I already have a database full of data and I want to use it with yii2 without modifying anything
Open the file
common/config/main.php
and add the parameters for your database connection
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
],
],
];

Master Slave Configuration in Laravel 5.5

How to configure Laravel 5.5 with master slave MySQL replication ?
I want to make write operations and read operations in master and slave respectively .
Optional: Is there any way to do connection pooling and max / min no of open connections in ideal conditions. ?
Just change your config/database.php file to include read (slave) and write (master) hosts like so like the Laravel docs suggest:
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'sticky' => true,
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],

How to Get values for DB configuration from some database Tables in CakePHP ?

I am making an application that will be installed at multiple clients , and DB configuration for every client will be different , as i will be using multiple different databases LIKE Oracle and MySql.
One database will be common in all , i have made a table in same where i will save the db config details , now how to pick that data from the table at database.php .
Can't find anything bit confused .`class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'hrportal_imp',
'prefix' => '',
//'encoding' => 'utf8',
);
//want to fetch data from x table from Default datasource.
public $ora = array(
'datasource' => 'Database/Oracle',
'persistent' => false,
'host' => '<IP i get from above db>',
'port' => '1521',
'login' => '<Data i get from above db>',
'password' => '<Data i get from above db>',
'database' => '<IP i get from above db>:1521/orcl',
'prefix' => '',
'sid' => 'orcl'
);
}`
I did it by Placing the same in Beforefilter of AppController .
App::import('Model', 'ConnectionManager');
ConnectionManager::create('ora',
$config = array('datasource' => 'Database/Oracle',
'persistent' => false,
'host' => 'dynamic Host',
'port' => '1521',
'login' => 'HCM',
'password' => 'hdhd',
'database' => 'dynamic host:1521/dhdh',
'prefix' => '',
'sid' => 'orcl')
);
by default, cakephp will use "default" configurations that you can write within your $default variable. you can also change your database connection as per requirements too. you can check your current selected database on your controller too.
App::import('Model', 'ConnectionManager');
$ds = ConnectionManager::getDataSource('default');
echo $ds->config['database'];

Can i use mysql with yii framework?

I have xampp installed which includes mysql. Can i use this mysql with yii framework?
If yes how can i do it with phpmyadmin. Pls help. Am a newbie..
I have seen many people using sqlite with yii in many tutorials..
ok what you need to do is open /protected/config/main.php, and look for the line like this :
),
'db'=>array( // as you can see this db is currently in use
'connectionString' => 'sqlite:protected/data/blog.db',
'tablePrefix' => 'tbl_',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
*/
just comment the first connection to sqlite data base and uncomment the second part that uses mysql database :
),
/*'db'=>array(
'connectionString' => 'sqlite:protected/data/blog.db',
'tablePrefix' => 'tbl_',
),*/
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
that's all!