Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ###################### |
| 2 | Database Configuration |
| 3 | ###################### |
| 4 | |
| 5 | CodeIgniter has a config file that lets you store your database |
| 6 | connection values (username, password, database name, etc.). The config |
| 7 | file is located at application/config/database.php. You can also set |
| 8 | database connection values for specific |
| 9 | :doc:`environments <../libraries/config>` by placing **database.php** |
Craig Johnson | b384779 | 2015-11-12 23:44:17 +0530 | [diff] [blame] | 10 | in the respective environment config folder. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 11 | |
| 12 | The config settings are stored in a multi-dimensional array with this |
| 13 | prototype:: |
| 14 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 15 | $db['default'] = array( |
| 16 | 'dsn' => '', |
| 17 | 'hostname' => 'localhost', |
| 18 | 'username' => 'root', |
| 19 | 'password' => '', |
| 20 | 'database' => 'database_name', |
| 21 | 'dbdriver' => 'mysqli', |
| 22 | 'dbprefix' => '', |
| 23 | 'pconnect' => TRUE, |
| 24 | 'db_debug' => TRUE, |
| 25 | 'cache_on' => FALSE, |
| 26 | 'cachedir' => '', |
| 27 | 'char_set' => 'utf8', |
| 28 | 'dbcollat' => 'utf8_general_ci', |
| 29 | 'swap_pre' => '', |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 30 | 'encrypt' => FALSE, |
| 31 | 'compress' => FALSE, |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 32 | 'stricton' => FALSE, |
| 33 | 'failover' => array() |
| 34 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 36 | Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might |
| 37 | require a full DSN string to be provided. If that is the case, you |
| 38 | should use the 'dsn' configuration setting, as if you're using the |
| 39 | driver's underlying native PHP extension, like this:: |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 40 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 41 | // PDO |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 42 | $db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name'; |
| 43 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 44 | // Oracle |
| 45 | $db['default']['dsn'] = '//localhost/XE'; |
| 46 | |
| 47 | .. note:: If you do not specify a DSN string for a driver that requires it, CodeIgniter |
| 48 | will try to build it with the rest of the provided settings. |
| 49 | |
| 50 | .. note:: If you provide a DSN string and it is missing some valid settings (e.g. the |
| 51 | database character set), which are present in the rest of the configuration |
| 52 | fields, CodeIgniter will append them. |
| 53 | |
Felix Balfoort | 85fe96d | 2011-11-29 16:27:53 +0100 | [diff] [blame] | 54 | You can also specify failovers for the situation when the main connection cannot connect for some reason. |
| 55 | These failovers can be specified by setting the failover for a connection like this:: |
| 56 | |
Felix Balfoort | 292a0f6 | 2011-11-29 22:29:33 +0100 | [diff] [blame] | 57 | $db['default']['failover'] = array( |
| 58 | array( |
| 59 | 'hostname' => 'localhost1', |
| 60 | 'username' => '', |
| 61 | 'password' => '', |
| 62 | 'database' => '', |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 63 | 'dbdriver' => 'mysqli', |
Felix Balfoort | 292a0f6 | 2011-11-29 22:29:33 +0100 | [diff] [blame] | 64 | 'dbprefix' => '', |
| 65 | 'pconnect' => TRUE, |
| 66 | 'db_debug' => TRUE, |
| 67 | 'cache_on' => FALSE, |
| 68 | 'cachedir' => '', |
| 69 | 'char_set' => 'utf8', |
| 70 | 'dbcollat' => 'utf8_general_ci', |
| 71 | 'swap_pre' => '', |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 72 | 'encrypt' => FALSE, |
| 73 | 'compress' => FALSE, |
Andrey Andreev | cbb70f0 | 2015-07-16 14:35:54 +0300 | [diff] [blame] | 74 | 'stricton' => FALSE |
Felix Balfoort | 292a0f6 | 2011-11-29 22:29:33 +0100 | [diff] [blame] | 75 | ), |
| 76 | array( |
| 77 | 'hostname' => 'localhost2', |
| 78 | 'username' => '', |
| 79 | 'password' => '', |
| 80 | 'database' => '', |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 81 | 'dbdriver' => 'mysqli', |
Felix Balfoort | 292a0f6 | 2011-11-29 22:29:33 +0100 | [diff] [blame] | 82 | 'dbprefix' => '', |
| 83 | 'pconnect' => TRUE, |
| 84 | 'db_debug' => TRUE, |
| 85 | 'cache_on' => FALSE, |
| 86 | 'cachedir' => '', |
| 87 | 'char_set' => 'utf8', |
| 88 | 'dbcollat' => 'utf8_general_ci', |
| 89 | 'swap_pre' => '', |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 90 | 'encrypt' => FALSE, |
| 91 | 'compress' => FALSE, |
Andrey Andreev | cbb70f0 | 2015-07-16 14:35:54 +0300 | [diff] [blame] | 92 | 'stricton' => FALSE |
Felix Balfoort | 292a0f6 | 2011-11-29 22:29:33 +0100 | [diff] [blame] | 93 | ) |
| 94 | ); |
Felix Balfoort | 85fe96d | 2011-11-29 16:27:53 +0100 | [diff] [blame] | 95 | |
Felix Balfoort | 68f5d49 | 2011-11-29 17:03:23 +0100 | [diff] [blame] | 96 | You can specify as many failovers as you like. |
Felix Balfoort | 85fe96d | 2011-11-29 16:27:53 +0100 | [diff] [blame] | 97 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | The reason we use a multi-dimensional array rather than a more simple |
| 99 | one is to permit you to optionally store multiple sets of connection |
| 100 | values. If, for example, you run multiple environments (development, |
| 101 | production, test, etc.) under a single installation, you can set up a |
| 102 | connection group for each, then switch between groups as needed. For |
| 103 | example, to set up a "test" environment you would do this:: |
| 104 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 105 | $db['test'] = array( |
| 106 | 'dsn' => '', |
| 107 | 'hostname' => 'localhost', |
| 108 | 'username' => 'root', |
| 109 | 'password' => '', |
| 110 | 'database' => 'database_name', |
| 111 | 'dbdriver' => 'mysqli', |
| 112 | 'dbprefix' => '', |
| 113 | 'pconnect' => TRUE, |
| 114 | 'db_debug' => TRUE, |
| 115 | 'cache_on' => FALSE, |
| 116 | 'cachedir' => '', |
| 117 | 'char_set' => 'utf8', |
| 118 | 'dbcollat' => 'utf8_general_ci', |
| 119 | 'swap_pre' => '', |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 120 | 'compress' => FALSE, |
| 121 | 'encrypt' => FALSE, |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 122 | 'stricton' => FALSE, |
| 123 | 'failover' => array() |
| 124 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
| 126 | Then, to globally tell the system to use that group you would set this |
| 127 | variable located in the config file:: |
| 128 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 129 | $active_group = 'test'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 131 | .. note:: The name 'test' is arbitrary. It can be anything you want. By |
| 132 | default we've used the word "default" for the primary connection, |
| 133 | but it too can be renamed to something more relevant to your project. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 134 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 135 | Query Builder |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | ------------- |
| 137 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 138 | The :doc:`Query Builder Class <query_builder>` is globally enabled or |
| 139 | disabled by setting the $query_builder variable in the database |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 140 | configuration file to TRUE/FALSE (boolean). The default setting is TRUE. |
| 141 | If you are not using the |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 142 | query builder class, setting it to FALSE will utilize fewer resources |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | when the database classes are initialized. |
| 144 | |
| 145 | :: |
| 146 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 147 | $query_builder = TRUE; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 149 | .. note:: that some CodeIgniter classes such as Sessions require Query |
| 150 | Builder to be enabled to access certain functionality. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 151 | |
| 152 | Explanation of Values: |
| 153 | ---------------------- |
| 154 | |
Andrey Andreev | cbb70f0 | 2015-07-16 14:35:54 +0300 | [diff] [blame] | 155 | ====================== =========================================================================================================== |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 156 | Name Config Description |
Andrey Andreev | cbb70f0 | 2015-07-16 14:35:54 +0300 | [diff] [blame] | 157 | ====================== =========================================================================================================== |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 158 | **dsn** The DSN connect string (an all-in-one configuration sequence). |
| 159 | **hostname** The hostname of your database server. Often this is 'localhost'. |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 160 | **username** The username used to connect to the database. |
| 161 | **password** The password used to connect to the database. |
| 162 | **database** The name of the database you want to connect to. |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 163 | **dbdriver** The database type. ie: mysqli, postgre, odbc, etc. Must be specified in lower case. |
vlakoff | 8f11e50 | 2015-01-21 23:39:36 +0100 | [diff] [blame] | 164 | **dbprefix** An optional table prefix which will added to the table name when running |
| 165 | :doc:`Query Builder <query_builder>` queries. This permits multiple CodeIgniter |
| 166 | installations to share one database. |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 167 | **pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection. |
| 168 | **db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed. |
| 169 | **cache_on** TRUE/FALSE (boolean) - Whether database query caching is enabled, |
| 170 | see also :doc:`Database Caching Class <caching>`. |
| 171 | **cachedir** The absolute server path to your database query cache directory. |
| 172 | **char_set** The character set used in communicating with the database. |
| 173 | **dbcollat** The character collation used in communicating with the database |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 175 | .. note:: Only used in the 'mysql' and 'mysqli' drivers. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 176 | |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 177 | **swap_pre** A default table prefix that should be swapped with dbprefix. This is useful for distributed |
| 178 | applications where you might run manually written queries, and need the prefix to still be |
| 179 | customizable by the end user. |
Andrey Andreev | 485a348 | 2012-10-27 03:22:43 +0300 | [diff] [blame] | 180 | **schema** The database schema, defaults to 'public'. Used by PostgreSQL and ODBC drivers. |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 181 | **encrypt** Whether or not to use an encrypted connection. |
Andrey Andreev | cbb70f0 | 2015-07-16 14:35:54 +0300 | [diff] [blame] | 182 | |
| 183 | - 'mysql' (deprecated), 'sqlsrv' and 'pdo/sqlsrv' drivers accept TRUE/FALSE |
| 184 | - 'mysqli' and 'pdo/mysql' drivers accept an array with the following options: |
| 185 | |
| 186 | - 'ssl_key' - Path to the private key file |
| 187 | - 'ssl_cert' - Path to the public key certificate file |
| 188 | - 'ssl_ca' - Path to the certificate authority file |
Andrey Andreev | 23aa166 | 2017-10-09 10:57:57 +0300 | [diff] [blame] | 189 | - 'ssl_capath' - Path to a directory containing trusted CA certificates in PEM format |
Andrey Andreev | cbb70f0 | 2015-07-16 14:35:54 +0300 | [diff] [blame] | 190 | - 'ssl_cipher' - List of *allowed* ciphers to be used for the encryption, separated by colons (':') |
Andrey Andreev | cfc9e77 | 2015-07-16 16:17:27 +0300 | [diff] [blame] | 191 | - 'ssl_verify' - TRUE/FALSE; Whether to verify the server certificate or not ('mysqli' only) |
Andrey Andreev | cbb70f0 | 2015-07-16 14:35:54 +0300 | [diff] [blame] | 192 | |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 193 | **compress** Whether or not to use client compression (MySQL only). |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 194 | **stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL |
| 195 | while developing an application. |
purwandi | abb456a | 2011-10-07 09:52:46 +0700 | [diff] [blame] | 196 | **port** The database port number. To use this value you have to add a line to the database config array. |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 197 | :: |
Michiel Vugteveen | cdb481b | 2012-08-21 10:11:16 +0200 | [diff] [blame] | 198 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 199 | $db['default']['port'] = 5432; |
Tim Nolte | c09ab9d | 2015-06-08 10:40:26 -0400 | [diff] [blame] | 200 | |
Andrey Andreev | cbb70f0 | 2015-07-16 14:35:54 +0300 | [diff] [blame] | 201 | ====================== =========================================================================================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
Andrey Andreev | 2f3beb2 | 2012-03-12 16:34:15 +0200 | [diff] [blame] | 203 | .. note:: Depending on what database platform you are using (MySQL, PostgreSQL, |
| 204 | etc.) not all values will be needed. For example, when using SQLite you |
| 205 | will not need to supply a username or password, and the database name |
| 206 | will be the path to your database file. The information above assumes |
Tim Nolte | c09ab9d | 2015-06-08 10:40:26 -0400 | [diff] [blame] | 207 | you are using MySQL. |