使用資料庫表替代檔案配置檔案

2021-09-23 19:37:58 字數 1765 閱讀 2822

很多時候我們需要使用資料庫儲存配置項,由於各種原因我們無法使用配置檔案來完成,例如在乙個有很多節點集群環境中使用檔案配置檔案時非常不方便。

drop table if exists `config`;

create table if not exists `config` (

`key` varchar(50) not null,

`value` varchar(50) not null,

`operator` varchar(50) not null default 'dba',

`mtime` timestamp not null default current_timestamp on update current_timestamp,

primary key (`key`)

) engine=innodb default charset=utf8 comment='**動態配置檔案';

insert into `config` (`key`, `value`, `operator`, `mtime`) values

('cache.apc', 'on', 'dba', '2013-07-18 16:17:13'),

('cache.file.path', '/tmp', 'dba', '2013-07-18 16:17:57'),

('cache.redis', 'yes', 'dba', '2013-07-18 16:17:22'),

('payment.alipay.status', 'enabled', 'dba', '2013-07-18 16:15:15'),

('payment.alipay.url', '', 'dba', '2013-07-18 16:16:38'),

('payment.yeepay.status', 'enabled', 'dba', '2013-07-18 16:15:17'),

('payment.99bill.status', 'enabled', 'dba', '2013-07-18 16:15:10'),

('payment.zqpay.status', 'disabled', 'dba', '2013-07-18 16:15:20');

配置項key的寫法很講究

單個配置

database.host=localhost

database.user=user

database.pass=pass

多個配置

database.1.host=localhost

database.1.user=user

database.1.pass=pass

database.1.status=1

database.2.host=localhost

database.2.user=user

database.2.pass=pass

database.2.status=1

優化配置項,例如:payment.alipay.status 可以這樣優化

payment.status.alipay

payment.status.yeepay

這樣做的目的是為了更好的使用like進行查詢

select `key`,`value` from config where `key` like 'payment.status.%';

select `key`,`value` from config where `key` like 'database.?.status';

延伸閱讀:

使用資料庫表替代檔案配置檔案

使用資料庫表替代檔案配置檔案 很多時候我們需要使用資料庫儲存配置項,由於各種原因我們無法使用配置檔案來完成,例如在乙個有很多節點集群環境中使用檔案配置檔案時非常不方便。drop table if exists config create table if not exists config key ...

discuz資料庫配置檔案

系統需要修改的位置有兩處 discuz 和 uc center 路徑 wwwroot config config global.php 這個根據你 安裝的路徑而定。開啟 config global.php 檔案修改 config db 1 dbpw 原來密碼 原來密碼 修改為新的資料庫密碼。路徑 w...

MySQL資料庫配置檔案

cat etc my.cnf mysqld basedir usr local mysql datadir usr local mysql data user mysql port 3306 socket usr local mysql tmp mysql.sock 伺服器字符集 character...