blob: 636b5b5b2cba14237fc289e2783b11199f12c67c [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
Andrey Andreevb94b91a2012-07-04 12:32:14 +030015 $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,
Michiel Vugteveencdb481b2012-08-21 10:11:16 +020031 'compress' => TRUE,
Andrey Andreevb94b91a2012-07-04 12:32:14 +030032 'stricton' => FALSE,
33 'failover' => array()
34 );
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Andrey Andreevb94b91a2012-07-04 12:32:14 +030036Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might
37require a full DSN string to be provided. If that is the case, you
38should use the 'dsn' configuration setting, as if you're using the
39driver's underlying native PHP extension, like this::
Taufan Aditya18209332012-02-09 16:07:27 +070040
Andrey Andreevb94b91a2012-07-04 12:32:14 +030041 // PDO
Taufan Aditya18209332012-02-09 16:07:27 +070042 $db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';
43
Andrey Andreevb94b91a2012-07-04 12:32:14 +030044 // 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 Balfoort85fe96d2011-11-29 16:27:53 +010054You can also specify failovers for the situation when the main connection cannot connect for some reason.
55These failovers can be specified by setting the failover for a connection like this::
56
Felix Balfoort292a0f62011-11-29 22:29:33 +010057 $db['default']['failover'] = array(
58 array(
59 'hostname' => 'localhost1',
60 'username' => '',
61 'password' => '',
62 'database' => '',
Andrey Andreevb94b91a2012-07-04 12:32:14 +030063 'dbdriver' => 'mysqli',
Felix Balfoort292a0f62011-11-29 22:29:33 +010064 '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' => '',
72 'autoinit' => TRUE,
Michiel Vugteveencdb481b2012-08-21 10:11:16 +020073 'compress' => TRUE,
Felix Balfoort292a0f62011-11-29 22:29:33 +010074 'stricton' => FALSE
75 ),
76 array(
77 'hostname' => 'localhost2',
78 'username' => '',
79 'password' => '',
80 'database' => '',
Andrey Andreevb94b91a2012-07-04 12:32:14 +030081 'dbdriver' => 'mysqli',
Felix Balfoort292a0f62011-11-29 22:29:33 +010082 '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' => '',
90 'autoinit' => TRUE,
Michiel Vugteveencdb481b2012-08-21 10:11:16 +020091 'compress' => TRUE,
Felix Balfoort292a0f62011-11-29 22:29:33 +010092 'stricton' => FALSE
93 )
94 );
Felix Balfoort85fe96d2011-11-29 16:27:53 +010095
Felix Balfoort68f5d492011-11-29 17:03:23 +010096You can specify as many failovers as you like.
Felix Balfoort85fe96d2011-11-29 16:27:53 +010097
Derek Jones8ede1a22011-10-05 13:34:52 -050098The reason we use a multi-dimensional array rather than a more simple
99one is to permit you to optionally store multiple sets of connection
100values. If, for example, you run multiple environments (development,
101production, test, etc.) under a single installation, you can set up a
102connection group for each, then switch between groups as needed. For
103example, to set up a "test" environment you would do this::
104
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300105 $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' => '',
120 'autoinit' => TRUE,
Michiel Vugteveencdb481b2012-08-21 10:11:16 +0200121 'compress' => TRUE,
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300122 'stricton' => FALSE,
123 'failover' => array()
124 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
126Then, to globally tell the system to use that group you would set this
127variable located in the config file::
128
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300129 $active_group = 'test';
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300131.. 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 Jones8ede1a22011-10-05 13:34:52 -0500134
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000135Query Builder
Derek Jones8ede1a22011-10-05 13:34:52 -0500136-------------
137
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000138The :doc:`Query Builder Class <query_builder>` is globally enabled or
139disabled by setting the $query_builder variable in the database
Derek Jones8ede1a22011-10-05 13:34:52 -0500140configuration file to TRUE/FALSE (boolean). If you are not using the
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000141query builder class, setting it to FALSE will utilize fewer resources
Derek Jones8ede1a22011-10-05 13:34:52 -0500142when the database classes are initialized.
143
144::
145
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000146 $query_builder = TRUE;
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300148.. note:: that some CodeIgniter classes such as Sessions require Query
149 Builder to be enabled to access certain functionality.
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
151Explanation of Values:
152----------------------
153
purwandi3eed88c2011-10-07 09:47:21 +0700154====================== ==================================================================================================
155 Name Config Description
156====================== ==================================================================================================
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300157**dsn** The DSN connect string (an all-in-one configuration sequence).
158**hostname** The hostname of your database server. Often this is 'localhost'.
purwandi3eed88c2011-10-07 09:47:21 +0700159**username** The username used to connect to the database.
160**password** The password used to connect to the database.
161**database** The name of the database you want to connect to.
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300162**dbdriver** The database type. ie: mysqli, postgre, odbc, etc. Must be specified in lower case.
purwandi3eed88c2011-10-07 09:47:21 +0700163**dbprefix** An optional table prefix which will added to the table name when running :doc:
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000164 `Query Builder <query_builder>` queries. This permits multiple CodeIgniter installations
purwandi3eed88c2011-10-07 09:47:21 +0700165 to share one database.
166**pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection.
167**db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed.
168**cache_on** TRUE/FALSE (boolean) - Whether database query caching is enabled,
169 see also :doc:`Database Caching Class <caching>`.
170**cachedir** The absolute server path to your database query cache directory.
171**char_set** The character set used in communicating with the database.
172**dbcollat** The character collation used in communicating with the database
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300174 .. note:: Only used in the 'mysql' and 'mysqli' drivers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
purwandi3eed88c2011-10-07 09:47:21 +0700176**swap_pre** A default table prefix that should be swapped with dbprefix. This is useful for distributed
177 applications where you might run manually written queries, and need the prefix to still be
178 customizable by the end user.
179**autoinit** Whether or not to automatically connect to the database when the library loads. If set to false,
180 the connection will take place prior to executing the first query.
Michiel Vugteveencdb481b2012-08-21 10:11:16 +0200181**compress** Whether or not to use client compression for MySQL or MySQLi.
purwandi3eed88c2011-10-07 09:47:21 +0700182**stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL
183 while developing an application.
purwandiabb456a2011-10-07 09:52:46 +0700184**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 +0700185 ::
Michiel Vugteveencdb481b2012-08-21 10:11:16 +0200186
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300187 $db['default']['port'] = 5432;
purwandi3eed88c2011-10-07 09:47:21 +0700188====================== ==================================================================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
Andrey Andreev2f3beb22012-03-12 16:34:15 +0200190.. note:: Depending on what database platform you are using (MySQL, PostgreSQL,
191 etc.) not all values will be needed. For example, when using SQLite you
192 will not need to supply a username or password, and the database name
193 will be the path to your database file. The information above assumes
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300194 you are using MySQL.