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 服务器