dsn-the-data-source-name.txt 3.38 KB
Newer Older
jepso's avatar
jepso committed
1 2
In order to connect to a database through Doctrine, you have to create a valid DSN - data source name. 

3
Doctrine supports both PEAR DB/MDB2 like data source names as well as PDO style data source names. The following section deals with PEAR like data source names. If you need more info about the PDO-style data source names see [[php PDO->__construct()]].
jepso's avatar
jepso committed
4 5 6

The DSN consists in the following parts:

7 8 9 10 11 12 13 14 15 16
||~ DSN part   ||~ Description ||
||  phptype    ||  Database backend used in PHP (i.e. mysql , pgsql etc.) ||
||  dbsyntax   ||  Database used with regards to SQL syntax etc. ||
||  protocol   ||  Communication protocol to use ( i.e. tcp, unix etc.) ||
||  hostspec   ||  Host specification (hostname[:port]) ||
||  database   ||  Database to use on the DBMS server ||
||  username   ||  User name for login ||
||  password   ||  Password for login ||
||  proto_opts ||  Maybe used with protocol ||
||  option     ||  Additional connection options in URI query string format. Options are separated by ampersand (&). The Following table shows a non complete list of options: ||
jepso's avatar
jepso committed
17 18 19 20 21 22 23 24 25


**List of options**

||~ Name     ||~ Description ||
||  charset  ||  Some backends support setting the client charset.||
||  new_link ||  Some RDBMS do not create new connections when connecting to the same host multiple times. This option will attempt to force a new connection. ||

The DSN can either be provided as an associative array or as a string. The string format of the supplied DSN is in its fullest form:
26 27

<code>phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value</code>
jepso's avatar
jepso committed
28 29 30

Most variations are allowed:

31
<code>
jepso's avatar
jepso committed
32 33 34 35 36 37 38 39 40 41
phptype://username:password@protocol+hostspec:110//usr/db_file.db
phptype://username:password@hostspec/database
phptype://username:password@hostspec
phptype://username@hostspec
phptype://hostspec/database
phptype://hostspec
phptype:///database
phptype:///database?option=value&anotheroption=anothervalue
phptype(dbsyntax)
phptype
42
</code>
jepso's avatar
jepso committed
43

44
The currently supported database backends are: 
jepso's avatar
jepso committed
45

46
||~ Driver name ||~ Supported databases ||
lsmith's avatar
lsmith committed
47 48 49
||  firbird     ||  Firebird ||
||  informix    ||  Informix ||
||  mssql       ||  Microsoft SQL Server ||
50
||  mysql       ||  MySQL ||
lsmith's avatar
lsmith committed
51
||  oracle      ||  Oracle    ||
52
||  pgsql       ||  PostgreSQL  ||
lsmith's avatar
lsmith committed
53
||  sqlite      ||  SQLite ||
jepso's avatar
jepso committed
54

55 56 57 58 59
A second DSN format supported is

<code>
phptype(syntax)://user:pass@protocol(proto_opts)/database
</code>
60

jepso's avatar
jepso committed
61
If your database, option values, username or password contain characters used to delineate DSN parts, you can escape them via URI hex encodings:  
62

63 64 65 66 67 68 69 70 71 72
||~ Character ||~ Hex Code ||
|| :          ||  %3a      ||
|| /          ||  %2f      || 
|| @          ||  %40      || 
|| +          ||  %2b      || 
|| (          ||  %28      || 
|| )          ||  %29      || 
|| ?          ||  %3f      || 
|| =          ||  %3d      || 
|| &          ||  %26      || 
jepso's avatar
jepso committed
73 74 75 76 77

Warning 
Please note, that some features may be not supported by all database backends. 
 

78 79
+++ Examples

jepso's avatar
jepso committed
80 81
**Example 1.** Connect to database through a socket

82
<code>
jepso's avatar
jepso committed
83
mysql://user@unix(/path/to/socket)/pear
84
</code>
jepso's avatar
jepso committed
85 86 87

**Example 2.** Connect to database on a non standard port

88
<code>
jepso's avatar
jepso committed
89
pgsql://user:pass@tcp(localhost:5555)/pear
90
</code>
jepso's avatar
jepso committed
91 92 93

**Example 3.** Connect to SQLite on a Unix machine using options

94
<code>
jepso's avatar
jepso committed
95
sqlite:////full/unix/path/to/file.db?mode=0666
96
</code>
jepso's avatar
jepso committed
97 98 99

**Example 4.** Connect to SQLite on a Windows machine using options

100
<code>
jepso's avatar
jepso committed
101
sqlite:///c:/full/windows/path/to/file.db?mode=0666
102
</code>