blob: 3f3bae336a2903f06be1515c80b960fd755e3ab8 [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
Taufan Aditya18209332012-02-09 16:07:27 +070031If you use PDO as your dbdriver, you can specify the full DSN string describe a connection to the database like this::
32
33 $db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';
34
Felix Balfoort85fe96d2011-11-29 16:27:53 +010035You can also specify failovers for the situation when the main connection cannot connect for some reason.
36These failovers can be specified by setting the failover for a connection like this::
37
Felix Balfoort292a0f62011-11-29 22:29:33 +010038 $db['default']['failover'] = array(
39 array(
40 'hostname' => 'localhost1',
41 'username' => '',
42 'password' => '',
43 'database' => '',
44 'dbdriver' => 'mysql',
45 'dbprefix' => '',
46 'pconnect' => TRUE,
47 'db_debug' => TRUE,
48 'cache_on' => FALSE,
49 'cachedir' => '',
50 'char_set' => 'utf8',
51 'dbcollat' => 'utf8_general_ci',
52 'swap_pre' => '',
53 'autoinit' => TRUE,
54 'stricton' => FALSE
55 ),
56 array(
57 'hostname' => 'localhost2',
58 'username' => '',
59 'password' => '',
60 'database' => '',
61 'dbdriver' => 'mysql',
62 'dbprefix' => '',
63 'pconnect' => TRUE,
64 'db_debug' => TRUE,
65 'cache_on' => FALSE,
66 'cachedir' => '',
67 'char_set' => 'utf8',
68 'dbcollat' => 'utf8_general_ci',
69 'swap_pre' => '',
70 'autoinit' => TRUE,
71 'stricton' => FALSE
72 )
73 );
Felix Balfoort85fe96d2011-11-29 16:27:53 +010074
Felix Balfoort68f5d492011-11-29 17:03:23 +010075You can specify as many failovers as you like.
Felix Balfoort85fe96d2011-11-29 16:27:53 +010076
Derek Jones8ede1a22011-10-05 13:34:52 -050077The reason we use a multi-dimensional array rather than a more simple
78one is to permit you to optionally store multiple sets of connection
79values. If, for example, you run multiple environments (development,
80production, test, etc.) under a single installation, you can set up a
81connection group for each, then switch between groups as needed. For
82example, to set up a "test" environment you would do this::
83
purwandi3eed88c2011-10-07 09:47:21 +070084 $db['test']['hostname'] = "localhost";
85 $db['test']['username'] = "root";
86 $db['test']['password'] = "";
87 $db['test']['database'] = "database_name";
88 $db['test']['dbdriver'] = "mysql";
89 $db['test']['dbprefix'] = "";
90 $db['test']['pconnect'] = TRUE;
91 $db['test']['db_debug'] = FALSE;
92 $db['test']['cache_on'] = FALSE;
93 $db['test']['cachedir'] = "";
94 $db['test']['char_set'] = "utf8";
95 $db['test']['dbcollat'] = "utf8_general_ci";
96 $db['test']['swap_pre'] = "";
97 $db['test']['autoinit'] = TRUE;
98 $db['test']['stricton'] = FALSE;
Derek Jones8ede1a22011-10-05 13:34:52 -050099
100Then, to globally tell the system to use that group you would set this
101variable located in the config file::
102
103 $active_group = "test";
104
105Note: The name "test" is arbitrary. It can be anything you want. By
106default we've used the word "default" for the primary connection, but it
107too can be renamed to something more relevant to your project.
108
109Active Record
110-------------
111
112The :doc:`Active Record Class <active_record>` is globally enabled or
113disabled by setting the $active_record variable in the database
114configuration file to TRUE/FALSE (boolean). If you are not using the
115active record class, setting it to FALSE will utilize fewer resources
116when the database classes are initialized.
117
118::
119
120 $active_record = TRUE;
121
122.. note:: that some CodeIgniter classes such as Sessions require Active
123 Records be enabled to access certain functionality.
124
125Explanation of Values:
126----------------------
127
purwandi3eed88c2011-10-07 09:47:21 +0700128====================== ==================================================================================================
129 Name Config Description
130====================== ==================================================================================================
131**hostname** The hostname of your database server. Often this is "localhost".
132**username** The username used to connect to the database.
133**password** The password used to connect to the database.
134**database** The name of the database you want to connect to.
Andrey Andreev2f3beb22012-03-12 16:34:15 +0200135**dbdriver** The database type. ie: mysql, postgre, odbc, etc. Must be specified in lower case.
purwandi3eed88c2011-10-07 09:47:21 +0700136**dbprefix** An optional table prefix which will added to the table name when running :doc:
137 `Active Record <active_record>` queries. This permits multiple CodeIgniter installations
138 to share one database.
139**pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection.
140**db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed.
141**cache_on** TRUE/FALSE (boolean) - Whether database query caching is enabled,
142 see also :doc:`Database Caching Class <caching>`.
143**cachedir** The absolute server path to your database query cache directory.
144**char_set** The character set used in communicating with the database.
145**dbcollat** The character collation used in communicating with the database
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
purwandi3eed88c2011-10-07 09:47:21 +0700147 .. note:: For MySQL and MySQLi databases, this setting is only used
148 as a backup if your server is running PHP < 5.2.3 or MySQL < 5.0.7
149 (and in table creation queries made with DB Forge). There is an
150 incompatibility in PHP with mysql_real_escape_string() which can
151 make your site vulnerable to SQL injection if you are using a
152 multi-byte character set and are running versions lower than these.
153 Sites using Latin-1 or UTF-8 database character set and collation are
154 unaffected.
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
purwandi3eed88c2011-10-07 09:47:21 +0700156**swap_pre** A default table prefix that should be swapped with dbprefix. This is useful for distributed
157 applications where you might run manually written queries, and need the prefix to still be
158 customizable by the end user.
159**autoinit** Whether or not to automatically connect to the database when the library loads. If set to false,
160 the connection will take place prior to executing the first query.
161**stricton** TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL
162 while developing an application.
purwandiabb456a2011-10-07 09:52:46 +0700163**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 +0700164 ::
Greg Akerffd24a42011-12-25 22:27:59 -0600165
purwandi3eed88c2011-10-07 09:47:21 +0700166 $db['default']['port'] = 5432;
167====================== ==================================================================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev2f3beb22012-03-12 16:34:15 +0200169.. note:: Depending on what database platform you are using (MySQL, PostgreSQL,
170 etc.) not all values will be needed. For example, when using SQLite you
171 will not need to supply a username or password, and the database name
172 will be the path to your database file. The information above assumes
173 you are using MySQL.