使用数据库而不是配置文件来存储配置

发布于 2024-10-07 10:22:53 字数 155 浏览 0 评论 0原文

不幸的是,搜索此类材料给我带来了很多噪音,所以我想知道这里是否有人知道对于希望学习如何使用数据库而不是文件来存储站点配置信息的人来说有一个很好的资源。

我想我感兴趣的一些点是: 1)如何存储数据。像 e107 这样的一个阵列?每个可配置的单独行? 2)如何获取配置数据。全局数组?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安稳善良 2024-10-14 10:22:53

考虑以下事项:

  • 某些配置必须存储在配置文件中,除非您对其进行硬编码,特别是如何连接到数据库
  • 数据库不擅长存储某些类型的配置数据 - 高度结构化的内容,或不可预测或可变的内容结构 - 您最终可能会得到一个没有多大意义的模式
  • 在 SCM 系统中存储配置文件要容易得多
  • 您可以在升级时简单地替换配置文件,如果需要,您可以将其回滚回去。尝试用数据库表执行此操作?即使在最好的情况下,这也更困难。

对于小型 Web 应用程序,我通常会选择配置文件。

一旦您拥有涉及网络和应用程序的基础设施,对于非 Web 服务器,将配置放在可集中访问的位置成为一个优势。这不一定是 SQL 数据库,有些人使用 LDAP 甚至 DNS。

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.

呆° 2024-10-14 10:22:53

我要在教堂里发誓,并谦虚地指出,两者都可以做到:)

您可以将配置参数存储在数据库中,然后在保存参数时生成您需要的任何配置文件。

这是一种冗余形式,但它还具有以下优点:

  1. 您现在可以将配置参数与普通查询结合起来。
  2. 对于DB中的每个参数,您可以添加一个默认值,这意味着您可以轻松地“重置”它
  3. 您可以选择更轻松地处理配置的版本(甚至跟踪更改)。
  4. 您甚至可以构建一个 Web 表单来修改配置(现在您不需要直接访问 Web 服务器)。
  5. 敏感数据在 Web 服务器文件系统上不再可见。

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.
乜一 2024-10-14 10:22:53
  1. 每行一行,类似于 EAV。

  2. 当然,全局数组或对象听起来不错。

  1. One row per, similar to EAV.

  2. Sure, global array or object sounds fine.

暮凉 2024-10-14 10:22:53

对于用户通过 Web 界面更改的设置,您的方法非常常见。

您可以查看 WordPress 在数据库中存储配置的方式。

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.

人事已非 2024-10-14 10:22:53

像这样的东西应该对你有用:

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

将你的键放入 prop 中,并将配置值放入 value 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文