Can i use mysql with yii framework? - mysql

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!

Related

Laravel: Database SSL connection not working

I tested the ssl connection with my (Open SSL) generated certificates via the MySQL workbench app and it was working perfectly fine via ssl. However, when I try to use a ssl connection to my remote database with Laravel I get errors. On my local machine I get a 502 Bad Gateway error when visiting my laravel website and on my online test setup I get a SQLSTATE[HY000] [2002] error.
Config:
'mysql' => [
'driver' => 'mysql',
'host' => env( 'DB_HOST', '127.0.0.1' ),
'port' => env( 'DB_PORT', '3306' ),
'database' => env( 'DB_DATABASE', 'forge' ),
'username' => env( 'DB_USERNAME', 'forge' ),
'password' => env( 'DB_PASSWORD', '' ),
'unix_socket' => env( 'DB_SOCKET', '' ),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'modes' => [
//'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
'options' => [
PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/client-key.pem'),
PDO::MYSQL_ATTR_SSL_CERT => base_path('ssl/client-cert.pem'),
PDO::MYSQL_ATTR_SSL_CA => base_path('ssl/ca-cert.pem')
]
],
This is an error that is logged in my online setup in the laravel log file:
[previous exception] [object] (PDOException(code: 0): PDO::__construct(): Unable to locate peer certificate CN at /var/www/vhosts/website.com/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:38)
[stacktrace]
I'd really appreciate any hints.
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,

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'];

Does yii2 have persistent connection options?

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
]
),

How to replace environment-specific config, not cascade

I want to have a local development environment configuration for my database.php. I've created a new config director in my app and copied the database.php file into it. I'm using MongoDB and on production, I'm using a replica set with multiple servers, but locally, I'm just running a single server, so my connection info for production has more options:
Production:
...
'connections' => array(
'mongodb' => array(
'driver' => 'mongodb',
'host' => array('mongoDBA', 'mongoDBB'),
'port' => 27017,
'username' => 'myUserName',
'password' => 'myPassword',
'database' => 'theDatabase',
'options' => array('replicaSet' => 'myReplicaSet')
)
),
Local:
...
'connections' => array(
'mongodb' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'database' => 'theDatabase'
)
),
The problem is, when my local environment config loads, it merges the "connections" array. I want a way to completely replace the "mongodb" connection, so it's either one or the other, not both.
How can I accomplish this?
Create different set of configuration files:
app/config/local/database.php
app/config/production/database.php
And delete the file (or what you don't want of it):
app/config/database.php
And the you have to set your environment as
local
development
EDIT
This is how I do set my environment flawlessly, so I don't have to deal with hostnames and still don't get my local environment conflict with staging and production.
Create a .environment file in the root of your application and define your environment and add your sensitive information to it:
<?php
return array(
'APPLICATION_ENV' => 'development', /// this is where you will set your environment
'DB_HOST' => 'localhost',
'DB_DATABASE_NAME' => 'laraveldatabase',
'DB_DATABASE_USER' => 'laraveluser',
'DB_DATABASE_PASSWORD' => '!Bassw0rT',
);
Add it to your .gitignore file, so you don't risk having your passwords sent to Github or any other of your servers.
Right before $app->detectEnvironment, in the file bootstrap/start.php, load your .environment file to PHP environment:
foreach(require __DIR__.'/../.environment' as $key => $value)
{
putenv(sprintf('%s=%s', $key, $value));
}
And then you just have to use it:
$env = $app->detectEnvironment(function () {
return getenv('APPLICATION_ENV'); // your environment name is in that file!
});
And it will work everywhere, so you don't need to have separate dirs for development and production anymore:
<?php
return array(
'connections' => array(
'postgresql' => array(
'driver' => 'pgsql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_DATABASE_NAME'),
'username' => getenv('DB_DATABASE_USER'),
'password' => getenv('DB_DATABASE_PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
),
);
Note that I don't set a fallback:
return getenv('APPLICATION_ENV') ?: 'local';
Because, if I don't set the file, I want it to fail on every server I deploy my app to.