简介
因为数据库用的RDS并且异地
打开会慢不少
所以目前采用了数据库主从热备
即使用
主 write
从 read
从在本地,主在rds
找了一下插件, 发现ludicrousdb目前比较好用的.
github地址 https://github.com/stuttter/ludicrousdb
配置步骤
1 首先,从git下载下来插件,放到/private_html/wp-content/plugins下面
1 2 |
cd xxx/private_html/wp-content/plugins git clone https://github.com/stuttter/ludicrousdb.git |
2 把db.php db-error.php 放到 wp-content/ 目录下
1 2 |
cp db.php wp-content/ cp db-error.php wp-content/ |
3 把db-config.php 放到 wp根目录下
1 |
cp db-config.php private_html/ |
4 配置 wp-config.php 数据库设置
1 2 3 4 |
vim wp-config.php /** MySQL hostname */ define('DB_SLAVE', 'localhost'); define('DB_MASTER', 'xxxxxx'); |
5 配置 db-config.php 数据库设置
master write 优先级是1 read是2
slave write 优先级是0 read是1
这样 read 优先从slave, wirte只会写master.
如果多数据库可以自己更改优先级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// master $wpdb->add_database( array( 'host' => DB_MASTER, 'user' => DB_USER, 'password' => DB_PASSWORD, 'name' => DB_NAME, 'write' => 1, 'read' => 2, ) ); // slave $wpdb->add_database( array( 'host' => DB_SLAVE, 'user' => DB_USER, 'password' => DB_PASSWORD, 'name' => DB_NAME, 'write' => 0, 'read' => 1, ) ); |
6 其他细节
SLAVE 加上
1 |
if (defined('DB_SLAVE') && !is_admin()){add_database(xxxxxxxxxx)} |
在theme function.php 加入
1 2 3 4 5 6 7 8 9 10 |
/** * * add delay fro admin * */ add_action('wp_login', 'delayLoginForDBReplicationLag'); /** * * * Due to replica latency from Writer to Reader, we need to delay login so the user is properly logged in * * */ function delayLoginForDBReplicationLag() { sleep(1); } |
Comment