James Smith
James Smith

Reputation: 501

Using a database to store configuration rather than a configuration file

Unfortunately searching for this kind of material produces a lot of noise for me so I was wondering if anyone here knew of a good resource for anyone wishing to learn how to use a database to store site configuration info rather than a file.

I guess some points I'm interested in are: 1) How to store the data. One array like e107? Separate row for each configurable? 2) How to get the configuration data. Global array?

Upvotes: 4

Views: 7158

Answers (5)

Ronnis
Ronnis

Reputation: 12843

I'm gonna swear in church and humbly point out that one can do both :)

You can store the configuration parameters in a database and then produce whatever configuration file you need when the parameters are saved.

This is a form of redundancy, but it also gives the following advantages:

  1. You can now combine configuration parameters with ordinary queries.
  2. For each parameter in DB, you can add a default value, which means you can easily "reset" it
  3. You can choose to version handle the configuration more easily (even track changes).
  4. You can even build a web form to modify the configuration (and now you don't need direct access to web server).
  5. Sensitive data is no longer visible on web server filesystem.

Upvotes: 2

Joshua Martell
Joshua Martell

Reputation: 7212

Something like this should work for you:

CREATE TABLE `config` (
  `prop` varchar(100) DEFAULT NULL,
  `value` mediumtext,
  UNIQUE KEY `lookup` (`lookup`)
) ENGINE=InnoDB

Put your keys into prop, and the config values into value.

Upvotes: 0

Nathan
Nathan

Reputation: 3920

Your approach is very common for settings that users change through a web interface.

You might look at the way WordPress stores configuration in the database.

Upvotes: 0

MarkR
MarkR

Reputation: 63616

Consider the following:

  • SOME configuration will have to be stored in a config file unless you hard code it, notably, how to connect to the database
  • Databases are bad at storing some types of config data - highly structured stuff, or things with unpredictable or variable structure - and you may end up with a schema which doesn't make a lot of sense
  • It is much easier to store config files in a SCM system
  • You can simply replace a config file upon upgrade, you can roll it back if you need to go back. Try doing this with database tables? Even in a best-case scenario, it's harder.

I would generally go for a config file, for small web applications.

Once you get as far as having an infrastructure involving web & non-web servers, putting the config in a centrally-accessible place becomes an advantage though. This doesn't have to be a SQL database, some people use LDAP or even DNS.

Upvotes: 9

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799450

  1. One row per, similar to EAV.

  2. Sure, global array or object sounds fine.

Upvotes: 0

Related Questions