Windows Environment:
Install the AppServ package.
Configure Apache Virtual Hosts:
Include conf/extra/httpd-vhosts.conf
Edit httpd-vhosts.conf:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "D:/AppServ/www/"
ServerName youhap
ErrorLog "logs/youhap-error.log"
CustomLog "logs/youhap-access.log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/AppServ/www/mysite/"
ServerName center
ErrorLog "logs/center-error.log"
CustomLog "logs/center-access.log" common
</VirtualHost>
Edit hosts:
127.0.0.1 localhost
127.0.0.1 center
127.0.0.1 youhap
Save each file.
For PHP coding, I am using the CodeIgniter framework.
Download link: http://219.239.26.9/download/832062/866311/1/zip/151/72/1252812646551_840/CodeIgniter_1.7.2.zip
MySQL Master-Slave Separation:
MySQL Master-Slave Configuration and Optimization:
————-Mysql Replication Setup————
# The MySQL server
[mysqld]
port = 3306
server-id = 1
#log-bin
master-host = 10.99.1.1
master-user = slave
master-password = slave
master-port = 3306
slave-skip-errors = 1050,1007,1051,1062
read-only
socket = /tmp/mysql.sock
skip-locking
key_buffer = 384M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
Execute on the master:
mysql>GRANT FILE ON *.* TO slave@'%' IDENTIFIED BY 'slave';
mysql>GRANT REPLICATION SLAVE ON *.* TO slave@'%' IDENTIFIED BY 'slave';
mysql>flush privileges;
Modify the slave’s my.cnf:
master-host = 10.99.1.1
master-user = slave
master-password = slave
master-port = 3306
server-id = 2
slave-skip-errors = 1050,1007,1051,1062
read-only
After starting MySQL,
execute on the slave: slave start;
Once master-slave replication is successful, proceed.
One master, multiple slaves.
Modification and insertion operations connect to the master server, while read operations fetch data from the slave(s).
The following code has been tested and verified.
Database connection settings:
<?php
$active_group = "master";
$active_record = TRUE;
$db['master']['hostname'] = "192.168.1.1";
$db['master']['username'] = "root";
$db['master']['password'] = "******************";
$db['master']['database'] = "dbname";
$db['master']['dbdriver'] = "mysql";
$db['master']['dbprefix'] = "";
$db['master']['pconnect'] = TRUE;
$db['master']['db_debug'] = TRUE;
$db['master']['cache_on'] = FALSE;
$db['master']['cachedir'] = "";
$db['master']['char_set'] = "utf8";
$db['master']['dbcollat'] = "utf8_general_ci";
$db['slave']['hostname'] = "192.168.1.2";
$db['slave']['username'] = "root";
$db['slave']['password'] = "********************";
$db['slave']['database'] = "dbname";
$db['slave']['dbdriver'] = "mysql";
$db['slave']['dbprefix'] = "";
$db['slave']['pconnect'] = TRUE;
$db['slave']['db_debug'] = TRUE;
$db['slave']['cache_on'] = FALSE;
$db['slave']['cachedir'] = "";
$db['slave']['char_set'] = "utf8";
$db['slave']['dbcollat'] = "utf8_general_ci";
?>
At this point, the master and slave are separated. However, if you only have one master and one slave server, and the proportion of write operations is relatively small, you can also let the master server handle a portion of the read queries. Modify the above configuration:
$rand = mt_rand(1, 10);
/* Only master-slave
* For reading data: master 30%, slave 70%
* */
if( $rand < 4){
$db['slave']['hostname'] = 'master_ip';
}else{
$db['slave']['hostname'] = 'slave_ip';
}
/* Multiple slaves, randomly select one to read data from
* $slaveGroup = array('slave1_ip' => 1, 'slave2_ip' => 2, 'slave3_ip' => 3);
* $db['slave']['hostname'] = array_rand($slaveGroup, 1);
* */
Controller:
<?php
class Replication extends Controller{
private $master;
private $slave;
function Replication(){
parent::Controller();
$this->master = $this->load->database('master', true, true);
$this->slave = $this->load->database('slave', true, true);
}
function index(){
// Read/Write Splitting Technique
$this->master->set('awardName', '2008 olimpic games media');
$this->master->set('awardSubTypeId', 1);
$this->master->set('awardTypeId', 1);
$this->master->insert('awards');
$this->slave->from('t_login');
$query2 = $this->slave->get();
}
function memcache(){
$this->load->library('cache');
$conn = $this->cache->useMemcache('127.0.0.1', 11211);
$this->slave->from('t_login');
$query = $this->slave->get();
$this->cache->save('loginInfo', $query->result(), null, 3600);
var_dump($this->cache->get('loginInfo'));
?>
Installing memcached:
Download: http://jehiah.cz/projects/memcached-win32/files/memcached-1.2.1-win32.zip
Install: cmd –> memcached.exe -d install
Start: cmd –> memcached.exe -d start
Once it has started successfully.
Load the memcache.dll extension in php.ini
Download: http://jp.php.net/distributions/pecl-5.2.6-Win32.zip (note the version; my PHP is 5.2.6)
Extract it, find memcache.dll and place it in the ext directory.
Loading: Open php.ini:
extension=php_memcache.dll
Restart Apache. No errors means memcache is ready to use.
Usage:
<?php
$memcache = new Memcache;$memcache->connect('localhost', 11211) or die ("Could not connect");$data = $memcache->get('view_data');$memcache->set('view_data', $data, false, 3600) or die ("Failed to save data at the server");$memcache->get('view_data');
?>