redis主从同步,redis集群,redis+mysql+gearman

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都 支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排 序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文 件,并且在此基础上实现了master-slave(主从)同步。

Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。 [1] 

Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同 步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机 制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。

目前,Vmware在资助着redis项目的开发和维护。

 

Redis 主从同步

Server4,server5,server6:

[root@server4 mnt]# tar zxf redis-4.0.1.tar.gz 

[root@server4 mnt]# cd redis-4.0.1

[root@server4 redis-4.0.1]# yum install gcc -y

[root@server4 redis-4.0.1]# make

[root@server4 redis-4.0.1]# make install

[root@server4 redis-4.0.1]# cd utils/

[root@server4 utils]# ./install_server.sh 

[root@server4 utils]# netstat -antlp

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      4730/redis-server 1 

[root@server4 utils]# cd /etc/redis/

[root@server4 redis]# vim 6379.conf 

bind 0.0.0.0

[root@server4 redis]# /etc/init.d/redis_6379 restart

[root@server4 redis]# netstat -antlp

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      4742/redis-server 0 

 

Server5,server6:

[root@server5 redis]# vim /etc/redis/6379.conf 

slaveof 172.25.20.4 6379

[root@server5 redis]# /etc/init.d/redis_6379 restart

 

测试:

Server4:

[root@server4 redis]# redis-cli 

127.0.0.1:6379> set name westos

OK

127.0.0.1:6379> get name

"westos"

 

Server5,server6:

[root@server5 redis]# redis-cli 

127.0.0.1:6379> get name

"westos"

[root@server6 ~]# redis-cli 

127.0.0.1:6379> get name

"westos"

Redis主从同步,当主节点down掉,会选举产生新的主节点

 

Server4:

[root@server4 etc]# cd /mnt/redis-4.0.1

[root@server4 redis-4.0.1]# cp sentinel.conf /etc/redis/

[root@server4 redis-4.0.1]# cd /etc/redis/

[root@server4 redis]# vim sentinel.conf 

sentinel monitor mymaster 172.25.20.4 6379 2
protected-mode no

[root@server4 redis]# scp sentinel.conf server5:/etc/redis/

[root@server4 redis]# scp sentinel.conf server6:/etc/redis/

[root@server4 redis]# redis-sentinel /etc/redis/sentinel.conf 

 

Server5,server6:

[root@server5 redis]# redis-sentinel /etc/redis/sentinel.conf 

[root@server6 ~]# redis-sentinel /etc/redis/sentinel.conf 

测试:

Server4:

[root@server4 ~]# redis-cli 

127.0.0.1:6379> SHUTDOWN

not connected> 

 

Redis集群

Server4:

[root@server4 6379]# /etc/init.d/redis_6379 stop

Stopping ...

[root@server4 mnt]# cd /usr/local/

[root@server4 local]# mkdir cluster

[root@server4 local]# cd cluster/

[root@server4 cluster]# mkdir 700{1..6}

[root@server4 cluster]# cd 7001/

[root@server4 7001]# vim redis.conf

port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes

[root@server4 7001]# cd ..

[root@server4 cluster]# cp 7001/redis.conf 7002

[root@server4 cluster]# cp 7001/redis.conf 7003

[root@server4 cluster]# cp 7001/redis.conf 7004

[root@server4 cluster]# cp 7001/redis.conf 7005

[root@server4 cluster]# cp 7001/redis.conf 7006

分别修改每个配置文件的端口号

 

[root@server4 cluster]# cd 7001/

[root@server4 7001]# redis-server redis.conf 

 

[root@server4 7001]# cd ..

[root@server4 cluster]# cd 7002

[root@server4 7002]# redis-server redis.conf 

 

[root@server4 7002]# cd ..

[root@server4 cluster]# cd 7003

[root@server4 7003]# redis-server redis.conf 

 

[root@server4 7003]# cd ..

[root@server4 cluster]# cd 7004

[root@server4 7004]# redis-server redis.conf 

 

[root@server4 7004]# cd ..

[root@server4 cluster]# cd 7005

[root@server4 7005]# redis-server redis.conf 

 

[root@server4 7005]# cd ..

[root@server4 cluster]# cd 7006

[root@server4 7006]# redis-server redis.conf 

 

[root@server4 7006]# cd /mnt/

[root@server4 redis-4.0.1]# cd src/

[root@server4 src]# cp redis-trib.rb /usr/local/bin

