blob: a9bf7dcb6eb427a24ef1b726c6bd30688b35ba0d [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**
Craig Johnsonb3847792015-11-12 23:44:17 +053010in the respective environment config folder.
Derek Jones8ede1a22011-10-05 13:34:52 -050011
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' => '',
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030030 'encrypt' => FALSE,
31 'compress' => FALSE,
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' => '',
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030072 'encrypt' => FALSE,
73 'compress' => FALSE,
Andrey Andreevcbb70f02015-07-16 14:35:54 +030074 'stricton' => FALSE
Felix Balfoort292a0f62011-11-29 22:29:33 +010075 ),
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' => '',
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030090 'encrypt' => FALSE,
91 'compress' => FALSE,
Andrey Andreevcbb70f02015-07-16 14:35:54 +030092 'stricton' => FALSE
Felix Balfoort292a0f62011-11-29 22:29:33 +010093 )
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' => '',
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300120 'compress' => FALSE,
121 'encrypt' => FALSE,
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
James L Parry42a7df62014-11-25 12:06:49 -0800140configuration file to TRUE/FALSE (boolean). The default setting is TRUE.
141If you are not using the
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000142query builder class, setting it to FALSE will utilize fewer resources
Derek Jones8ede1a22011-10-05 13:34:52 -0500143when the database classes are initialized.
144
145::
146
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000147 $query_builder = TRUE;
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300149.. note:: that some CodeIgniter classes such as Sessions require Query
150 Builder to be enabled to access certain functionality.
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
152Explanation of Values:
153----------------------
154
Andrey Andreevcbb70f02015-07-16 14:35:54 +0300155====================== ===========================================================================================================
purwandi3eed88c2011-10-07 09:47:21 +0700156 Name Config Description
Andrey Andreevcbb70f02015-07-16 14:35:54 +0300157====================== ===========================================================================================================
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300158**dsn** The DSN connect string (an all-in-one configuration sequence).
159**hostname** The hostname of your database server. Often this is 'localhost'.
purwandi3eed88c2011-10-07 09:47:21 +0700160**username** The username used to connect to the database.
161**password** The password used to connect to the database.
162**database** The name of the database you want to connect to.
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300163**dbdriver** The database type. ie: mysqli, postgre, odbc, etc. Must be specified in lower case.
vlakoff8f11e502015-01-21 23:39:36 +0100164**dbprefix** An optional table prefix which will added to the table name when running
165 :doc:`Query Builder <query_builder>` queries. This permits multiple CodeIgniter
166 installations to share one database.
purwandi3eed88c2011-10-07 09:47:21 +0700167**pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection.
168**db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed.
169**cache_on** TRUE/FALSE (boolean) - Whether database query caching is enabled,
170 see also :doc:`Database Caching Class <caching>`.
171**cachedir** The absolute server path to your database query cache directory.
172**char_set** The character set used in communicating with the database.
173**dbcollat** The character collation used in communicating with the database
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300175 .. note:: Only used in the 'mysql' and 'mysqli' drivers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
purwandi3eed88c2011-10-07 09:47:21 +0700177**swap_pre** A default table prefix that should be swapped with dbprefix. This is useful for distributed
178 applications where you might run manually written queries, and need the prefix to still be
179 customizable by the end user.
Andrey Andreev485a3482012-10-27 03:22:43 +0300180**schema** The database schema, defaults to 'public'. Used by PostgreSQL and ODBC drivers.
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300181**encrypt** Whether or not to use an encrypted connection.
Andrey Andreevcbb70f02015-07-16 14:35:54 +0300182
183 - 'mysql' (deprecated), 'sqlsrv' and 'pdo/sqlsrv' drivers accept TRUE/FALSE
184 - 'mysqli' and 'pdo/mysql' drivers accept an array with the following options:
185
186 - 'ssl_key' - Path to the private key file
187 - 'ssl_cert' - Path to the public key certificate file
188 - 'ssl_ca' - Path to the certificate authority file
Andrey Andreev23aa1662017-10-09 10:57:57 +0300189 - 'ssl_capath' - Path to a directory containing trusted CA certificates in PEM format
Andrey Andreevcbb70f02015-07-16 14:35:54 +0300190 - 'ssl_cipher' - List of *allowed* ciphers to be used for the encryption, separated by colons (':')
Andrey Andreevcfc9e772015-07-16 16:17:27 +0300191 - 'ssl_verify' - TRUE/FALSE; Whether to verify the server certificate or not ('mysqli' only)
Andrey Andreevcbb70f02015-07-16 14:35:54 +0300192
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300193**compress** Whether or not to use client compression (MySQL only).
purwandi3eed88c2011-10-07 09:47:21 +0700194**stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL
195 while developing an application.
purwandiabb456a2011-10-07 09:52:46 +0700196**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 +0700197 ::
Michiel Vugteveencdb481b2012-08-21 10:11:16 +0200198
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300199 $db['default']['port'] = 5432;
Tim Noltec09ab9d2015-06-08 10:40:26 -0400200
Andrey Andreevcbb70f02015-07-16 14:35:54 +0300201====================== ===========================================================================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Andrey Andreev2f3beb22012-03-12 16:34:15 +0200203.. note:: Depending on what database platform you are using (MySQL, PostgreSQL,
204 etc.) not all values will be needed. For example, when using SQLite you
205 will not need to supply a username or password, and the database name
206 will be the path to your database file. The information above assumes
Tim Noltec09ab9d2015-06-08 10:40:26 -0400207 you are using MySQL.