文章目錄
將MySQL資料對應到Memcached中2月 6th, 2009
作者:張立冰
出處:http://www.libing.name/2009/02/06/mysql-map-data-to-memcachedmysql-map-data-to-memcached.html
差不多在一年前,寫過一篇文章介紹將MySQL資料對應到Memcached,當時MySQL和Memcached Functions for MySQL都還不夠成熟,時過一年,Memcached Functions for MySQL升級到了0.8版本,而MySQL也發布了GA版本,加上很多朋友反應前一篇文章中的實現他們因種種原因沒能成功,於是便有了這篇文章,就當是上一篇文章的升級版本吧。
測試環境在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 <sql/install_functions.sql
註:如果對這些UDFs不熟悉或者不懂,可進行源碼目錄參看README,裡邊有相應的說明。
至此,相關軟體的編譯和安裝完成,進行測試,我們要達到的目的是當MySQL有新記錄插入時,同時插入到Memcached中,當記錄更新時同步更新Memcached中的記錄,刪除時同時也刪除Memcached相關的記錄,為此建立三個觸發器來實現,如果對MySQL的觸發程式不熟悉可以參考MySQL手冊第21章,下面SQL中的memcached為需要操作的表名,SQL如下:
#插入資料時插入Memcachedcreate trigger mysqlmmci after insert on memcached for each row set @tmp = memc_set(NEW.key, NEW.value);#更新記錄時更新Memcachedcreate 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 6Server 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> quitBye
Memcache查看時的記錄:
[root@localhost ~]#telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get keyiVALUE keyi 0 6valueiENDget keyuVALUE keyu 0 6valueuENDget keydVALUE keyd 0 6valuedENDget keyuVALUE keyu 0 6updateENDget keydENDquitConnection closed by foreign host.
至此,我們基本實現的將MySQL的資料同步到Memcached中,效能暫時還沒有測試,當然上面只是簡單的實現的資料對應的功能,如果在實現的生產環境中,則需要考慮名字空間,高可靠性的問題,這些都是可以通過資料庫名-表名-關鍵字的方面能達到KEY唯一的目的,而高可靠性則是一個比較大的問題。
您可能還喜歡