Drupal 7 开发内部培训资料, 分头诗人

drupal7如何做mysql的主从复制和读写分离呢?

赞成!
0
否决!

我们有个站点,node和user由于共享,都放在一个DB里,域名由domain_access模板管理。
随着数据越来越多,我们需要做主从复制和读写分离,意思是主服务器专门写入数据,从服务器是读出来,并做负载均衡。

是直接在setting里改,还是怎么呢?

请问各位有思路或者案例吗?谢谢了!

1 个回答

赞成!
0
否决!

没真正试过drupal主从,在drupal 7 的settings.php 里面可以找到下面注释

 * For each database, you may optionally specify multiple "target" databases.
 * A target database allows Drupal to try to send certain queries to a
 * different database if it can but fall back to the default connection if not.
 * That is useful for master/slave replication, as Drupal may try to connect
 * to a slave server when appropriate and if one is not available will simply
 * fall back to the single master server.
 *
 * The general format for the $databases array is as follows:
 * @code
 * $databases['default']['default'] = $info_array;
 * $databases['default']['slave'][] = $info_array;
 * $databases['default']['slave'][] = $info_array;
 * $databases['extra']['default'] = $info_array;
 * @endcode
 *
 * In the above example, $info_array is an array of settings described above.
 * The first line sets a "default" database that has one master database
 * (the second level default).  The second and third lines create an array
 * of potential slave databases.  Drupal will select one at random for a given
 * request as needed.  The fourth line creates a new database with a name of
 * "extra".
 *
 * For a single database configuration, the following is sufficient:
 * @code
 * $databases['default']['default'] = array(
 *   'driver' => 'mysql',
 *   'database' => 'databasename',
 *   'username' => 'username',
 *   'password' => 'password',
 *   'host' => 'localhost',
 *   'prefix' => 'main_',
 *   'collation' => 'utf8_general_ci',
 * );
 * @endcode

 

上面注释说的是可以通过下面代码方式进行设置主从

 $databases['default']['default'] = $info_array;
 $databases['default']['slave'][] = $info_array;
 $databases['default']['slave'][] = $info_array;
 $databases['extra']['default'] = $info_array;

 

第一行是猪数据库,

第二行第三行是从数据库,

第四行是额外附加数据库,

按照这个说法,是类似如下例子:

<?php
$databases['default']['default'] = array(
  'driver' => 'mysql',
  'database' => 'drupaldb1',
  'username' => 'username',
  'password' => 'secret',
  'host' => 'dbserver1',
);
$databases['default']['slave'][] = array(
  'driver' => 'mysql',
  'database' => 'drupaldb2',
  'username' => 'username',
  'password' => 'secret',
  'host' => 'dbserver2',
);
$databases['default']['slave'][] = array(
  'driver' => 'mysql',
  'database' => 'drupaldb3',
  'username' => 'username',
  'password' => 'secret',
  'host' => 'dbserver3',
);
?>

当然,你也可以试一下使用模块 AutoSlave,说不定他能符合你需要,又或者在其身上找到解决方法。