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 | |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 15 | $db['default']['hostname'] = "localhost"; |
| 16 | $db['default']['username'] = "root"; |
| 17 | $db['default']['password'] = ""; |
| 18 | $db['default']['database'] = "database_name"; |
| 19 | $db['default']['dbdriver'] = "mysql"; |
| 20 | $db['default']['dbprefix'] = ""; |
| 21 | $db['default']['pconnect'] = TRUE; |
| 22 | $db['default']['db_debug'] = FALSE; |
| 23 | $db['default']['cache_on'] = FALSE; |
| 24 | $db['default']['cachedir'] = ""; |
| 25 | $db['default']['char_set'] = "utf8"; |
| 26 | $db['default']['dbcollat'] = "utf8_general_ci"; |
| 27 | $db['default']['swap_pre'] = ""; |
| 28 | $db['default']['autoinit'] = TRUE; |
| 29 | $db['default']['stricton'] = FALSE; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
Felix Balfoort | 85fe96d | 2011-11-29 16:27:53 +0100 | [diff] [blame] | 31 | You can also specify failovers for the situation when the main connection cannot connect for some reason. |
| 32 | These failovers can be specified by setting the failover for a connection like this:: |
| 33 | |
| 34 | $db['default']['failover'][0]['hostname'] = 'localhost1'; |
| 35 | $db['default']['failover'][0]['username'] = ''; |
| 36 | $db['default']['failover'][0]['password'] = ''; |
| 37 | $db['default']['failover'][0]['database'] = ''; |
| 38 | $db['default']['failover'][0]['dbdriver'] = 'mysql'; |
| 39 | $db['default']['failover'][0]['dbprefix'] = ''; |
| 40 | $db['default']['failover'][0]['pconnect'] = TRUE; |
| 41 | $db['default']['failover'][0]['db_debug'] = TRUE; |
| 42 | $db['default']['failover'][0]['cache_on'] = FALSE; |
| 43 | $db['default']['failover'][0]['cachedir'] = ''; |
| 44 | $db['default']['failover'][0]['char_set'] = 'utf8'; |
| 45 | $db['default']['failover'][0]['dbcollat'] = 'utf8_general_ci'; |
| 46 | $db['default']['failover'][0]['swap_pre'] = ''; |
| 47 | $db['default']['failover'][0]['autoinit'] = TRUE; |
| 48 | $db['default']['failover'][0]['stricton'] = FALSE; |
| 49 | $db['default']['failover'][0]['failover'] = array(); |
| 50 | |
Felix Balfoort | 68f5d49 | 2011-11-29 17:03:23 +0100 | [diff] [blame^] | 51 | You can specify as many failovers as you like. |
Felix Balfoort | 85fe96d | 2011-11-29 16:27:53 +0100 | [diff] [blame] | 52 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | The reason we use a multi-dimensional array rather than a more simple |
| 54 | one is to permit you to optionally store multiple sets of connection |
| 55 | values. If, for example, you run multiple environments (development, |
| 56 | production, test, etc.) under a single installation, you can set up a |
| 57 | connection group for each, then switch between groups as needed. For |
| 58 | example, to set up a "test" environment you would do this:: |
| 59 | |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 60 | $db['test']['hostname'] = "localhost"; |
| 61 | $db['test']['username'] = "root"; |
| 62 | $db['test']['password'] = ""; |
| 63 | $db['test']['database'] = "database_name"; |
| 64 | $db['test']['dbdriver'] = "mysql"; |
| 65 | $db['test']['dbprefix'] = ""; |
| 66 | $db['test']['pconnect'] = TRUE; |
| 67 | $db['test']['db_debug'] = FALSE; |
| 68 | $db['test']['cache_on'] = FALSE; |
| 69 | $db['test']['cachedir'] = ""; |
| 70 | $db['test']['char_set'] = "utf8"; |
| 71 | $db['test']['dbcollat'] = "utf8_general_ci"; |
| 72 | $db['test']['swap_pre'] = ""; |
| 73 | $db['test']['autoinit'] = TRUE; |
| 74 | $db['test']['stricton'] = FALSE; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
| 76 | Then, to globally tell the system to use that group you would set this |
| 77 | variable located in the config file:: |
| 78 | |
| 79 | $active_group = "test"; |
| 80 | |
| 81 | Note: The name "test" is arbitrary. It can be anything you want. By |
| 82 | default we've used the word "default" for the primary connection, but it |
| 83 | too can be renamed to something more relevant to your project. |
| 84 | |
| 85 | Active Record |
| 86 | ------------- |
| 87 | |
| 88 | The :doc:`Active Record Class <active_record>` is globally enabled or |
| 89 | disabled by setting the $active_record variable in the database |
| 90 | configuration file to TRUE/FALSE (boolean). If you are not using the |
| 91 | active record class, setting it to FALSE will utilize fewer resources |
| 92 | when the database classes are initialized. |
| 93 | |
| 94 | :: |
| 95 | |
| 96 | $active_record = TRUE; |
| 97 | |
| 98 | .. note:: that some CodeIgniter classes such as Sessions require Active |
| 99 | Records be enabled to access certain functionality. |
| 100 | |
| 101 | Explanation of Values: |
| 102 | ---------------------- |
| 103 | |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 104 | ====================== ================================================================================================== |
| 105 | Name Config Description |
| 106 | ====================== ================================================================================================== |
| 107 | **hostname** The hostname of your database server. Often this is "localhost". |
| 108 | **username** The username used to connect to the database. |
| 109 | **password** The password used to connect to the database. |
| 110 | **database** The name of the database you want to connect to. |
| 111 | **dbdriver** The database type. ie: mysql, postgres, odbc, etc. Must be specified in lower case. |
| 112 | **dbprefix** An optional table prefix which will added to the table name when running :doc: |
| 113 | `Active Record <active_record>` queries. This permits multiple CodeIgniter installations |
| 114 | to share one database. |
| 115 | **pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection. |
| 116 | **db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed. |
| 117 | **cache_on** TRUE/FALSE (boolean) - Whether database query caching is enabled, |
| 118 | see also :doc:`Database Caching Class <caching>`. |
| 119 | **cachedir** The absolute server path to your database query cache directory. |
| 120 | **char_set** The character set used in communicating with the database. |
| 121 | **dbcollat** The character collation used in communicating with the database |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 123 | .. note:: For MySQL and MySQLi databases, this setting is only used |
| 124 | as a backup if your server is running PHP < 5.2.3 or MySQL < 5.0.7 |
| 125 | (and in table creation queries made with DB Forge). There is an |
| 126 | incompatibility in PHP with mysql_real_escape_string() which can |
| 127 | make your site vulnerable to SQL injection if you are using a |
| 128 | multi-byte character set and are running versions lower than these. |
| 129 | Sites using Latin-1 or UTF-8 database character set and collation are |
| 130 | unaffected. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
purwandi | 3eed88c | 2011-10-07 09:47:21 +0700 | [diff] [blame] | 132 | **swap_pre** A default table prefix that should be swapped with dbprefix. This is useful for distributed |
| 133 | applications where you might run manually written queries, and need the prefix to still be |
| 134 | customizable by the end user. |
| 135 | **autoinit** Whether or not to automatically connect to the database when the library loads. If set to false, |
| 136 | the connection will take place prior to executing the first query. |
| 137 | **stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL |
| 138 | while developing an application. |
purwandi | abb456a | 2011-10-07 09:52:46 +0700 | [diff] [blame] | 139 | **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] | 140 | :: |
| 141 | $db['default']['port'] = 5432; |
| 142 | ====================== ================================================================================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
| 144 | .. note:: Depending on what database platform you are using (MySQL, |
| 145 | Postgres, etc.) not all values will be needed. For example, when using |
| 146 | SQLite you will not need to supply a username or password, and the |
| 147 | database name will be the path to your database file. The information |
| 148 | above assumes you are using MySQL. |