Database Configuration
CodeIgniter has a config file that lets you store your databaseconnection values (username, password, database name, etc.). The configfile is located at app/Config/Database.php. You can also setdatabase connection values in the .env file. See below for more details.
The config settings are stored in a class property that is an array with thisprototype:
- public $default = [
- 'DSN' => '',
- 'hostname' => 'localhost',
- 'username' => 'root',
- 'password' => '',
- 'database' => 'database_name',
- 'DBDriver' => 'MySQLi',
- 'DBPrefix' => '',
- 'pConnect' => TRUE,
- 'DBDebug' => TRUE,
- 'cacheOn' => FALSE,
- 'cacheDir' => '',
- 'charset' => 'utf8',
- 'DBCollat' => 'utf8_general_ci',
- 'swapPre' => '',
- 'encrypt' => FALSE,
- 'compress' => FALSE,
- 'strictOn' => FALSE,
- 'failover' => [],
- ];
The name of the class property is the connection name, and can be usedwhile connecting to specify a group name.
Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) mightrequire a full DSN string to be provided. If that is the case, youshould use the ‘DSN’ configuration setting, as if you’re using thedriver’s underlying native PHP extension, like this:
- // PDO
- $default['DSN'] = 'pgsql:host=localhost;port=5432;dbname=database_name';
- // Oracle
- $default['DSN'] = '//localhost/XE';
Note
If you do not specify a DSN string for a driver that requires it, CodeIgniterwill try to build it with the rest of the provided settings.
Note
If you provide a DSN string and it is missing some valid settings (e.g. thedatabase character set), which are present in the rest of the configurationfields, CodeIgniter will append them.
You can also specify failovers for the situation when the main connection cannot connect for some reason.These failovers can be specified by setting the failover for a connection like this:
- $default['failover'] = [
- [
- 'hostname' => 'localhost1',
- 'username' => '',
- 'password' => '',
- 'database' => '',
- 'DBDriver' => 'MySQLi',
- 'DBPrefix' => '',
- 'pConnect' => TRUE,
- 'DBDebug' => TRUE,
- 'cacheOn' => FALSE,
- 'cacheDir' => '',
- 'charset' => 'utf8',
- 'DBCollat' => 'utf8_general_ci',
- 'swapPre' => '',
- 'encrypt' => FALSE,
- 'compress' => FALSE,
- 'strictOn' => FALSE
- ],
- [
- 'hostname' => 'localhost2',
- 'username' => '',
- 'password' => '',
- 'database' => '',
- 'DBDriver' => 'MySQLi',
- 'DBPrefix' => '',
- 'pConnect' => TRUE,
- 'DBDebug' => TRUE,
- 'cacheOn' => FALSE,
- 'cacheDir' => '',
- 'charset' => 'utf8',
- 'DBCollat' => 'utf8_general_ci',
- 'swapPre' => '',
- 'encrypt' => FALSE,
- 'compress' => FALSE,
- 'strictOn' => FALSE
- ]
- ];
You can specify as many failovers as you like.
You may optionally store multiple sets of connectionvalues. If, for example, you run multiple environments (development,production, test, etc.) under a single installation, you can set up aconnection group for each, then switch between groups as needed. Forexample, to set up a “test” environment you would do this:
- public $test = [
- 'DSN' => '',
- 'hostname' => 'localhost',
- 'username' => 'root',
- 'password' => '',
- 'database' => 'database_name',
- 'DBDriver' => 'MySQLi',
- 'DBPrefix' => '',
- 'pConnect' => TRUE,
- 'DBDebug' => TRUE,
- 'cacheOn' => FALSE,
- 'cacheDir' => '',
- 'charset' => 'utf8',
- 'DBCollat' => 'utf8_general_ci',
- 'swapPre' => '',
- 'compress' => FALSE,
- 'encrypt' => FALSE,
- 'strictOn' => FALSE,
- 'failover' => []
- );
Then, to globally tell the system to use that group you would set thisvariable located in the config file:
- $defaultGroup = 'test';
Note
The name ‘test’ is arbitrary. It can be anything you want. Bydefault we’ve used the word “default” for the primary connection,but it too can be renamed to something more relevant to your project.
You could modify the config file to detect the environment and automaticallyupdate the defaultGroup value to the correct one by adding the required logicwithin the class’ constructor:
- class Database
- {
- public $development = [...];
- public $test = [...];
- public $production = [...];
- public function __construct()
- {
- $this->defaultGroup = ENVIRONMENT;
- }
- }
Configuring With .env File
You can also save your configuration values within a .env
file with the current server’sdatabase settings. You only need to enter the values that change from what is in thedefault group’s configuration settings. The values should be name following this format, wheredefault
is the group name:
- database.default.username = 'root';
- database.default.password = '';
- database.default.database = 'ci4';
As with all other
Explanation of Values:
Name Config | Description |
---|---|
dsn | The DSN connect string (an all-in-one configuration sequence). |
hostname | The hostname of your database server. Often this is ‘localhost’. |
username | The username used to connect to the database. |
password | The password used to connect to the database. |
database | The name of the database you want to connect to. |
DBDriver | The database type. eg: MySQLi, Postgre, etc. The case must match the driver name |
DBPrefix | An optional table prefix which will added to the table name when runningQuery Builder queries. This permits multiple CodeIgniterinstallations to share one database. |
pConnect | TRUE/FALSE (boolean) - Whether to use a persistent connection. |
DBDebug | TRUE/FALSE (boolean) - Whether database errors should be displayed. |
cacheOn | TRUE/FALSE (boolean) - Whether database query caching is enabled. |
cacheDir | The absolute server path to your database query cache directory. |
charset | The character set used in communicating with the database. |
DBCollat | The character collation used in communicating with the databaseNoteOnly used in the ‘MySQLi’ driver. |
swapPre | A default table prefix that should be swapped with dbprefix. This is useful for distributedapplications where you might run manually written queries, and need the prefix to still becustomizable by the end user. |
schema | The database schema, defaults to ‘public’. Used by PostgreSQL and ODBC drivers. |
encrypt | Whether or not to use an encrypted connection.
|
compress | Whether or not to use client compression (MySQL only). |
strictOn | TRUE/FALSE (boolean) - Whether to force “Strict Mode” connections, good for ensuring strict SQLwhile developing an application. |
port | The database port number. To use this value you have to add a line to the database config array.
|
Note
Depending on what database platform you are using (MySQL, PostgreSQL,etc.) not all values will be needed. For example, when using SQLite youwill not need to supply a username or password, and the database namewill be the path to your database file. The information above assumesyou are using MySQL.