[root@server4 mnt]# yum install ruby-2.2.3-1.el6.x86_64.rpm rubygems-1.3.7-5.el6.noarch.rpm libyaml-0.1.3-4.el6_6.x86_64.rpm -y

[root@server4 mnt]# gem install --local redis-4.0.1.gem 
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 1 seconds
1 gem installed
[root@server4 mnt]# redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: 0d0662f07246fa64e0a1171d9d829e3ec7367b38 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: acfd67b2d488fb7c20801300a80681c75f4bf2df 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: 742e41d5701993f14e4dc9939cbb2c0c77da9c78 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: 7bc1eade3c74cb70802dbe613287ae3bd42a5031 127.0.0.1:7004
   replicates 0d0662f07246fa64e0a1171d9d829e3ec7367b38
S: 28ecd3852953a59852bd617a6efc811c885d3b0c 127.0.0.1:7005
   replicates acfd67b2d488fb7c20801300a80681c75f4bf2df
S: dd9549014d5627188ef775e5c46b712759185a67 127.0.0.1:7006
   replicates 742e41d5701993f14e4dc9939cbb2c0c77da9c78
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 0d0662f07246fa64e0a1171d9d829e3ec7367b38 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 28ecd3852953a59852bd617a6efc811c885d3b0c 127.0.0.1:7005
   slots: (0 slots) slave
   replicates acfd67b2d488fb7c20801300a80681c75f4bf2df
M: 742e41d5701993f14e4dc9939cbb2c0c77da9c78 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: acfd67b2d488fb7c20801300a80681c75f4bf2df 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: dd9549014d5627188ef775e5c46b712759185a67 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 742e41d5701993f14e4dc9939cbb2c0c77da9c78
S: 7bc1eade3c74cb70802dbe613287ae3bd42a5031 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 0d0662f07246fa64e0a1171d9d829e3ec7367b38
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试:

7001:

[root@server4 mnt]# redis-cli -c -p 7001

127.0.0.1:7001> set name westos

-> Redirected to slot [5798] located at 127.0.0.1:7002

OK

127.0.0.1:7002> get name

"westos"

 

7002:

[root@server4 mnt]# redis-cli -c -p 7002

127.0.0.1:7002> get name

"westos"

 

7003:

[root@server4 mnt]# redis-cli -c -p 7003

127.0.0.1:7003> get name

-> Redirected to slot [5798] located at 127.0.0.1:7002

"westos"

 

关闭7002:

[root@server4 mnt]# redis-cli -p 7002

127.0.0.1:7002> SHUTDOWN

not connected> 

[root@server4 mnt]# redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 0d0662f07246fa64e0a1171d9d829e3ec7367b38 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 28ecd3852953a59852bd617a6efc811c885d3b0c 127.0.0.1:7005
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
M: 742e41d5701993f14e4dc9939cbb2c0c77da9c78 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: dd9549014d5627188ef775e5c46b712759185a67 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 742e41d5701993f14e4dc9939cbb2c0c77da9c78
S: 7bc1eade3c74cb70802dbe613287ae3bd42a5031 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 0d0662f07246fa64e0a1171d9d829e3ec7367b38
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

7005成为master,集群正常工作

关闭7005:

 

[root@server4 mnt]# redis-cli -p 7005

127.0.0.1:7005> SHUTDOWN

not connected> 

 

[root@server4 mnt]# redis-cli -c -p 7001

127.0.0.1:7001> get name

(error) CLUSTERDOWN The cluster is down

集群不能正常工作

 

Redis+mysql

 

Client (r/w) -> redis -> 钩子 -> mysqld

Client r -> redis  w -> mysqld

 

[root@server4 7005]# yum install -y mysql-server

[root@server4 redis]# rpm -ivh nginx-1.8.0-1.el6.ngx.x86_64.rpm 

[root@server4 redis]# yum install -y php-5.3.3-38.el6.x86_64.rpm php-cli-5.3.3-38.el6.x86_64.rpm php-common-5.3.3-38.el6.x86_64.rpm

 php-devel-5.3.3-38.el6.x86_64.rpm php-fpm-5.3.3-38.el6.x86_64.rpm php-gd-5.3.3-38.el6.x86_64.rpm php-mbstring-5.3.3-38.el6.x86_64.rpm php-mysql-5.3.3-38.el6.x86_64.rpm php-pdo-5.3.3-38.el6.x86_64.rpm

