mod_dbd
Overview
mod_dbd module manages SQL database connections. It provides upon-request database
connections to modules requiring SQL database functions, and takes care of managing databases
with optimal efficiency and scalability.
Quick start
Mod_dbd provides the interface to mod_authn_dbd, which checks user credentials from SQL tables.
The configuration below demonstrates mod_dbd and mod_authn_dbd in action:
DBDriver mssql
DBDParams "Data Source=db_server;Initial Catalog=users_db;Persist Security Info=True;User ID=sa;Password=your_password"
# core authentication and mod_auth_basic configuration
# for mod_authn_dbd
AuthType Basic
AuthName "My Server"
AuthBasicProvider dbd
# core authorization configuration
Require valid-user
# mod_authn_dbd SQL query to authenticate a user
AuthDBDUserPWQuery "SELECT password FROM users_table WHERE user = @USERNAME"
Also mod_dbd allows to define SQL request that can provide rewrite-maps for rewrite rules from SQL database.
DBDriver mssql
DBDParams "Data Source=server;Initial Catalog=database;User ID=user;Password=password"
DBDPrepareSQL "select OriginalURL from seo_mapping where `SEO_URL`=@KEY" seo_map_select
RewriteEngine On
RewriteMap map_dbd dbd:seo_map_select
RewriteCond ${map_dbd:$1|NOT_FOUND} (.*)
RewriteCond %1 !NOT_FOUND
RewriteRule (.+) %1 [L]
Related articles and topics
Connection pooling
mod_dbd supports connection pooling. This feature allows managing
connections in a very efficient way by uniting them into connection pools and reusing already created connections many times. It means that connections aren't closed and reopened upon every request, instead they are stored
inside a special place—connection pool. mod_dbd provides special directives for connection pooling tweaking, namely DBDExptime, DBDMax, DBDMin and DBDPersist.
Directives
DBDExptime
DBDExptime directive sets the time to keep idle connections alive. The value 0 means that
pooled connections never time out. The directive isn't supported by Oracle database driver.
Syntax
DBDExptime time-in-seconds
Default
DBDExptime 300
DBDKeep
Currently not supported.
Specifies maximum sustained number of connections.
Syntax
DBDKeep number
Default
DBDKeep 2
DBDMax
DBDMax directive sets the maximum number of connections per a connections pool.
Syntax
DBDMax number
Default
DBDMax 10
DBDMin
DBDMin directive sets the minimum number of connections per a connections pool.
Syntax
DBDMin number
Default
DBDMin 1
DBDParams
DBDParams directive sets the parameters for the database connection depending on the
database driver.
Syntax
DBDParams param1=value1[,param2=value2]
DBDPersist
Tells whether to use persistent connections. If DBDPersist directive is set to Off, connection pooling is disabled.
A new database connection is opened when requested by a client, and closed immediately on release.
This option is for debugging and low-usage servers.
The default is to enable and use a pool of persistent connections.
Syntax
DBDPersist On|Off
DBDPrepareSQL
DBDPrepareSQL directive prepares an SQL statement and assigns it a label.
It is useful for modules such as authentication that repeatedly use a single SQL statement
and where it's more efficient to prepare the statement at startup rather than every time it is used.
Syntax
DBDPrepareSQL "SQL statement" label
Example
DBDPrepareSQL "SELECT password FROM users_table WHERE user = john" password_query
DBDriver
DBDriver directive specifies the driver which should be used to create and maintain database connections.
Syntax
DBDriver name [reference connection object type]
Description
- name may take the following values:
- mssql—handles connections to Microsoft SQL Server 7.0 or higher. Example:
DBDriver mssql
DBDParams "Data Source=server;Initial Catalog=database_name;User ID=user;Password=password"
- mysql—handles connections to MySQL. This driver requres MySQL Connector for .NET to be installed. You may find the connector here: http://dev.mysql.com/downloads/connector/net/. It's
also required to specify a reference to the connector's library. To do that you should open C:\Windows\assembly and find MySql.Data.dll assembly. Please open its properties and configure mod_dbd using them as follows:
DBDriver mysql "MySql.Data, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
DBDParams "Data Source=server;Initial Catalog=database_name;User ID=user;Password=password"
- oracle—handles connections to Oracle database. Example:
DBDriver oracle
DBDParams "Data Source=server;Initial Catalog=database_name;User ID=user;Password=password"
- odbc—handles connections to various databases through ODBC interface. Please make sure you have installed an ODBC connector for the database you want to use. Here is an example for MySQL:
DBDriver odbc
DBDParams "Driver={MySQL ODBC 5.1 Driver};Server=server;Database=database;User=root;Password=password;"
- oledb—handles connections to various databases through OLEDB interface. Please make sure you have installed an OLEDB connector for the database you want to use. Here is an example for MySQL:
DBDriver oledb
DBDParams "Driver={Some OleDb provider};Server=server;Database=database;User=user;Password=password;"
Connection string is taken from DBDParams directive as you can see in the examples above.