yii,migrate project to another server - php

I get a project from some provider and I try to migrate the site to another server and all lock fine only I can't make the connection to db.
The original code is this:
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=******',
'username' => 'root',
'password' => '********',
'charset' => 'utf8',
];
and the code then I modify is this
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost:3306;dbname=*******',
'username' => 'evoprom_user',
'password' => '********',
'charset' => 'utf8',
];
Do you have some idea?

Related

Laravel 4.2 Session Database Driver Read / Write Host

I configured the way I separate the database to read and write.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
But now, I want to change the database driver in the session and I want to be able to read and write operations to a single host of the session value.
How can I only run on a single host in the session process without disturbing the above structure?
I think I found a solution.
We are adding a new connection into the first database.php connections.
'session' => array(
'host' => "HOST_NAME",
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
then we give the name of the connection a little bit before we install the connection value in session.php file.
'connection' => "session",
Thats it.

Multi-tenant SaaS built in Yii2

I am working in a multi-tenant software (SaaS) built with yii2 on the Advanced Template, but I am not having the desired result about the tenants database connection.
I am trying to set the Database Connection as next in my config file for the frontend:
$defaultAdminDB = [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=untitled',
'username' => 'postgres',
'password' => 'myPass',
'charset' => 'utf8',
];
$config = [
'components' => [
'db' => function(){
if (Yii::$app->session->get('login', false)){
return [
'class' => 'yii\db\Connection',
'dsn' => Yii::$app->session->get('client_connection.dns'),
'username' => Yii::$app->session->get('client_connection.username'),
'password' => Yii::$app->session->get('client_connection.password'),
'charset' => 'utf8',
];
}
return [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=untitled',
'username' => 'postgres',
'password' => 'myPass',
'charset' => 'utf8',
];
},
'dbAdmin' => $defaultAdminDB
]
];
Then I have a two steps log in, where the first asks for the login (tenant id) and the next provides the user and password. On the first controller I do the next with:
$account = \frontend\models\AdminAccounts::findOne(['login'=>$this->login]);
if (!$account){
$this->addError('login', Yii::t('app', 'Account data not found.'));
return false;
}
$dns = sprintf('pgsql:host=%s;dbname=%s', $account->getAttribute('db_host'), $account->getAttribute('db'));
Yii::$app->session->set('login', $this->login);
Yii::$app->session->set('client_connection.dns', $dns);
Yii::$app->session->set('client_connection.username', $account->getAttribute('db_user'));
Yii::$app->session->set('client_connection.password', $account->getAttribute('db_pass'));
I get successfully the account (tenant) data and store it in session, but I guess my error is on instantiating the yii\db\Connection on the model's getDb() method.
Hope can help me.
Regards!
If you are changing the db connection info of an existing db connection then you should close the existing connection (Yii::$app->db->close()), change the connection info, and then open the new connection (Yii::$app->db->open()).
You can try
Yii::$app->db = new \yii\db\Connection([
'dsn' => Yii::$app->session->get('client_connection.dsn'),
'username' => Yii::$app->session->get('client_connection.username'),
'password' => Yii::$app->session->get('client_connection.password'),
]);

Yii 2.0 : 2 database connection

I read this question yii 2.0 multiple database connection, and use the answer of #Ali MasudianPour.
I follow the first step:
First you need to configure your databases like below:
return [
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
]; ?>
But in my configuration in my db it gives me this error:
The configuration for the "db" component must contain a "class"
element.
This is because db component is main and required and you simply omitted its declaration.
Rename db1 and db2 for example to db and db1 accordingly:
return [
'components' => [
// Main connection
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
// Another connection
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
]; ?>
Update:
Note that for the basic application db is configured in separate file config/db.php and then required in main config config/web.php like so:
'db' => require(__DIR__ . '/db.php'),
So you can configure main connection in db.php and add additional below as db1.
Simply you just have to create separate file for each database .
config/db ->
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
To access another db create file in config named db1.php add add another db configuration.
config/db1 ->
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
In config->web.php add
'db' => require(__DIR__ . '/db.php'),
'db1' => require(__DIR__ . '/db1.php'),
To access the Model:
public static function getDb() {
return Yii::$app->db1;
}

laravel Undefined variable: host

My problem is that I get the following error in MySqlConnector.php.
Undefined variable: host error
I am using two different connections for both read/write. I merged them in config/database.php file depending on environment that system uses. Here is the mysql connection codes.
<?php
switch($_SERVER['LARAVEL_ENV']){
case 'production':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'beta':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'development':
$connections = array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
}
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => $connections,
'migrations' => 'migrations',
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
All I wanted to do is using a different host for reading and another one for writing. When I use one connection in localhost, I get no error. But in multiple connections, I get the error. What is the reason for the error?
Ok I found the problem. It was because of laravel version. I updated laravel 4.0 to 4.1 and problem was solved.
just for the record of your environment implementation.
You have a build in laravel the environmen detection and loading of specific conf files.
you can read here: http://laravel.com/docs/configuration#environment-configuration
but the general idea is to define your environments in the :
$env = $app->detectEnvironment(array(
'local' => array('your-local-machine-name'),
'production' => array('your--prod-machine-name'),
));
and than you go to config folder and create inside it a folder foreach environment you have.
create a production folder, and than each file you want to override simply copy paste it into the folder and edit the things you want to override.
implement this, and than check your errors back here again

laravel database connection returns undefined index error

I am developing a project using laravel 4 framework. In my database.php file I get the following error:
Undefined index: driver
And my connection is as following:
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'mysql2' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
);
I am also using environments in order to set different mysql connections. What is wrong with the code?
In my case it was because I deleted
'default' => 'mysql',
by mistake from app/config/database.php.
Moving the 'driver' key up a level should fix the issue.
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'driver' => 'mysql'
),
Most of the other params that are shared can me moved as well
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
),
'write' => array(
'host' => 'localhost',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
This happens to me because I deleted
'default' =>env('DB_CONNECTION', 'mysql'),
From app/config/database.php. It's necesary have a default connection
Go to root directory .env
This values are taken first.
If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Character extends Model {
protected $connection = 'my_db_connection_name_here';
}
This would be on the of the connections defined in config/database.php.
I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.

Categories