[root@server4 redis]# vim /etc/php.ini

[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = Asia/Shanghai

[root@server4 php.d]# /etc/init.d/php-fpm start

[root@server4 redis]# cd /etc/

[root@server4 etc]# vim php-fpm.d/www.conf 

; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

[root@server4 etc]# /etc/init.d/php-fpm restart

[root@server4 etc]# cd /etc/nginx/

[root@server4 nginx]# cd conf.d/

[root@server4 conf.d]# vim default.conf 


  location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
}

[root@server4 conf.d]# cd /usr/share/nginx/html/

[root@server4 html]# vim index.php

[root@server4 html]# /etc/init.d/nginx start

redis主从同步,redis集群,redis+mysql+gearman_第1张图片

[root@server4 redis]# cp test.php /usr/share/nginx/html/

[root@server4 html]# vim test.php 

connect('172.25.20.6',6379) or die ("could net connect redis server");
  #      $query = "select * from test limit 9";
        $query = "select * from test";
        for ($key = 1; $key < 10; $key++)
        {
                if (!$redis->get($key))
                {
                        $connect = mysql_connect('172.25.20.4','redis','westos');
                        mysql_select_db(test);
                        $result = mysql_query($query);
                        //如果没有找到$key,就将该查询sql的结果缓存到redis
                        while ($row = mysql_fetch_assoc($result))
                        {
                                $redis->set($row['id'],$row['name']);
                        }
                        $myserver = 'mysql';
                        break;
                }
                else
                {
                        $myserver = "redis";
                        $data[$key] = $redis->get($key);
                }
        }
        echo $myserver;
        echo "
"; for ($key = 1; $key < 10; $key++) { echo "number is $key"; echo "
"; echo "name is $data[$key]"; echo "
"; } ?>

[root@server4 redis]# yum install unzip -y

[root@server4 redis]# unzip phpredis-master.zip 

[root@server4 redis]# cd phpredis-master

[root@server4 phpredis-master]# phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

[root@server4 phpredis-master]# ./configure 

[root@server4 phpredis-master]# make && make install

[root@server4 modules]# cd /etc/php.d/

[root@server4 php.d]# cp mysql.ini redis.ini

[root@server4 php.d]# vim redis.ini 

extension=redis.so

[root@server4 html]# /etc/init.d/mysqld start

[root@server4 html]# /etc/init.d/mysqld start

[root@server4 php.d]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all on test.* to redis@'%' identified by 'westos';
Query OK, 0 rows affected (0.00 sec)

mysql> Bye

vim test.sql

use test;
CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');

#DELIMITER $$
#CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
#    SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`));
#  END$$
#DELIMITER ;

[root@server4 redis]# mysql < test.sql 

[root@server4 redis]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

mysql> select * from test;
+----+-------+
| id | name  |
+----+-------+
|  1 | test1 |
|  2 | test2 |
|  3 | test3 |
|  4 | test4 |
|  5 | test5 |
|  6 | test6 |
|  7 | test7 |
|  8 | test8 |
|  9 | test9 |
+----+-------+
9 rows in set (0.00 sec)

mysql> Bye

redis主从同步,redis集群,redis+mysql+gearman_第2张图片

此时当更新数据库的数据后,redis并不会更新

 

配置 gearman 实现数据同步

Gearman 是一个支持分布式的任务分发框架:

Gearman Job Server: Gearman 核心程序,需要编译安装并以守护进程形式运行在后台。

Gearman Client:可以理解为任务的请求者。

Gearman Worker:任务的真正执行者,一般需要自己编写具体逻辑并通过守护进程方式

运行,Gearman Worker 接收到 Gearman Client 传递的任务内容后,会按顺序处理。

大致流程:下面要编写的 mysql 触发器,就相当于 Gearman 的客户端。修改表,插入表就相当于直接下发任务。然后通过 lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式,然后在通过 gearman-mysql-udf 插件将任务加入到 Gearman 的任务队列中,最后通过

redis_worker.php,也就是 Gearman 的 worker 端来完成 redis 数据库的更新。

 

[root@server4 redis]# yum install gearmand-1.1.8-2.el6.x86_64.rpm libgearman-1.1.8-2.el6.x86_64.rpm libgearman-devel-1.1.8-2.el6.x86_64.rpm  libevent-1.4.13-4.el6.x86_64.rpm libevent-devel-1.4.13-4.el6.x86_64.rpm libevent-doc-1.4.13-4.el6.noarch.rpm libevent-headers-1.4.13-4.el6.noarch.rpm tokyocabinet-1.4.33-6.el6.x86_64.rpm -y

