Web Development

Yii2: Connection string for different Database Server

Yii2 supports different database server types and each one have its own way to connect to database. To connect with any database we need to prepare DSN for connection.

In this quick tip, we are going to see how DSN should be created for different Database servers.

[gads]

If you are running the basic app of Yii2 then you need to make these changes in [code]db.php[/code] file under config directory and in case of advance application, it should be done in [code]main.php[/code] under config directory.

[cc lang=”php”]
[
‘class’ => ‘yii\db\Connection’,
‘dsn’ => ‘mysql:host=localhost;dbname=reportsa_macromoney’, // MySQL, MariaDB
//’dsn’ => ‘sqlite:/path/to/database/file’, // SQLite
//’dsn’ => ‘pgsql:host=localhost;port=5432;dbname=mydatabase’, // PostgreSQL
//’dsn’ => ‘cubrid:dbname=demodb;host=localhost;port=33000′, // CUBRID
//’dsn’ => ‘sqlsrv:Server=localhost;Database=mydatabase’, // MS SQL Server, sqlsrv driver
//’dsn’ => ‘dblib:host=localhost;dbname=mydatabase’, // MS SQL Server, dblib driver
//’dsn’ => ‘mssql:host=localhost;dbname=mydatabase’, // MS SQL Server, mssql driver
//’dsn’ => ‘oci:dbname=//localhost:1521/mydatabase’, // Oracle
‘username’ => ‘DB User Name’,
‘password’ => ‘Password for selected user’,
‘charset’ => ‘utf8’,
]
];
];

?>
[/cc]

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *