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