4

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?

1
  • Application data != configuration data. One belongs into the database, the other doesn't. Well, user settings are another topic. Commented Dec 14, 2010 at 0:12

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0
  1. One row per, similar to EAV.

  2. Sure, global array or object sounds fine.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.