blob: 34cefffbdb601df10007aad04617d4120eaa1871 [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,
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030031 'encrypt' => FALSE,
32 'compress' => FALSE,
Andrey Andreevb94b91a2012-07-04 12:32:14 +030033 'stricton' => FALSE,
34 'failover' => array()
35 );
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Andrey Andreevb94b91a2012-07-04 12:32:14 +030037Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might
38require a full DSN string to be provided. If that is the case, you
39should use the 'dsn' configuration setting, as if you're using the
40driver's underlying native PHP extension, like this::
Taufan Aditya18209332012-02-09 16:07:27 +070041
Andrey Andreevb94b91a2012-07-04 12:32:14 +030042 // PDO
Taufan Aditya18209332012-02-09 16:07:27 +070043 $db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';
44
Andrey Andreevb94b91a2012-07-04 12:32:14 +030045 // 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 Balfoort85fe96d2011-11-29 16:27:53 +010055You can also specify failovers for the situation when the main connection cannot connect for some reason.
56These failovers can be specified by setting the failover for a connection like this::
57
Felix Balfoort292a0f62011-11-29 22:29:33 +010058 $db['default']['failover'] = array(
59 array(
60 'hostname' => 'localhost1',
61 'username' => '',
62 'password' => '',
63 'database' => '',
Andrey Andreevb94b91a2012-07-04 12:32:14 +030064 'dbdriver' => 'mysqli',
Felix Balfoort292a0f62011-11-29 22:29:33 +010065 '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 Andreev2f8bf9b2012-10-12 20:37:52 +030074 'encrypt' => FALSE,
75 'compress' => FALSE,
Felix Balfoort292a0f62011-11-29 22:29:33 +010076 'stricton' => FALSE
77 ),
78 array(
79 'hostname' => 'localhost2',
80 'username' => '',
81 'password' => '',
82 'database' => '',
Andrey Andreevb94b91a2012-07-04 12:32:14 +030083 'dbdriver' => 'mysqli',
Felix Balfoort292a0f62011-11-29 22:29:33 +010084 '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 Andreev2f8bf9b2012-10-12 20:37:52 +030093 'encrypt' => FALSE,
94 'compress' => FALSE,
Felix Balfoort292a0f62011-11-29 22:29:33 +010095 'stricton' => FALSE
96 )
97 );
Felix Balfoort85fe96d2011-11-29 16:27:53 +010098
Felix Balfoort68f5d492011-11-29 17:03:23 +010099You can specify as many failovers as you like.
Felix Balfoort85fe96d2011-11-29 16:27:53 +0100100
Derek Jones8ede1a22011-10-05 13:34:52 -0500101The reason we use a multi-dimensional array rather than a more simple
102one is to permit you to optionally store multiple sets of connection
103values. If, for example, you run multiple environments (development,
104production, test, etc.) under a single installation, you can set up a
105connection group for each, then switch between groups as needed. For
106example, to set up a "test" environment you would do this::
107
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300108 $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 Andreev2f8bf9b2012-10-12 20:37:52 +0300124 'compress' => FALSE,
125 'encrypt' => FALSE,
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300126 'stricton' => FALSE,
127 'failover' => array()
128 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130Then, to globally tell the system to use that group you would set this
131variable located in the config file::
132
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300133 $active_group = 'test';
Derek Jones8ede1a22011-10-05 13:34:52 -0500134
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300135.. 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 Jones8ede1a22011-10-05 13:34:52 -0500138
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000139Query Builder
Derek Jones8ede1a22011-10-05 13:34:52 -0500140-------------
141
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000142The :doc:`Query Builder Class <query_builder>` is globally enabled or
143disabled by setting the $query_builder variable in the database
Derek Jones8ede1a22011-10-05 13:34:52 -0500144configuration file to TRUE/FALSE (boolean). If you are not using the
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000145query builder class, setting it to FALSE will utilize fewer resources
Derek Jones8ede1a22011-10-05 13:34:52 -0500146when the database classes are initialized.
147
148::
149
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000150 $query_builder = TRUE;
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300152.. note:: that some CodeIgniter classes such as Sessions require Query
153 Builder to be enabled to access certain functionality.
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
155Explanation of Values:
156----------------------
157
purwandi3eed88c2011-10-07 09:47:21 +0700158====================== ==================================================================================================
159 Name Config Description
160====================== ==================================================================================================
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300161**dsn** The DSN connect string (an all-in-one configuration sequence).
162**hostname** The hostname of your database server. Often this is 'localhost'.
purwandi3eed88c2011-10-07 09:47:21 +0700163**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 Andreevb94b91a2012-07-04 12:32:14 +0300166**dbdriver** The database type. ie: mysqli, postgre, odbc, etc. Must be specified in lower case.
purwandi3eed88c2011-10-07 09:47:21 +0700167**dbprefix** An optional table prefix which will added to the table name when running :doc:
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000168 `Query Builder <query_builder>` queries. This permits multiple CodeIgniter installations
purwandi3eed88c2011-10-07 09:47:21 +0700169 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 Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300178 .. note:: Only used in the 'mysql' and 'mysqli' drivers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
purwandi3eed88c2011-10-07 09:47:21 +0700180**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 Andreev485a3482012-10-27 03:22:43 +0300185**schema** The database schema, defaults to 'public'. Used by PostgreSQL and ODBC drivers.
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300186**encrypt** Whether or not to use an encrypted connection.
187**compress** Whether or not to use client compression (MySQL only).
purwandi3eed88c2011-10-07 09:47:21 +0700188**stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL
189 while developing an application.
purwandiabb456a2011-10-07 09:52:46 +0700190**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 +0700191 ::
Michiel Vugteveencdb481b2012-08-21 10:11:16 +0200192
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300193 $db['default']['port'] = 5432;
purwandi3eed88c2011-10-07 09:47:21 +0700194====================== ==================================================================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
Andrey Andreev2f3beb22012-03-12 16:34:15 +0200196.. 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 Andreevb94b91a2012-07-04 12:32:14 +0300200 you are using MySQL.