Leo の Blog Where there is a will there is a way! Trust youself can do it!

27八/090

玩玩 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, 下面进行数据库的主从配置.

27八/090

玩玩 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^');
27八/090

玩玩 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 上的数据. 见下图:

MySQL_Update_Memcached

OK, 现在我们分别搭建模拟服务器的环境. 见下一篇 玩玩 MySQL 更新 Memcached (2) Memcached 服务器

26八/091

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 模式更新数据时失效.

原因正在研究中..

1七/090

PHP5.3.0 正式发布, 超级爽!

今天晚上打开 php.net 一看, 5.3.0 的正式版已经发布。 刚刚手头有台机器刚装了 ubuntu, 编译了一下 LAMP 系统, PHP 5.3.0 改动比较大。 第一眼看到的 php.ini 文件里已经变动了。 感觉比一以前版本的舒服点了。 有了开发环境和生产环境的配置区分了。 详细的以后慢慢了解了。 貌似增强了很多功能。 从 phpinfo() 里看出来的。 放出一张刚测试出的phpinfo()截图, 后面张贴一下本次编译的命令记录。

php-5.3.0-info

php-5.3.0-info

看看编译的一堆命令。 有需要的朋友可以拿去参考一下。

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
sudo apt-get install build-essential gcc make autoconf automake1.9 libtool
 
sudo apt-get install zlib1g-dev libdb-dev
 
tar -jxf apr-1.3.5.tar.bz2 -C ../builds/
tar -jxf apr-util-1.3.7.tar.bz2 -C ../builds/
tar -zxf httpd-2.2.11.tar.gz -C ../builds/
 
cd ../builds/
cd apr-1.3.5/
./configure 
make
sudo make install
 
cd ../apr-util-1.3.7/
./configure --with-apr=/usr/local/apr --with-berkeley-db=/usr
make
sudo make install
 
cd ../httpd-2.2.11/
./configure --prefix=/usr/local/apache2 --enable-rewrite=shared --enable-dav=shared --enable-so --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr/bin/apu-1-config
make
sudo make install
 
 
sudo apt-get install libxml2 libxml2-dev
 
tar -zxf neon-0.28.4.tar.gz -C ../builds/
tar -zxf zlib-1.2.3.tar.gz -C ../builds/
tar -zxf expat-2.0.1.tar.gz -C ../builds/
tar -zxf subversion-1.6.2.tar.gz -C ../builds/
mkdir ../builds/subversion-1.6.2/sqlite-amalgamation
tar -zxf sqlite-amalgamation-3.6.13.tar.gz
cp sqlite-3.6.13/sqlite3.c ../builds/subversion-1.6.2/sqlite-amalgamation/
rm -rf sqlite-3.6.13
 
cd ../builds/neon-0.28.4/
./configure
make
sudo make install
 
cd ../zlib-1.2.3/
./configure --prefix=/usr/local/zlib
make
sudo make install
 
cd ../expat-2.0.1/
./configure
make
sudo make install
 
cd ../subversion-1.6.2/
./configure --prefix=/usr/local/subversion --with-apxs=/usr/local/apache2/bin/apxs --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr/bin/apu-1-config --with-neon=/usr/local --with-zlib=/usr/local/zlib --with-ssl
make
sudo make install
 
tar -zxf mysql-5.0.83-linux-i686-icc-glibc23.tar.gz
sudo mv mysql-5.0.83-linux-i686-icc-glibc23 /usr/local/mysql
cd /usr/local/mysql/
sudo groupadd mysql
sudo useradd -g mysql mysql
sudo scripts/mysql_install_db --user=mysql
sudo chown -R mysql .
sudo chgrp -R mysql .
sudo chown -R root .
sudo chown -R mysql data
cd support-files/
sudo cp my-medium.cnf /etc/my.cnf
sudo gedit /etc/my.cnf
sudo ./mysql.server start
cd ../bin
./mysqladmin -u root password 'mypassword'
./mysql -u root -p
cd ../support-files/
sudo ./mysql.server stop
 
sudo apt-get install flex libpng12-0 libpng12-dev libltdl3-dev
sudo ln -sf libpng12.so.0 libpng.so
 
tar -zxf curl-7.19.5.tar.gz -C ../builds/
tar -zxf jpegsrc.v6b.tar.gz -C ../builds/
tar -zxf freetype-2.3.9.tar.gz -C ../builds/
tar -zxf libmcrypt-2.5.8.tar.gz -C ../builds/
tar -zxf mhash-0.9.9.9.tar.gz -C ../builds/
tar -zxf php-5.3.0.tar.gz -C ../builds/
 
cd ../builds/curl-7.19.5/
sudo mkdir /usr/local/curl
./configure --prefix=/usr/local/curl --with-ssl
make
sudo make install
 
cd ../jpeg-6b/
sudo mkdir /usr/local/jpeg
sudo mkdir /usr/local/jpeg/bin
sudo mkdir /usr/local/jpeg/lib
sudo mkdir /usr/local/jpeg/include
sudo mkdir /usr/local/jpeg/man
sudo mkdir /usr/local/jpeg/man/man1
./configure --prefix=/usr/local/jpeg --enable-shared --enable-static
make
sudo make install
 
cd ../freetype-2.3.9/
./configure --prefix=/usr/local/freetype2
make
sudo make install
 
cd ../libmcrypt-2.5.8/
./configure --prefix=/usr/local/libmcrypt
make
sudo make install
 
cd ../mhash-0.9.9.9/
./configure --prefix=/usr/local/libmhash
make
sudo make install
 
cd ../php-5.3.0/
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php \
--with-openssl \
--with-zlib \
--with-zlib-dir=/usr/local/zlib \
--enable-bcmath \
--enable-calendar \
--with-curl=/usr/local/curl \
--with-curlwrappers \
--enable-ftp \
--with-gd \
--with-jpeg-dir=/usr/local/jpeg \
--with-png-dir=/usr \
--with-freetype-dir=/usr/local/freetype2 \
--enable-gd-native-ttf \
--with-gettext \
--with-mcrypt=/usr/local/libmcrypt \
--with-mhash=/usr/local/libmhash \
--enable-mbstring \
--enable-exif \
--with-mysql=/usr/local/mysql \
--with-pdo-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-xmlrpc \
--enable-soap \
--enable-sockets \
--enable-zip
 
sudo cp php.ini-development /usr/local/php/php.ini
sudo gedit /usr/local/apache2/conf/httpd.conf

OK, 玩过头了。 赶紧睡觉先!

标签: , 没有评论
   下一页