MySQL数据映射到Memcached

测试情况在Linux下举行,版本体系为CentOS5.
以下为相干软件,包罗其版本和下载地点:
mysql-5.1.30  下载
memcached-1.2.6  下载
libevent-1.4.7-stable  下载
memcached_functions_mysql-0.8  下载
libmemcached-0.26  下载

编译安置MySQL,安置因小我细好而定,省略很多与测试无关的编译细节及参数。
[root@localhost ~]#tar xzf mysql-5.1.30.tar_001.gz
[root@localhost ~]#cd mysql-5.1.30
[root@localhost ~]#./configure --prefix=/usr/local/mysql51
[root@localhost ~]#make
[root@localhost ~]#make install
[root@localhost ~]#./scripts/mysql_install_db --user=mysql --skip-name-resolve
[root@localhost ~]#/usr/local/mysql51/bin/mysqld_safe
省略列出安置memcached和libevent的相干号令,详细可根据现实环境安置,测试时我将libevent默认安置,memcached安置于/usr/local/memcached目次下。
启动memcached.
/usr/local/memcached/bin/memcached -d -m 50 -u root -p 11211
编译安置libmemcache.
[root@localhost ~]#tar xzf libmemcached-0.26.tar.gz
[root@localhost ~]#cd libmemcached-0.26
[root@localhost ~]#./configure --with-memcached=/usr/local/memcached/bin/memcached
[root@localhost ~]# make && make install
编译安置Memcache UDFs for MySQL.
[root@localhost ~]# tar xzf memcached_functions_mysql-0.8.tar.gz
[root@localhost ~]# cd memcached_functions_mysql-0.8
[root@localhost ~]# ./configure --with-mysql-config=/usr/local/mysql51/bin/mysql_config
[root@localhost ~]# make && make install
编译完成后将编译好的库文件复制到mysql的插件目次下,以便于加载利用。
cp /usr/local/lib/libmemcached_functions_mysql* /usr/local/mysql51/lib/mysql/plugin/
进入memcached_functions_mysql的源码目次,在目次下有相干添加UDF的SQL文件用于初始化。
[root@localhost ~]# mysql 
      
注:UDFs相干利用在文章后部门
至此,相干软件的编译和安置完成,举行测试,我们要到达的目标是当MySQL有新记实插入时,同时插入到Memcached中,当记实更新时同步更 新Memcached中的记实,删除时同时也删除Memcached相干的记实,为此建立三个触发器来实现,若是对MySQL的触发法式不熟习可以参考 MySQL手册第21章,下面SQL中的memcached为必要操纵的表名,SQL如下:
#插入数据时插入Memcached
create trigger mysqlmmci after insert on memcached for each row set @tmp = memc_set(NEW.key, NEW.value);
#更新记实时更新Memcached
create trigger mysqlmmcu after update on memcached for each row set @tmp = memc_set(NEW.key, NEW.value);
#删除记实时删除Memcached响应的记实
create trigger mysqlmmcd before delete on memcached for each row set @tmp = memc_delete(OLD.key);
以下为测试记实,在对MySQL操纵的同时操纵Memcached来检察环境,固然你也可以在启动Memcached的时辰带-vv参数来检察相干信息.
MySQL操纵相干的记实:
[root@localhost ~]#mysql -S /tmp/mysql51.sock 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.30 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use test;
Database changed

mysql> create table `memcached` (`key` varchar(10), `value` varchar(100));
Query OK, 0 rows affected (0.00 sec)

mysql> create trigger mysqlmmci after insert on memcached for each row set @tmp = memc_set(NEW.key, NEW.value);
Query OK, 0 rows affected (0.00 sec)

mysql> create trigger mysqlmmcu after update on memcached for each row set @tmp = memc_set(NEW.key, NEW.value);
Query OK, 0 rows affected (0.00 sec)

