blob: c17de600a1769b806da4a146d002838deafb13ce [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,
31 'stricton' => FALSE,
32 'failover' => array()
33 );
Derek Jones8ede1a22011-10-05 13:34:52 -050034
Andrey Andreevb94b91a2012-07-04 12:32:14 +030035Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might
36require a full DSN string to be provided. If that is the case, you
37should use the 'dsn' configuration setting, as if you're using the
38driver's underlying native PHP extension, like this::
Taufan Aditya18209332012-02-09 16:07:27 +070039
Andrey Andreevb94b91a2012-07-04 12:32:14 +030040 // PDO
Taufan Aditya18209332012-02-09 16:07:27 +070041 $db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';
42
Andrey Andreevb94b91a2012-07-04 12:32:14 +030043 // Oracle
44 $db['default']['dsn'] = '//localhost/XE';
45
46.. note:: If you do not specify a DSN string for a driver that requires it, CodeIgniter
47 will try to build it with the rest of the provided settings.
48
49.. note:: If you provide a DSN string and it is missing some valid settings (e.g. the
50 database character set), which are present in the rest of the configuration
51 fields, CodeIgniter will append them.
52
Felix Balfoort85fe96d2011-11-29 16:27:53 +010053You can also specify failovers for the situation when the main connection cannot connect for some reason.
54These failovers can be specified by setting the failover for a connection like this::
55
Felix Balfoort292a0f62011-11-29 22:29:33 +010056 $db['default']['failover'] = array(
57 array(
58 'hostname' => 'localhost1',
59 'username' => '',
60 'password' => '',
61 'database' => '',
Andrey Andreevb94b91a2012-07-04 12:32:14 +030062 'dbdriver' => 'mysqli',
Felix Balfoort292a0f62011-11-29 22:29:33 +010063 'dbprefix' => '',
64 'pconnect' => TRUE,
65 'db_debug' => TRUE,
66 'cache_on' => FALSE,
67 'cachedir' => '',
68 'char_set' => 'utf8',
69 'dbcollat' => 'utf8_general_ci',
70 'swap_pre' => '',
71 'autoinit' => TRUE,
72 'stricton' => FALSE
73 ),
74 array(
75 'hostname' => 'localhost2',
76 'username' => '',
77 'password' => '',
78 'database' => '',
Andrey Andreevb94b91a2012-07-04 12:32:14 +030079 'dbdriver' => 'mysqli',
Felix Balfoort292a0f62011-11-29 22:29:33 +010080 'dbprefix' => '',
81 'pconnect' => TRUE,
82 'db_debug' => TRUE,
83 'cache_on' => FALSE,
84 'cachedir' => '',
85 'char_set' => 'utf8',
86 'dbcollat' => 'utf8_general_ci',
87 'swap_pre' => '',
88 'autoinit' => TRUE,
89 'stricton' => FALSE
90 )
91 );
Felix Balfoort85fe96d2011-11-29 16:27:53 +010092
Felix Balfoort68f5d492011-11-29 17:03:23 +010093You can specify as many failovers as you like.
Felix Balfoort85fe96d2011-11-29 16:27:53 +010094
Derek Jones8ede1a22011-10-05 13:34:52 -050095The reason we use a multi-dimensional array rather than a more simple
96one is to permit you to optionally store multiple sets of connection
97values. If, for example, you run multiple environments (development,
98production, test, etc.) under a single installation, you can set up a
99connection group for each, then switch between groups as needed. For
100example, to set up a "test" environment you would do this::
101
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300102 $db['test'] = array(
103 'dsn' => '',
104 'hostname' => 'localhost',
105 'username' => 'root',
106 'password' => '',
107 'database' => 'database_name',
108 'dbdriver' => 'mysqli',
109 'dbprefix' => '',
110 'pconnect' => TRUE,
111 'db_debug' => TRUE,
112 'cache_on' => FALSE,
113 'cachedir' => '',
114 'char_set' => 'utf8',
115 'dbcollat' => 'utf8_general_ci',
116 'swap_pre' => '',
117 'autoinit' => TRUE,
118 'stricton' => FALSE,
119 'failover' => array()
120 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
122Then, to globally tell the system to use that group you would set this
123variable located in the config file::
124
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300125 $active_group = 'test';
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300127.. note:: The name 'test' is arbitrary. It can be anything you want. By
128 default we've used the word "default" for the primary connection,
129 but it too can be renamed to something more relevant to your project.
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000131Query Builder
Derek Jones8ede1a22011-10-05 13:34:52 -0500132-------------
133
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000134The :doc:`Query Builder Class <query_builder>` is globally enabled or
135disabled by setting the $query_builder variable in the database
Derek Jones8ede1a22011-10-05 13:34:52 -0500136configuration file to TRUE/FALSE (boolean). If you are not using the
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000137query builder class, setting it to FALSE will utilize fewer resources
Derek Jones8ede1a22011-10-05 13:34:52 -0500138when the database classes are initialized.
139
140::
141
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000142 $query_builder = TRUE;
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300144.. note:: that some CodeIgniter classes such as Sessions require Query
145 Builder to be enabled to access certain functionality.
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
147Explanation of Values:
148----------------------
149
purwandi3eed88c2011-10-07 09:47:21 +0700150====================== ==================================================================================================
151 Name Config Description
152====================== ==================================================================================================
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300153**dsn** The DSN connect string (an all-in-one configuration sequence).
154**hostname** The hostname of your database server. Often this is 'localhost'.
purwandi3eed88c2011-10-07 09:47:21 +0700155**username** The username used to connect to the database.
156**password** The password used to connect to the database.
157**database** The name of the database you want to connect to.
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300158**dbdriver** The database type. ie: mysqli, postgre, odbc, etc. Must be specified in lower case.
purwandi3eed88c2011-10-07 09:47:21 +0700159**dbprefix** An optional table prefix which will added to the table name when running :doc:
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000160 `Query Builder <query_builder>` queries. This permits multiple CodeIgniter installations
purwandi3eed88c2011-10-07 09:47:21 +0700161 to share one database.
162**pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection.
163**db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed.
164**cache_on** TRUE/FALSE (boolean) - Whether database query caching is enabled,
165 see also :doc:`Database Caching Class <caching>`.
166**cachedir** The absolute server path to your database query cache directory.
167**char_set** The character set used in communicating with the database.
168**dbcollat** The character collation used in communicating with the database
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300170 .. note:: Only used in the 'mysql' and 'mysqli' drivers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
purwandi3eed88c2011-10-07 09:47:21 +0700172**swap_pre** A default table prefix that should be swapped with dbprefix. This is useful for distributed
173 applications where you might run manually written queries, and need the prefix to still be
174 customizable by the end user.
175**autoinit** Whether or not to automatically connect to the database when the library loads. If set to false,
176 the connection will take place prior to executing the first query.
177**stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL
178 while developing an application.
purwandiabb456a2011-10-07 09:52:46 +0700179**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 +0700180 ::
Greg Akerffd24a42011-12-25 22:27:59 -0600181
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300182 $db['default']['port'] = 5432;
purwandi3eed88c2011-10-07 09:47:21 +0700183====================== ==================================================================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
Andrey Andreev2f3beb22012-03-12 16:34:15 +0200185.. note:: Depending on what database platform you are using (MySQL, PostgreSQL,
186 etc.) not all values will be needed. For example, when using SQLite you
187 will not need to supply a username or password, and the database name
188 will be the path to your database file. The information above assumes
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300189 you are using MySQL.