玩玩 MySQL 更新 Memcached (2) MySQL 服务器
现在配置 MySQL 数据库服务器 和 MySQL 的 Memcached 扩展.
下载最新版的 mysql 5.1 二进制包. 这里使用 mysql-5.1.37-linux-i686-icc-glibc23.tar.gz
tar zxf mysql-5.1.37-linux-i686-icc-glibc23.tar.gz sudo mv mysql-5.1.37-linux-i686-icc-glibc23 /usr/local/mysql sudo groupadd mysql sudo useradd -g mysql mysql cd /usr/local/mysql sudo chown -R mysql . sudo chgrp -R mysql . sudo scripts/mysql_install_db --user=mysql sudo chown -R root . sudo chown -R mysql data sudo cp support-files/my-medium.cnf /etc/my.cnf sudo support-files/mysql.server start sudo bin/mysqladmin -u root password 'rootpwd' sudo bin/mysql -u root -p
创建两个供外网测试帐号:
GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'localhost' IDENTIFIED BY 'dbpasswd' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'%' IDENTIFIED BY 'dbpasswd' WITH GRANT OPTION;
数据库初步安装OK, 下面配置 Memcached 扩展.
wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz tar zxf libevent-1.4.12-stable.tar.gz ./configure --prefix=/usr make sudo make install cd .. wget http://memcached.googlecode.com/files/memcached-1.4.0.tar.gz tar zxf memcached-1.4.0.tar.gz ./configure --prefix=/usr make sudo make install cd .. wget http://download.tangent.org/libmemcached-0.31.tar.gz cd libmemcached-0.31/ ./configure --prefix=/usr make sudo make install cd .. wget http://download.tangent.org/memcached_functions_mysql-0.9.tar.gz ./configure --with-mysql=/usr/local/mysql/bin/mysql_config --libdir=/usr/local/mysql/lib/plugin make sudo make install
给 MySQL 添加 UDF 函数, 修改 memcached_functions_mysql_0.9 下的 sql 目录中的 install_functions.sql 文件, 去掉 memc_servers_version 这行. 保存, 导入到 mysql 中.
/usr/local/mysql/bin/mysql -u root -p < sql/install_functions.sql
数据库配置到此OK, 下面进行数据库的主从配置.
玩玩 MySQL 更新 Memcached (2) Memcached 服务器
现在准备两台 Memcached 服务器, 使用超简单模式安装配置:
wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz tar zxf libevent-1.4.12-stable.tar.gz cd libevent-1.4.12-stable/ ./configure --prefix=/usr make sudo make install cd .. wget http://memcached.googlecode.com/files/memcached-1.4.0.tar.gz tar zxf memcached-1.4.0.tar.gz ./configure --prefix=/usr make sudo make install
安装完毕! 启动 Memcached 服务:
sudo memcached -d -m 64 -u root -l 192.168.2.95 -p 11211 -c 64 -P /tmp/memcached.pid
检查一下是否起来:
ps auxf | grep memcached
关闭 Memcached 服务:
sudo kill `cat /tmp/memcached.pid`
Memcached 启动参数参考:
-d 选项是启动一个守护进程,
-m 是分配给Memcache使用的内存数量,单位是MB,我这里是64MB
-u 是运行Memcache的用户,我这里是root
-l 是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.2.95,
-p 是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c 选项是最大运行的并发连接数,默认是1024,我这里设置了64,按照你服务器的负载量来设定,
-P 是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
2台 Memcached 服务器都按此配置即可. 注意启动 memcached 的时候监听IP地址不同. 最后贴一下测试脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <?php /** * Memache test Script * * @author guya * @version 1.0 */ /** * print message * * @param string $str message content * @return void */ function p($str = '') { echo '<h3>'.$str.'</h3>'; } // start test p('start memcache test'); //the memcache host ip address $memHost = '192.168.2.95'; //$memHost = '192.168.2.96'; //the memache host port $memPort = 11211; //test key value $testKey = 'mmkey'; //create memcahce client boject $mem = new Memcache(); //connect to memcache server if(!$mem->connect($memHost, $memPort)) { p('Can not connect memcache server: ' . $memHost . ':' . $memPort); exit(); } /** else { //print the memcache server status p('memcache server status: ' . $mem->getServerStatus($memHost, $memPort)); //print the memache server information $stats = $mem->getStats(); if(is_array($stats)) { foreach($stats as $k => $v) { p('Server setting key: ' . $k . ' => value: ' . $v); } } } //*/ //get the test key value p('Test Data: key: ' . $testKey . ' => value: ' . $mem->get($testKey)); //update key value $mem->set($testKey, $memHost . ' - Updated at - ' . time()); //get data from memcache again p('Test Data get again: key: ' . $testKey . ' => value: ' . $mem->get($testKey)); p('test end! ^o^'); |
玩玩 MySQL 更新 Memcached (1) 目标
玩玩 MySQL 5.1 的 trigger, 让 MySQL 通过 Trigger 更新 本地的 Memcached 服务器. 数据库 主从模式. 具体模拟测试配置如下:
Master DB Server IP: 192.168.2. 92 (Ubuntu-8.04 base system + mysql 5.1.37)
Slave DB Server IP: 192.168.2.93 (同上)
Memcached Server 1: 192.168.2. 95 (Ubuntu-8.04 base system + memcached-1.4)
Memcached Server 2: 192.168.2.96 (同上)
假设 Memcached Server 1 和 Master DB Server 是一个本地环境, Memcached Server 2 和 Slave DB Server 是一个本地环境, Master 和 Slave 在不同的 IDC 中. 我们实现当 Master DB 上的数据被更新时. Master DB 自己更新 Memcached Server 1 上的数据, 当 Slave DB 同步 Master DB 上的数据时, 也自己去更新 Memcached Server 2 上的数据. 见下图:
OK, 现在我们分别搭建模拟服务器的环境. 见下一篇 玩玩 MySQL 更新 Memcached (2) Memcached 服务器
mysql 同步 trigger 更新 memcache
聊一下通过 MySQL 通过触发器更新本地的 Memcached 服务器.
模拟一下服务器环境:
Master Database Server: 192.168.2.96
Slave Database Server: 192.168.2.95
和 master db 同步的 memcache 服务器: 192.168.2.92
和 slave db 同步的 memcache 服务器: 192.168.2.93
先搭建 Memcached 服务器环境:
下载安装 Memcached, 编译安装.
wget http://memcached.googlecode.com/files/memcached-1.4.0.tar.gz tar zxf memcached-1.4.0.tar.gz ./configure --prefix=/usr make sudo make install wget http://download.tangent.org/libmemcached-0.31.tar.gz cd libmemcached-0.31/ ./configure --prefix=/usr make sudo make install
启动 Memcached 服务, 在另一台机器用PHP测试一下:
sudo memcached -d -m 64 -u root -l 192.168.2.92 -p 11211 -c 64 -P /tmp/memcached.pid
检查一下 ps auxf | grep memcached
<?php /** * Memache test Script * * @author guya * @version 1.0 */ /** * print message * * @param string $str message content * @return void */ function p($str = '') { echo '<h3>'.$str.'</h3>'; } // start test p('start memcache test'); //the memcache host ip address $memHost = '192.168.2.92'; //$memHost = '192.168.2.93'; //the memache host port $memPort = 11211; //test key value $testKey = 'mmkey'; //create memcahce client boject $mem = new Memcache(); //connect to memcache server if(!$mem->connect($memHost, $memPort)) { p('connect memcache server false!'); exit(); } /** else { //print the memcache server status p('memcache server status: ' . $mem->getServerStatus($memHost, $memPort)); //print the memache server information $stats = $mem->getStats(); if(is_array($stats)) { foreach($stats as $k => $v) { p('Server setting key: ' . $k . ' => value: ' . $v); } } } //*/ //get the test key value $var = $mem->get($testKey); p('Test Data: key: ' . $testKey . ' => value: ' . $var); //update key value $mem->set($testKey, $memHost . ' - ' . time()); //get data from memcache again //get the test key value $var = $mem->get($testKey); p('Test Data get again: key: ' . $testKey . ' => value: ' . $var); p('test end! ^o^');
关闭 memcached 服务器: sudo kill `cat /tmp/memcached.pid`
测试脚本 memcache.php
Memcached 服务器运行正常. OK! 按照上面的再配置一台, IP地址修改为 192.168.2.93
再配置一下数据库主从配置
创建一个新的数据库服务器. 安装 MySQL 5.1 版服务器.
下载编译好的 mysql5.1 包, 解压即可, 也可以编译安装. 编译时间比较长.
tar zxf mysql-5.1.37-linux-i686-icc-glibc23.tar.gz mv mysql-5.1.37-linux-i686-icc-glibc23 /usr/local/mysql root@db:~# groupadd mysql root@db:~# useradd -g mysql mysql root@db:~# cd /usr/local/mysql root@db:~# chown -R mysql . root@db:~# chgrp -R mysql . root@db:~# scripts/mysql_install_db --user=mysql root@db:~# chown -R root . root@db:~# chown -R mysql data root@db:~# cp support-files/my-medium.cnf /etc/my.cnf root@db:~# support-files/mysql.server start root@db:~# bin/mysqladmin -u root password 'rootpwd' root@db:~# bin/mysql -u root -p
登录MySQL, 创建一个对外的测试账户.
GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'localhost' IDENTIFIED BY 'dbpasswd' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'%' IDENTIFIED BY 'dbpasswd' WITH GRANT OPTION;
配置从数据库同步帐号
GRANT REPLICATION SLAVE ON *.* TO repluser@192.168.2.95 IDENTIFIED BY 'slavepwd';
创建数据库和数据表
CREATE DATABASE memcache; CREATE TABLE `memcache`.`memcaches` ( `mkey` VARCHAR(255) UNIQUE NOT NULL, `mvalue` TEXT , PRIMARY KEY (`mkey`) ) ENGINE = MyISAM CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT = 'memcache key and values';
查看 Master 状态 : show master status;
测试数据库, 测试脚本 db.php
<?php /** * MySQL Test Script * * @author guya * @version 1.0 */ /** * print message * * @param string $str message content * @return void */ function p($str = '') { echo '<h3>'.$str.'</h3>'; } /** * connect to mysql server * * @param string $host server host name or ip address * @param string $user the database user account * @param string $passwd the user login password * @param string $db the database name * @return resource */ function connectToDb($host, $user, $passwd, $db) { //create new mysql server link $dbLink = mysql_connect($host, $user, $passwd, true); if(!$dbLink) { p('mysql connect false! ' . mysql_error()); exit(); } //select db if(!mysql_select_db($db, $dbLink)) { p('can not use database: ' . $db . ' error: ' . mysql_error($dbLink)); exit(); } //set the charset, need php 5.2.3 or newer mysql_set_charset('utf8', $dbLink); return $dbLink; } // start test p('start mysql test'); //database host address $dbHost = '192.168.2.96'; //database user name $dbUser = 'dbuser'; //database user password $dbPasswd = 'dbpasswd'; //database name $dbName = 'memcache'; //key $testKey = 'mmkey'; //get the key from database $sql = "SELECT * FROM `memcaches` WHERE `mkey` = '$testKey' LIMIT 1"; $dbLink = connectToDb($dbHost, $dbUser, $dbPasswd, $dbName); $result = mysql_query($sql, $dbLink); if(!$result) { p('SQL execute error: ' . mysql_error($dbLink)); exit(); } $isExists = 0; if(mysql_num_rows($result) == 0) { p('no record in database table'); } else { $isExists = 1; $row = mysql_fetch_assoc($result); $dbValue = $row['mvalue']; p('Get Database data: ' . $row['mkey'] . ' => ' . $dbValue); } // update db data $newValue = $dbHost . ' Update at ' . time(); if($isExists) { $sql = "UPDATE `memcaches` SET `mvalue`='$newValue' WHERE `mkey`='$testKey'"; } else { $sql = "INSERT INTO `memcaches` (`mkey`,`mvalue`) VALUES ('$testKey', '$newValue')"; } //$sql = "INSERT INTO `memcaches` VALUES('$testKey','$newValue') ON DUPLICATE KEY UPDATE mvalue='$newValue'"; mysql_query($sql, $dbLink); p('db data has been update to: ' . $newValue); mysql_close($dbLink); //get db data again //get the key from database $sql = "SELECT * FROM `memcaches` WHERE `mkey` = '$testKey' LIMIT 1"; $dbLink = connectToDb($dbHost, $dbUser, $dbPasswd, $dbName); $result = mysql_query($sql, $dbLink); if(!$result) { p('2: SQL execute error: ' . mysql_error($dbLink)); exit(); } if(mysql_num_rows($result) == 0) { p('2: no record in database table'); } else { $row = mysql_fetch_assoc($result); $dbValue = $row['mvalue']; p('2: Get Database data: ' . $row['mkey'] . ' => ' . $dbValue); } p('test end! ^o^');
接下来配置 Master 数据库服务器同步更新 Memcache 扩展功能.
wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz tar zxf libevent-1.4.12-stable.tar.gz ./configure --prefix=/usr make sudo make install wget http://memcached.googlecode.com/files/memcached-1.4.0.tar.gz tar zxf memcached-1.4.0.tar.gz ./configure --prefix=/usr make sudo make install wget http://download.tangent.org/libmemcached-0.31.tar.gz cd libmemcached-0.31/ ./configure --prefix=/usr make sudo make install wget http://download.tangent.org/memcached_functions_mysql-0.9.tar.gz ./configure --with-mysql=/usr/local/mysql/bin/mysql_config --libdir=/usr/local/mysql/lib/plugin make sudo make install
添加 UDF 到 MySQL, 0.9 版的 memcached_functions_mysql 需要删除 install_functions.sql 文件内的 memc_servers_version 行.
/usr/local/mysql/bin/mysql -u root -p < sql/install_functions.sql
登录数据库, 初始化 mysql 连接 memcache 服务器参数.
USE memcache; SELECT memc_servers_set('192.168.2.92:11211');
给表 memcaches 添加 tigger
DELIMITER | DROP TRIGGER IF EXISTS memcaches_insert | CREATE TRIGGER memcaches_insert AFTER INSERT ON memcaches FOR EACH ROW BEGIN SET @mm= memc_set(NEW.mkey, NEW.mvalue); END | DROP TRIGGER IF EXISTS memcaches_update | CREATE TRIGGER memcaches_update BEFORE UPDATE ON memcaches FOR EACH ROW BEGIN SET @mm= memc_replace(OLD.mkey, NEW.mvalue); END | DROP TRIGGER IF EXISTS memcaches_delete | CREATE TRIGGER memcaches_delete BEFORE DELETE ON memcaches FOR EACH ROW BEGIN SET @mm= memc_delete(OLD.mkey); END | DELIMITER ;
OK, 现在集成测试一下. 测试脚本 mysql-memcache.php
<?php /** * MySQL to Memache test Script * * @author guya * @version 1.0 */ /** * print message * * @param string $str message content * @return void */ function p($str = '') { echo '<h3>'.$str.'</h3>'; } /** * connect to mysql server * * @param string $host server host name or ip address * @param string $user the database user account * @param string $passwd the user login password * @param string $db the database name * @return resource */ function connectToDb($host, $user, $passwd, $db) { //create new mysql server link $dbLink = mysql_connect($host, $user, $passwd, true); if(!$dbLink) { p('mysql connect false! ' . mysql_error()); exit(); } //select db if(!mysql_select_db($db, $dbLink)) { p('can not use database: ' . $db . ' error: ' . mysql_error($dbLink)); exit(); } //set the charset, need php 5.2.3 or newer mysql_set_charset('utf8', $dbLink); return $dbLink; } // start test p('start test'); //database host address $dbHost = '192.168.2.96'; //database user name $dbUser = 'dbuser'; //database user password $dbPasswd = 'dbpasswd'; //database name $dbName = 'memcache'; //key $testKey = 'mmkey'; //the memcache host ip address $memHost = '192.168.2.92'; //the memache host port $memPort = 11211; //create memcahce client boject $mem = new Memcache(); //connect to memcache server if(!$mem->connect($memHost, $memPort)) { p('connect memcache server false!'); exit(); } /** else { //print the memcache server status p('memcache server status: ' . $mem->getServerStatus($memHost, $memPort)); //print the memache server information $stats = $mem->getStats(); if(is_array($stats)) { foreach($stats as $k => $v) { p('Server setting key: ' . $k . ' => value: ' . $v); } } } //*/ //get the test key value $var = $mem->get($testKey); p('Get memcahce data for the first time: key: ' . $testKey . ' => value: ' . $var); unset($var); //get the key from database $sql = "SELECT * FROM `memcaches` WHERE `mkey` = '$testKey' LIMIT 1"; $dbLink = connectToDb($dbHost, $dbUser, $dbPasswd, $dbName); $result = mysql_query($sql, $dbLink); if(!$result) { p('SQL execute error for the first time: ' . mysql_error($dbLink)); exit(); } //key is exists $isExists = 0; if(mysql_num_rows($result) == 0) { p('No record in database table memcaches with mkey is: ' . $testKey); } else { $isExists = 1; $row = mysql_fetch_assoc($result); $dbValue = $row['mvalue']; p('Get Database data for the first time: ' . $row['mkey'] . ' => ' . $dbValue); } // update db data $newValue = $dbHost . ' Update at ' . time(); if($isExists) { $sql = "UPDATE `memcaches` SET `mvalue`='$newValue' WHERE `mkey`='$testKey'"; } else { $sql = "INSERT INTO `memcaches` (`mkey`,`mvalue`) VALUES ('$testKey', '$newValue')"; } //$sql = "INSERT INTO `memcaches` VALUES('$testKey','$newValue') ON DUPLICATE KEY UPDATE mvalue='$newValue'"; mysql_query($sql, $dbLink); p('Table memcaches data has been update to: ' . $newValue . ' by key: ' . $testKey); mysql_close($dbLink); //get db data again //get the key from database $sql = "SELECT * FROM `memcaches` WHERE `mkey` = '$testKey' LIMIT 1"; $dbLink = connectToDb($dbHost, $dbUser, $dbPasswd, $dbName); $result = mysql_query($sql, $dbLink); if(!$result) { p('SQL execute error for the second time: ' . mysql_error($dbLink)); exit(); } if(mysql_num_rows($result) == 0) { p('Second time select table. No record in database table memcaches with mkey is: ' . $testKey); } else { $row = mysql_fetch_assoc($result); $dbValue = $row['mvalue']; p('Get Database data for the second time: ' . $row['mkey'] . ' => ' . $dbValue); } //get data from memcache again //get the test key value $var = $mem->get($testKey); p('Get data again from memcache server: key: ' . $testKey . ' => value: ' . $var); p('test end! ^o^');
配置从数据库服务器
数据库和安装和 master 数据库安装一样. 另外有些其他配置
修改 /etc/my.cnf 确保有下面的配置存在
[mysqld] server-id=2 master-host = 192.168.2.96 master-user = repluser master-password = slavepwd master-port = 3306 replicate-do-db = memcache
配置OK, 重启 mysql 服务, 数据库主从生效. 现在也从数据库也配置同步memcache扩展
在从数据库服务器安装 libevent, memcahced, libmemcached, memcached_functions_mysql 和主数据一致.
最终测试脚本
<?php /** * MySQL to Memache test Script * * @author guya * @version 1.0 */ /** * print message * * @param string $str message content * @return void */ function p($str = '') { echo '<h3>'.$str.'</h3>'; } /** * connect to mysql server * * @param string $host server host name or ip address * @param string $user the database user account * @param string $passwd the user login password * @param string $db the database name * @return resource */ function connectToDb($host, $user, $passwd, $db) { //create new mysql server link $dbLink = mysql_connect($host, $user, $passwd, true); if(!$dbLink) { p('mysql connect false! ' . mysql_error()); exit(); } //select db if(!mysql_select_db($db, $dbLink)) { p('can not use database: ' . $db . ' error: ' . mysql_error($dbLink)); exit(); } //set the charset, need php 5.2.3 or newer mysql_set_charset('utf8', $dbLink); return $dbLink; } // start test p('start test'); //database host address $dbMasterHost = '192.168.2.96'; $dbSlaveHost = '192.168.2.95'; //database user name $dbUser = 'dbuser'; //database user password $dbPasswd = 'dbpasswd'; //database name $dbName = 'memcache'; //key $testKey = 'mmkey'; //the memcache host ip address $memMasterHost = '192.168.2.92'; $memSlaveHost = '192.168.2.93'; //the memache host port $memPort = 11211; //create memcahce client boject $memMaster = new Memcache(); $memSlave = new Memcache(); //connect to memcache server if(!$memMaster->connect($memMasterHost, $memPort)) { p('connect memcache master server false!'); exit(); } /** else { //print the memcache server status p('memcache master server status: ' . $memMaster->getServerStatus($memMasterHost, $memPort)); //print the memache server information $stats = $memMaster->getStats(); if(is_array($stats)) { foreach($stats as $k => $v) { p('Master Memcached Server setting key: ' . $k . ' => value: ' . $v); } } } //*/ if(!$memSlave->connect($memSlaveHost, $memPort)) { p('connect memcache slave server false!'); exit(); } /** else { //print the memcache server status p('memcache slave server status: ' . $memSlave->getServerStatus($memSlaveHost, $memPort)); //print the memache server information $stats = $memSlave->getStats(); if(is_array($stats)) { foreach($stats as $k => $v) { p('Slave Memcached Server setting key: ' . $k . ' => value: ' . $v); } } } //*/ //get the test key value $masterVar = $memMaster->get($testKey); p('Get Master memcahce data for the first time: key: ' . $testKey . ' => value: ' . $masterVar); unset($masterVar); //get the test key value $slaveVar = $memSlave->get($testKey); p('Get Slave memcahce data for the first time: key: ' . $testKey . ' => value: ' . $slaveVar); unset($slaveVar); //get the key from slave database $sql = "SELECT * FROM `memcaches` WHERE `mkey` = '$testKey' LIMIT 1"; $dbSlaveLink = connectToDb($dbSlaveHost, $dbUser, $dbPasswd, $dbName); $result = mysql_query($sql, $dbSlaveLink); if(!$result) { p('SQL execute error for the first time at slave server: ' . mysql_error($dbSlaveLink)); exit(); } if(mysql_num_rows($result) == 0) { p('No record in slave database table memcaches with mkey is: ' . $testKey); } else { $row = mysql_fetch_assoc($result); p('Get slave database data for the first time: ' . $row['mkey'] . ' => ' . $row['mvalue']); } mysql_close($dbSlaveLink); unset($dbSlaveLink); //get the key from master database $sql = "SELECT * FROM `memcaches` WHERE `mkey` = '$testKey' LIMIT 1"; $dbMasterLink = connectToDb($dbMasterHost, $dbUser, $dbPasswd, $dbName); $result = mysql_query($sql, $dbMasterLink); if(!$result) { p('SQL execute error for the first time at master server: ' . mysql_error($dbMasterLink)); exit(); } //key is exists $isExists = 0; if(mysql_num_rows($result) == 0) { p('No record in master database table memcaches with mkey is: ' . $testKey); } else { $isExists = 1; $row = mysql_fetch_assoc($result); p('Get master database data for the first time: ' . $row['mkey'] . ' => ' . $row['mvalue']); } // update db data $newValue = 'New Data Update at ' . time(); if($isExists) { $sql = "UPDATE `memcaches` SET `mvalue`='$newValue' WHERE `mkey`='$testKey'"; } else { $sql = "INSERT INTO `memcaches` (`mkey`,`mvalue`) VALUES ('$testKey', '$newValue')"; } //$sql = "INSERT INTO `memcaches` VALUES('$testKey','$newValue') ON DUPLICATE KEY UPDATE mvalue='$newValue'"; mysql_query($sql, $dbMasterLink); p('Master Db table memcaches data has been update to: ' . $newValue . ' by key: ' . $testKey); mysql_close($dbMasterLink); unset($dbMasterLink); //get db data again //get db data from salve database server $sql = "SELECT * FROM `memcaches` WHERE `mkey` = '$testKey' LIMIT 1"; $dbSlaveLink = connectToDb($dbSlaveHost, $dbUser, $dbPasswd, $dbName); $result = mysql_query($sql, $dbSlaveLink); if(!$result) { p('SQL execute error for the second time at slave server: ' . mysql_error($dbSlaveLink)); exit(); } if(mysql_num_rows($result) == 0) { p('No record in slave database table memcaches in second time with mkey is: ' . $testKey); } else { $row = mysql_fetch_assoc($result); p('Get slave database data for the second time: ' . $row['mkey'] . ' => ' . $row['mvalue']); } mysql_close($dbSlaveLink); unset($dbSlaveLink); //get db data from master database server $sql = "SELECT * FROM `memcaches` WHERE `mkey` = '$testKey' LIMIT 1"; $dbMasterLink = connectToDb($dbMasterHost, $dbUser, $dbPasswd, $dbName); $result = mysql_query($sql, $dbMasterLink); if(!$result) { p('SQL execute error for the second time at master server: ' . mysql_error($dbMasterLink)); exit(); } if(mysql_num_rows($result) == 0) { p('No record in master database table memcaches in second time with mkey is: ' . $testKey); } else { $row = mysql_fetch_assoc($result); p('Get master database data for the second time: ' . $row['mkey'] . ' => ' . $row['mvalue']); } mysql_close($dbMasterLink); unset($dbMasterLink); //get data from memcache again p('Get data again from master memcache server: key: ' . $testKey . ' => value: ' . $memMaster->get($testKey)); p('Get data again from slave memcache server: key: ' . $testKey . ' => value: ' . $memSlave->get($testKey)); p('test end! ^o^');
目前碰到的问题: Slave 数据库通过 trigger 同步更新 memcache 的时候, 手动执行 update, insert, delete 操作正常. 在 master -slave 模式更新数据时失效.
原因正在研究中..
发布一个基于 Zend Framework 的开源项目 momoger.com
作开源工作很累。 这个项目是我在新公司给同事们介绍 Zend Framework 用的示例项目。 可惜效果不是很理想。具体的就不说了。
项目源码发布到 google code 上。 项目地址为: http://code.google.com/p/momoger/
喜欢学习 Zend Framework 的同学可以下载去看看, 这个例子还是比较全面的。 以后有时间可能把其他的方面也开发进来。
建议先看项目里的 documents/readme.txt , 关键的说明我都写在里面了。
有问题和已经可以和我交流。 呵呵!
