时间:2021-07-01 10:21:17 帮助过:54人阅读
$configs['db_info']['blog'][0] = array(
'db_host' => '192.168.0.1',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['blog'][1] = array(
'db_host' => '192.168.0.2',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['blog'][2] = array(
'db_host' => '192.168.0.2',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
//...还有很多
至于选择哪一台服务器,只需要根据uid做一个简单的匹配就可以了。
再谈到的就是主从数据库了。什么情况下使用主从数据库?比如某个名人博客,访问量相当的大,已经没有办法把他的数据再进行拆分了,这个时候就得考虑主从数据库服务器了,使用多台数据库来分流。这样要适用主从和分库,可能上面配置信息得稍微改动一下。
代码如下:
$configs['db_info']['blog'][0]['master'] = array(
'db_host' => '192.168.0.1',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['blog'][0]['slave'][0] = array(
'db_host' => '192.168.0.2',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['blog'][0]['slave'][1] = array(
'db_host' => '192.168.0.3',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['blog'][1]['master'] = array(
'db_host' => '192.168.0.4',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['blog'][1]['slave'][0] = array(
'db_host' => '192.168.0.5',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['blog'][1]['slave'][1] = array(
'db_host' => '192.168.0.6',
'db_name' => 'blog',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['session'][0]['master'] = array(
'db_host' => '192.168.0.7',
'db_name' => 'session',
'db_user' => 'root',
'db_pass' => '',
);
$configs['db_info']['session'][1]['master'] = array(
'db_host' => '192.168.0.8',
'db_name' => 'session',
'db_user' => 'root',
'db_pass' => '',
);
写到这里,我想都应该知道如何分表和分配你的数据库了吧,接下去我就来说一下如何轻松的读取这样的配置信息,如何将这些配置融入你的数据库驱动中。
12下一页阅读全文