阿七

  • HOME
  • CATEGORY
    • TECH
    • ENG
    • VIDEO
    • Image
    • MUSIC
  • Baidu
  • MY LIST
LOVE LASTS FOREVER
  1. Home
  2. TECH
  3. Text

LudicrousDB - Wordpress 数据库主从热备

20/10/2018 2318Views 6Likes 1Comments

简介

因为数据库用的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);
}

Licensed under 知识共享署名 4.0 国际许可协议
Tag: web
Last:24/11/2018

frank

Love Lasts Forever

Like
< PREV
NEXT >

Comment

  • aq

    :persevering:

    12/05/2022
    回复
  • 取消回复

    frank

    Love Lasts Forever

    New Hot Rand
    New Hot Rand
    好久没更新了 DirectAdmin 去掉邮件队列 Debian Directadmin 中设置 wordpress 最全的伪静态配置 vnstat 清零流量重新统计 忘记关于什么事情了 selinux DirectAdmin面板 删除log
    Hello World! rclone 使用方法 GoogleDrive,DropBox,OneDrive for Business ZOHO 目前最好用的免费邮局 Beautiful Now - Zedd The master-slave hot standby of databese of wordpress DirectAdmin面板 删除log

    © 2022 阿七 ALL RIGHTS RESERVED | Working

    Theme Kratos Made By Seaton Jiang