[root@server4 redis]# service gearmand start

[root@server4 redis]# netstat -natlp 

tcp        0      0 0.0.0.0:4730                0.0.0.0:*                   LISTEN      7787/gearmand       

[root@server4 redis]# yum install unzip -y

[root@server4 redis]# unzip lib_mysqludf_json-master.zip 

[root@server4 redis]# cd lib_mysqludf_json-master

[root@server4 lib_mysqludf_json-master]# yum install -y mysql-devel

[root@server4 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c

[root@server4 lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/

[root@server4 lib_mysqludf_json-master]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from mysql.func;
+-------------+-----+----------------------+----------+
| name        | ret | dl                   | type     |
+-------------+-----+----------------------+----------+
| json_object |   0 | lib_mysqludf_json.so | function |
+-------------+-----+----------------------+----------+
1 row in set (0.00 sec)

mysql> Bye

[root@server4 redis]# tar zxf gearman-mysql-udf-0.6.tar.gz 

[root@server4 redis]# cd gearman-mysql-udf-0.6

[root@server4 gearman-mysql-udf-0.6]# ./configure --with-mysql --libdir=/usr/lib64/mysql/plugin/

[root@server4 gearman-mysql-udf-0.6]# make && make install

[root@server4 plugin]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE FUNCTION gman_do_background RETURNS STRING SONAME
    -> 'libgearman_mysql_udf.so';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from mysql.func;
+--------------------+-----+-------------------------+----------+
| name               | ret | dl                      | type     |
+--------------------+-----+-------------------------+----------+
| json_object        |   0 | lib_mysqludf_json.so    | function |
| gman_do_background |   0 | libgearman_mysql_udf.so | function |
| gman_servers_set   |   0 | libgearman_mysql_udf.so | function |
+--------------------+-----+-------------------------+----------+
3 rows in set (0.00 sec)

mysql> SELECT gman_servers_set('127.0.0.1:4730');
+------------------------------------+
| gman_servers_set('127.0.0.1:4730') |
+------------------------------------+
| 127.0.0.1:4730                     |
+------------------------------------+
1 row in set (0.00 sec)

mysql> Bye

vim test.sql

use test;
#CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');

DELIMITER $$
CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
    SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`));
  END$$
DELIMITER ;
[root@server4 redis]# mysql < test.sql 

mysql> SHOW TRIGGERS FROM test;
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| Trigger     | Event  | Table | Statement                                                                                                      | Timing | Created | sql_mode | Definer        | character_set_client | collation_connection | Database Collation |
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| datatoredis | UPDATE | test  | BEGIN
    SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`)); 
  END | AFTER  | NULL    |          | root@localhost | latin1               | latin1_swedish_ci    | latin1_swedish_ci  |
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

[root@server4 redis]# tar zxf gearman-1.1.2.tgz

[root@server4 redis]# cd gearman-1.1.2

[root@server4 gearman-1.1.2]# phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

[root@server4 gearman-1.1.2]# ./configure --with-php-config=/usr/bin/php-config

[root@server4 gearman-1.1.2]# make && make install

[root@server4 php.d]# cp redis.ini gearman.ini

[root@server4 php.d]# vim gearman.ini 

extension=gearman.so

[root@server4 php.d]# /etc/init.d/php-fpm reload

[root@server4 redis]# cp worker.php /usr/local/

[root@server4 redis]# cd /usr/local/

[root@server4 local]# vim worker.php 

addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');

$redis = new Redis();
$redis->connect('172.25.20.6', 6379);

while($worker->work());
function syncToRedis($job)
{
        global $redis;
        $workString = $job->workload();
        $work = json_decode($workString);
        if(!isset($work->id)){
                return false;
        }
        $redis->set($work->id, $work->name);
}
?>

[root@server4 local]# nohup php /usr/local/worker.php &

[1] 13142

测试:

Server4:

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update test set name='westos' where id=1;
Query OK, 1 row affected (0.50 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Server6:

 

[root@server6 ~]# redis-cli 

127.0.0.1:6379> get 1

"westos"

redis主从同步,redis集群,redis+mysql+gearman_第3张图片

你可能感兴趣的:(redis主从同步,redis集群,redis+mysql+gearman)