目錄html
使用mysql2redis能夠很是便捷的將mysql中的數據導出到redis中去, 一般是須要一個select語句便可實現。java
// 安裝apr + apr-util $ wget http://mirror.bit.edu.cn/apache//apr/apr-1.5.1.tar.gz $ tar zxvf apr-1.5.1.tar.gz $ ./configure --prefix=/usr/local/apr $ make $ make install $ wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.4.tar.gz $ cd apr-util-1.5.4 $ ./configure --with-apr=/usr/local/apr $ make $ make install // 安裝hiredis, redis的C語言client $ git clone https://github.com/redis/hiredis $ cd hiredis $ make $ make install $ git clone https://github.com/dawnbreaks/mysql2redis.git $ cd mysql2redis $ make $ cp lib_mysqludf_redis_v2.so /usr/lib64/mysql/plugin/
CREATE FUNCTION redis_servers_set_v2 RETURNS int SONAME "lib_mysqludf_redis_v2.so"; CREATE FUNCTION redis_command_v2 RETURNS int SONAME "lib_mysqludf_redis_v2.so"; CREATE FUNCTION free_resources RETURNS int SONAME "lib_mysqludf_redis_v2.so";
若是出現如下異常信息,則關閉SELINUX
ERROR 1126 (HY000): Can't open shared library 'lib_mysqludf_redis_v2.so' (errno: 2 libhiredis.so.0.12: failed to map segment from shared object: Permission denied)mysql關閉selinux
// 臨時關閉
$ setenforce 0linux//永久關閉
cat /etc/selinux/config
SELINUX=disabledgit
// mysql // 設置redis服務器地址 mysql> select redis_servers_set_v2("127.0.0.1",6379); mysql> select * from user_info; +----+------------+------+-------------------+---------+ | id | NAME | age | email | addr | +----+------------+------+-------------------+---------+ | 1 | Troy Zhang | 30 | java-koma@163.com | ChengDu | +----+------------+------+-------------------+---------+ // 將user_info表的數據更新到redis中 mysql> select redis_command_v2("hmset", concat("user_info:", id), 'name', name, 'age', age, 'email', email, 'addr', addr) from user_info; // redis 127.0.0.1:6379> keys * 1) "user_info:1" 127.0.0.1:6379> hgetall user_info:1 1) "name" 2) "Troy Zhang" 3) "age" 4) "\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 5) "email" 6) "java-koma@163.com" 7) "addr" 8) "ChengDu"
https://github.com/dawnbreaks/mysql2redisgithub
個人另一篇文章:
Gearman + Nodejs + MySQL UDF異步實現 MySQL 到 Redis 的數據同步redis