summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/database/configuration.rst
blob: 521eb6010a4753f7c9657a824c61da9d92608264 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
######################
Database Configuration
######################

CodeIgniter has a config file that lets you store your database
connection values (username, password, database name, etc.). The config
file is located at application/config/database.php. You can also set
database connection values for specific
:doc:`environments <../libraries/config>` by placing **database.php**
it the respective environment config folder.

The config settings are stored in a multi-dimensional array with this
prototype::

	$db['default'] = array(
		'dsn'	=> '',
		'hostname' => 'localhost',
		'username' => 'root',
		'password' => '',
		'database' => 'database_name',
		'dbdriver' => 'mysqli',
		'dbprefix' => '',
		'pconnect' => TRUE,
		'db_debug' => TRUE,
		'cache_on' => FALSE,
		'cachedir' => '',
		'char_set' => 'utf8',
		'dbcollat' => 'utf8_general_ci',
		'swap_pre' => '',
		'autoinit' => TRUE,
		'encrypt' => FALSE,
		'compress' => FALSE,
		'stricton' => FALSE,
		'failover' => array()
	);

Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might
require a full DSN string to be provided. If that is the case, you
should use the 'dsn' configuration setting, as if you're using the
driver's underlying native PHP extension, like this::

	// PDO
	$db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';

	// Oracle
	$db['default']['dsn'] = '//localhost/XE';

.. note:: If you do not specify a DSN string for a driver that requires it, CodeIgniter
	will try to build it with the rest of the provided settings.

.. note:: If you provide a DSN string and it is missing some valid settings (e.g. the
	database character set), which are present in the rest of the configuration
	fields, CodeIgniter will append them.

You can also specify failovers for the situation when the main connection cannot connect for some reason.
These failovers can be specified by setting the failover for a connection like this::

	$db['default']['failover'] = array(
			array(
				'hostname' => 'localhost1',
				'username' => '',
				'password' => '',
				'database' => '',
				'dbdriver' => 'mysqli',
				'dbprefix' => '',
				'pconnect' => TRUE,
				'db_debug' => TRUE,
				'cache_on' => FALSE,
				'cachedir' => '',
				'char_set' => 'utf8',
				'dbcollat' => 'utf8_general_ci',
				'swap_pre' => '',
				'autoinit' => TRUE,
				'encrypt' => FALSE,
				'compress' => FALSE,
				'stricton' => FALSE
			),
			array(
				'hostname' => 'localhost2',
				'username' => '',
				'password' => '',
				'database' => '',
				'dbdriver' => 'mysqli',
				'dbprefix' => '',
				'pconnect' => TRUE,
				'db_debug' => TRUE,
				'cache_on' => FALSE,
				'cachedir' => '',
				'char_set' => 'utf8',
				'dbcollat' => 'utf8_general_ci',
				'swap_pre' => '',
				'autoinit' => TRUE,
				'encrypt' => FALSE,
				'compress' => FALSE,
				'stricton' => FALSE
			)
		);

You can specify as many failovers as you like.

The reason we use a multi-dimensional array rather than a more simple
one is to permit you to optionally store multiple sets of connection
values. If, for example, you run multiple environments (development,
production, test, etc.) under a single installation, you can set up a
connection group for each, then switch between groups as needed. For
example, to set up a "test" environment you would do this::

	$db['test'] = array(
		'dsn'	=> '',
		'hostname' => 'localhost',
		'username' => 'root',
		'password' => '',
		'database' => 'database_name',
		'dbdriver' => 'mysqli',
		'dbprefix' => '',
		'pconnect' => TRUE,
		'db_debug' => TRUE,
		'cache_on' => FALSE,
		'cachedir' => '',
		'char_set' => 'utf8',
		'dbcollat' => 'utf8_general_ci',
		'swap_pre' => '',
		'autoinit' => TRUE,
		'compress' => FALSE,
		'encrypt' => FALSE,
		'stricton' => FALSE,
		'failover' => array()
	);

Then, to globally tell the system to use that group you would set this
variable located in the config file::

	$active_group = 'test';

.. note:: The name 'test' is arbitrary. It can be anything you want. By
	default we've used the word "default" for the primary connection,
	but it too can be renamed to something more relevant to your project.

Query Builder
-------------

The :doc:`Query Builder Class <query_builder>` is globally enabled or
disabled by setting the $query_builder variable in the database
configuration file to TRUE/FALSE (boolean). The default setting is TRUE.
If you are not using the
query builder class, setting it to FALSE will utilize fewer resources
when the database classes are initialized.

::

	$query_builder = TRUE;

.. note:: that some CodeIgniter classes such as Sessions require Query
	Builder to be enabled to access certain functionality.

Explanation of Values:
----------------------

======================  ==================================================================================================
 Name Config             Description
======================  ==================================================================================================
**dsn**			The DSN connect string (an all-in-one configuration sequence).
**hostname** 		The hostname of your database server. Often this is 'localhost'.
**username**		The username used to connect to the database.
**password**		The password used to connect to the database.
**database**		The name of the database you want to connect to.
**dbdriver**		The database type. ie: mysqli, postgre, odbc, etc. Must be specified in lower case.
**dbprefix**		An optional table prefix which will added to the table name when running
			:doc:`Query Builder <query_builder>` queries. This permits multiple CodeIgniter
			installations to share one database.
**pconnect**		TRUE/FALSE (boolean) - Whether to use a persistent connection.
**db_debug**		TRUE/FALSE (boolean) - Whether database errors should be displayed.
**cache_on**		TRUE/FALSE (boolean) - Whether database query caching is enabled,
			see also :doc:`Database Caching Class <caching>`.
**cachedir**		The absolute server path to your database query cache directory.
**char_set**		The character set used in communicating with the database.
**dbcollat**		The character collation used in communicating with the database

			.. note:: Only used in the 'mysql' and 'mysqli' drivers.

**swap_pre**		A default table prefix that should be swapped with dbprefix. This is useful for distributed
			applications where you might run manually written queries, and need the prefix to still be
			customizable by the end user.
**autoinit**		Whether or not to automatically connect to the database when the library loads. If set to false,
			you will have to manually connect via the ``$this->db->db_connect()`` method.
**schema**		The database schema, defaults to 'public'. Used by PostgreSQL and ODBC drivers.
**encrypt**		Whether or not to use an encrypted connection.
**compress**		Whether or not to use client compression (MySQL only).
**stricton**		TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL
			while developing an application.
**port**		The database port number. To use this value you have to add a line to the database config array.
			::

				$db['default']['port'] = 5432;
======================  ==================================================================================================

.. note:: Depending on what database platform you are using (MySQL, PostgreSQL,
	etc.) not all values will be needed. For example, when using SQLite you
	will not need to supply a username or password, and the database name
	will be the path to your database file. The information above assumes
	you are using MySQL.