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