mysql> create trigger mysqlmmcd before delete on memcached for each row set @tmp = memc_delete(OLD.key);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into memcached values("keyi", "valuei"),("keyu","valueu"),("keyd", "valued");
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> update memcached set `value`="update" where `key`="keyu";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> delete from memcached where `key`="keyd";
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye
Memcache检察时的记实:
[root@localhost ~]#telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get keyi
VALUE keyi 0 6
valuei
END
get keyu
VALUE keyu 0 6
valueu
END
get keyd
VALUE keyd 0 6
valued
END
get keyu
VALUE keyu 0 6
update
END
get keyd
END
quit
Connection closed by foreign host.
至此,我们根基实现的将MySQL的数据同步到Memcached中,机能临时还没有测试,固然上面只是简略的实现的数据映射的功效,若是在实现的 出产情况中,则必要思量名字空间,高靠得住性的题目,这些都是可以经由过程数据库名-表名-关头字的方面能到达KEY独一的目标,而高靠得住性则是一个比力大的问 题。
UDFS相干:

MySQL memcached UDFs

UDFs 是 User Defined Functions 的缩写,指 Mysql 的用户界说函数,应用可以经由过程利用这些函数从 Mysql5.0 以上版本的数据库中拜候 Memcached 写入大概得到数据。别的,从 Mysql5.1 起头撑持触发器,如许就可以在触发器中利用 UDFs 直接更新 Memcached 的内容,减轻了应用法式计划和编写的庞大性。下面我们扼要先容 UDFs 的安置和利用:
UDFs 的安置,必要在数据库办事器上安置两个包,别离是 libmemcached 和 memcached_functions_mysql ,都可以从  [url]http://download.tangent.org/[/url]下载。



安置完成之后,必要将 libmemcached_functions_mysql 的库文件拷贝到 mysql/lib 目次下的 plugin 目次中。
cp /usr/local/lib/libmemcached_functions_mysql* /home/mysql/lib/plugin/.
初次利用的时辰,必要利用 CREATE FUNCTION 来初始化用户界说函数。有两种方式可以初始化全部供给的用户界说函数,一个是实行 memcached_functions_mysql-0.4/sql 目次下的 install_functions.sql ;另一个是实行 memcached_functions_mysql-0.4/utils/install.pl 。
建立好函数之后,就可以利用这些函数举行 Memcached 的操纵,起首必要界说 Memcached 办事器,假设我们测试的情况设置装备摆设了两个 Memcached 办事器( 192.168.0.1 和 192.168.0.2 ),都利用默认端口 11211 :
mysql> SELECT memc_servers_set('192.168.0.1:11211,192.168.0.2:11211');
设置装备摆设完成后,就可以举行数据的写入和读取的操纵了,下面我们建立了一个姑且表用来先容经由过程触发器更新 Memcached 的扼要步调:
建立测试表:
mysql> create table test3 (
-> id int(3) not null,
-> name varchar(64) not null default '',
-> primary key (id)
-> );
Query OK, 0 rows affected (0.17 sec)
给自增变量赋初值:
mysql> select memc_set('id:sequence', 0);
+----------------------------+
| memc_set('id:sequence', 0) |
+----------------------------+
| 0 |
+----------------------------+
1 row in set (0.00 sec)
mysql>
在测试表上建立 insert 触发器:
mysql> DELIMITER |
mysql>
mysql> DROP TRIGGER IF EXISTS test3_insert;
-> CREATE TRIGGER test3_insert
-> BEFORE INSERT ON test3
-> FOR EACH ROW BEGIN
-> SET NEW.id= memc_increment('id:sequence');
-> SET @mm= memc_set(concat('id:',NEW.id), NEW.name);
-> END |
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> DELIMITER ;
mysql>
插入记实:
mysql> insert into test3 (name) values ('memcached');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test3 (name) values ('test');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test3 (name) values ('mysql');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test3;
+----+-----------+
| id | name |
+----+-----------+
| 1 | memcached |
| 2 | test |
| 3 | mysql |
+----+-----------+
3 rows in set (0.00 sec)
拜候 Memcached 可以获得已经经由过程触发器写入缓存的记实:
mysql> select memc_get('id:1');
+------------------+
| memc_get('id:1') |
+------------------+
| memcached |
+------------------+
1 row in set (0.00 sec)
mysql> select memc_get('id:2');
+------------------+
| memc_get('id:2') |
+------------------+
| test |
+------------------+
1 row in set (0.00 sec)
mysql> select memc_get('id:3');
+------------------+
| memc_get('id:3') |
+------------------+
| mysql |
+------------------+
1 row in set (0.00 sec)

你可能感兴趣的:(mysql,数据库,数据,memcached,映射)