欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Redis集群

程序员文章站 2022-05-21 23:52:31
...

安装包:

nginx-1.8.0-1.el6.ngx.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

实验环境:

应用平台:    server3
Redies端:   server4
Mysql端:    server5
##杀死不必要的进程
[aaa@qq.com 7001]# ps ax
.........
 1254 pts/0    S      0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql
 1547 pts/0    Sl     0:08 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/my
 1595 pts/0    T      0:00 mysql -p
 1888 ?        Ssl    0:09 redis-server *:7001 [cluster]
 1920 ?        Ssl    0:08 redis-server *:7003 [cluster]
 1939 ?        Ssl    0:08 redis-server *:7006 [cluster]
 1970 ?        S      0:00 pickup -l -t fifo -u
 1979 ?        Ssl    0:02 redis-server *:7004 [cluster]
 2001 pts/0    R+     0:00 ps -ax
[aaa@qq.com 7001]# kill -9 1888
[aaa@qq.com 7001]# kill -9 1920
[aaa@qq.com 7001]# kill -9 1939
[aaa@qq.com 7001]# kill -9 1970
[aaa@qq.com 7001]# kill -9 1979
[aaa@qq.com 7001]# ps ax
.........
1213 pts/0    Ss     0:00 -bash
 1254 pts/0    S      0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql
 1547 pts/0    Sl     0:08 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/my
 2003 ?        S      0:00 pickup -l -t fifo -u
 2005 pts/0    R+     0:00 ps ax
[aaa@qq.com 7001]# cd
[aaa@qq.com ~]# ls
libyaml-0.1.3-4.el6_6.x86_64.rpm  redis-4.0.8.tar.gz
nohup.out                         ruby-2.2.3-1.el6.x86_64.rpm
redis-4.0.1.gem                   rubygems-1.3.7-5.el6.noarch.rpm
redis-4.0.8
[aaa@qq.com ~]# rm -rf *
[aaa@qq.com ~]# ls
[aaa@qq.com ~]# rpm -qa|grep htpd
[aaa@qq.com ~]# rpm -qa|grep php
php-5.3.3-26.el6.x86_64
php-common-5.3.3-26.el6.x86_64
php-cli-5.3.3-26.el6.x86_64
[aaa@qq.com ~]# ls
redis
[aaa@qq.com ~]# cd redis/
[aaa@qq.com redis]# ls
codis-release3.2.zip                       php-cli-5.3.3-38.el6.x86_64.rpm
gearman-1.1.2.tgz                          php-common-5.3.3-38.el6.x86_64.rpm
gearmand-1.1.8-2.el6.x86_64.rpm            php-devel-5.3.3-38.el6.x86_64.rpm
gearman-mysql-udf-0.6.tar.gz               php-fpm-5.3.3-38.el6.x86_64.rpm
go1.8.linux-amd64.tar.gz                   php-gd-5.3.3-38.el6.x86_64.rpm
jemalloc-3.6.0-1.el6.x86_64.rpm            php-mbstring-5.3.3-38.el6.x86_64.rpm
jemalloc-devel-3.6.0-1.el6.x86_64.rpm      php-mysql-5.3.3-38.el6.x86_64.rpm
libevent-1.4.13-4.el6.x86_64.rpm           php-pdo-5.3.3-38.el6.x86_64.rpm
libevent-devel-1.4.13-4.el6.x86_64.rpm     phpredis-master.zip
libevent-doc-1.4.13-4.el6.noarch.rpm       redis-3.0.2.tar.gz
libevent-headers-1.4.13-4.el6.noarch.rpm   redis-4.0.1.gem
libgearman-1.1.8-2.el6.x86_64.rpm          redis-4.0.1.tar.gz
libgearman-devel-1.1.8-2.el6.x86_64.rpm    redis-4.0.8.tar.gz
lib_mysqludf_json-master.zip               rhel6 Redis.pdf
libyaml-0.1.3-4.el6_6.x86_64.rpm           ruby-2.2.3-1.el6.x86_64.rpm
nginx-1.10.2-1.el6.x86_64.rpm              rubygems-1.3.7-5.el6.noarch.rpm
nginx-1.8.0-1.el6.ngx.x86_64.rpm           test.php
nginx-all-modules-1.10.2-1.el6.noarch.rpm  test.sql
nginx-filesystem-1.10.2-1.el6.noarch.rpm   tokyocabinet-1.4.33-6.el6.x86_64.rpm
php-5.3.3-38.el6.x86_64.rpm                worker.php
[aaa@qq.com redis]# yum install -y nginx-1.8.0-1.el6.ngx.x86_64.rpm php-*
[root@server4 ~]# vim /etc/redis/6379.conf 
#################
注释:#slaveof 172.25.52.5 6379

Redis集群

[aaa@qq.com ~]# /etc/init.d/redis_6379  restart
Stopping ...
Redis stopped
Starting Redis server...
[aaa@qq.com ~]# redis-cli 
127.0.0.1:6379> info
# Replication
role:master
connected_slaves:0
master_replid:9dc2bc10a70ef45a5224c7bd23dc7c7a48ae2153
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
 [root@server3 redis]# vim /etc/php.ini
######################
946 date.timezone = Asia/Shangha

Redis集群

[root@server3 redis]# cd /etc/php-fpm.d/
[root@server3 php-fpm.d]# id nginx
uid=498(nginx) gid=499(nginx) groups=499(nginx)
[root@server3 php-fpm.d]# ls
www.conf
[root@server3 php-fpm.d]# vim www.conf 
#################
39 user = nginx
41 group = nginx

Redis集群

[root@server3 php.d]# /etc/init.d/php-fpm  start
Starting php-fpm:                                          [  OK  ]
[root@server3 php.d]# 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:22                  0.0.0.0:*                   LISTEN      1011/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1098/master         
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      2091/php-fpm        
tcp        0      0 172.25.52.3:36063           172.25.52.4:3306            ESTABLISHED 1547/mysqld         
tcp        0      0 172.25.52.3:22              172.25.52.250:59876         ESTABLISHED 1211/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      1011/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1098/master         
tcp        0      0 :::11111                    :::*                        LISTEN      1187/ricci          
tcp        0      0 :::3306                     :::*                        LISTEN      1547/mysqld         

[root@server3 php.d]# cd /etc/nginx/conf.d
[root@server3 conf.d]# ls
default.conf  example_ssl.conf
[root@server3 conf.d]# vim default.conf
######################
添加:index.php
  8 location / {
  9         root   /usr/share/nginx/html;
 10         index  index.php  index.html index.htm;
 11       }
 取消注释:
 30     location ~ \.php$ {
 31         root           html;
 32         fastcgi_pass   127.0.0.1:9000;
 33         fastcgi_index  index.php;
 34         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_scr    ipt_name;

Redis集群
Redis集群

[root@server3 conf.d]# /etc/init.d/nginx  start
Starting nginx:                                            [  OK  ]
[root@server3 conf.d]# netstat -antpl
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:80                  0.0.0.0:*                   LISTEN      2113/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1011/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1098/master         
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      2091/php-fpm        
tcp        0      0 172.25.52.3:36063           172.25.52.4:3306            ESTABLISHED 1547/mysqld         
tcp        0      0 172.25.52.3:22              172.25.52.250:59876         ESTABLISHED 1211/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      1011/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1098/master         
tcp        0      0 :::11111                    :::*                        LISTEN      1187/ricci          
tcp        0      0 :::3306                     :::*                        LISTEN      1547/mysqld

[root@server3 conf.d]# cd /usr/share/nginx/html/
[root@server3 html]# ls
50x.html  index.html
[root@server3 html]# vim index.php
####################
<?php
phpinfo()
?>

Redis集群

测试:
输入172.25.52.3
Redis集群

[aaa@qq.com conf.d]# php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
ereg
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
json
libxml
mbstring
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
xml
zip
zlib

[Zend Modules]

[aaa@qq.com conf.d]# cd 
[aaa@qq.com ~]# ls
redis
[aaa@qq.com ~]# cd redis/
[aaa@qq.com redis]# cat test.php
<?php
        $redis = new Redis();
        $redis->connect('127.0.0.1',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('127.0.0.1','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 "<br>";
        for ($key = 1; $key < 10; $key++)
        {
                echo "number is <b><font color=#FF0000>$key</font></b>";

                echo "<br>";

                echo "name is <b><font color=#FF0000>$data[$key]</font></b>";

                echo "<br>";
        }
?>
[aaa@qq.com redis]# cp test.php  /usr/share/nginx/html/index.php 
cp: overwrite `/usr/share/nginx/html/index.php'? yes
[aaa@qq.com redis]# cd /usr/share/nginx/html/
[aaa@qq.com html]# ls
50x.html  index.html  index.php
[aaa@qq.com html]# vim index.php 
####################
更改:
3  $redis->connect('172.25.52.4',6379) or die ("could net connect re    dis server");   ##redis端的ip

10 $connect = mysql_connect('172.25.52.5','redis','westos');     ##mysql端的ip

Redis集群

[aaa@qq.com html]# cd
[aaa@qq.com ~]# cd redis/
[aaa@qq.com redis]# yum install unzip -y
[aaa@qq.com redis]# unzip phpredis-master.zip  
[aaa@qq.com redis]# cd phpredis-master
[aaa@qq.com phpredis-master]# ls
common.h   debian.control  mkdeb-apache2.sh  redis_session.c
config.m4  igbinary        php_redis.h       redis_session.h
CREDITS    library.c       README.markdown   serialize.list
debian     library.h       redis.c           tests
[aaa@qq.com phpredis-master]# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[aaa@qq.com phpredis-master]# ./configure
[aaa@qq.com phpredis-master]# make && make install
[aaa@qq.com phpredis-master]# cd /usr/lib64/php/modules/
[aaa@qq.com modules]# ls
curl.so      json.so      mysql.so      pdo_sqlite.so  sqlite3.so
fileinfo.so  mbstring.so  pdo_mysql.so  phar.so        zip.so
gd.so        mysqli.so    pdo.so        redis.so

[aaa@qq.com modules]# cd /etc/php.d/
[aaa@qq.com php.d]# ls
curl.ini      json.ini      mysql.ini      pdo_sqlite.ini  zip.ini
fileinfo.ini  mbstring.ini  pdo.ini        phar.ini
gd.ini        mysqli.ini    pdo_mysql.ini  sqlite3.ini
[aaa@qq.com php.d]# cp mysql.ini  redis.ini
[aaa@qq.com php.d]# vim redis.ini 
#############
extension=redis.so

Redis集群

[aaa@qq.com php.d]# /etc/init.d/php-fpm  reload
Reloading php-fpm: [12-Aug-2018 14:18:35] NOTICE: configuration file /etc/php-fpm.conf test is successful
                                                           [  OK  ]
[aaa@qq.com php.d]# php -m |grep redis
Redis
[aaa@qq.com ~]# /etc/init.d/redis_6379  stop
Stopping ...
Redis stopped
[aaa@qq.com ~]# ls
mha4mysql-node-0.56-0.el6.noarch.rpm  redis-4.0.8  redis-4.0.8.tar.gz
[aaa@qq.com ~]# cd /var/lib/mysql
[aaa@qq.com mysql]# ls
auto.cnf         ibdata1             private_key.pem
binlog.000001    ib_logfile0         public_key.pem
binlog.000002    ib_logfile1         server5-relay-bin.000001
binlog.index     ibtmp1              server5-relay-bin.000002
ca-key.pem       mysql               server5-relay-bin.index
ca.pem           mysqld_safe.pid     server-cert.pem
client-cert.pem  mysql.sock          server-key.pem
client-key.pem   mysql.sock.lock     sys
ib_buffer_pool   performance_schema  test
[aaa@qq.com mysql]# rm -fr *
[aaa@qq.com mysql]# ls
[aaa@qq.com mysql]# cd
[aaa@qq.com ~]# rpm -qa|grep mysql
mysql-community-common-5.7.17-1.el6.x86_64
mysql-community-client-5.7.17-1.el6.x86_64
mysql-community-libs-compat-5.7.17-1.el6.x86_64
mysql-community-libs-5.7.17-1.el6.x86_64
mysql-community-server-5.7.17-1.el6.x86_64
mha4mysql-node-0.56-0.el6.noarch
[aaa@qq.com ~]# rpm -e --nodeps `rpm -qa|grep mysql`
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
[aaa@qq.com ~]# yum install -y mysql-server
[aaa@qq.com ~]# ls
mha4mysql-node-0.56-0.el6.noarch.rpm  redis-4.0.8  redis-4.0.8.tar.gz
[aaa@qq.com ~]# cd /var/lib/mysql/
[aaa@qq.com mysql]# ls
ib_buffer_pool
[aaa@qq.com mysql]# /etc/init.d/mysqld   start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h server5 password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[aaa@qq.com mysql]# cd 
##登陆无需密码
[aaa@qq.com ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
##授权
mysql> grant all on test.* to aaa@qq.com'%' identified by 'westos';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
[root@server3 ~]# cd redis/
[root@server3 redis]# ls
[root@server3 redis]# scp test.sql server5:
test.sql                               100%  493     0.5KB/s   00:00 
[aaa@qq.com ~]# ls
mha4mysql-node-0.56-0.el6.noarch.rpm  redis-4.0.8.tar.gz
redis-4.0.8                           test.sql
####查看文件内容
[aaa@qq.com ~]# vim test.sql 
[aaa@qq.com ~]# mysql < test.sql 

测试:
Redis集群
刷新网页之后:
Redis集群

[aaa@qq.com ~]# redis-cli 
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> get 3
"test3"
127.0.0.1:6379> get 4
"test4"
127.0.0.1:6379> get 5
"test5"
127.0.0.1:6379> get 6
"test6"
127.0.0.1:6379> get 7
"test7"
127.0.0.1:6379> get 8
"test8"
127.0.0.1:6379> get 9
"test9"
127.0.0.1:6379> get 10
(nil)
[aaa@qq.com ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
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> 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> update test set name='westos' where id=1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test ;
+----+--------+
| id | name   |
+----+--------+
|  1 | westos |
|  2 | test2  |
|  3 | test3  |
|  4 | test4  |
|  5 | test5  |
|  6 | test6  |
|  7 | test7  |
|  8 | test8  |
|  9 | test9  |
+----+--------+
9 rows in set (0.00 sec)
[aaa@qq.com ~]# redis-cli 
127.0.0.1:6379> get 1
"test1"

在网页上测试:
Redis集群
虽然mysql端的数据变动了 但由于redis端的数据并没有获取到变更信息
故网页上也无法获取到变更 因为网页来源于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@server3 redis]# ls
##安装 gearman 软件包
[root@server3 redis]# rpm -ivh gearmand-1.1.8-2.el6.x86_64.rpm 
warning: gearmand-1.1.8-2.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
error: Failed dependencies:
    libboost_program_options-mt.so.5()(64bit) is needed by gearmand-1.1.8-2.el6.x86_64
    libboost_system.so.5()(64bit) is needed by gearmand-1.1.8-2.el6.x86_64
    libevent-1.4.so.2()(64bit) is needed by gearmand-1.1.8-2.el6.x86_64
    libgearman.so.8()(64bit) is needed by gearmand-1.1.8-2.el6.x86_64
    libpq.so.5()(64bit) is needed by gearmand-1.1.8-2.el6.x86_64
    libtokyocabinet.so.8()(64bit) is needed by gearmand-1.1.8-2.el6.x86_64
[root@server3 redis]# yum install -y  gearmand-1.1.8-2.el6.x86_64.rpm libgearman-*  libevent-*
[root@server3 redis]# rpm -ivh gearmand-1.1.8-2.el6.x86_64.rpm
warning: gearmand-1.1.8-2.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                                                            (########################################### [100%]
    package gearmand-1.1.8-2.el6.x86_64 is already installed
##启动服务
[root@server3 redis]# /etc/init.d/gearmand  start
Starting gearmand:                                     [  OK  ]
[root@server3 redis]# netstat -antpl
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:80                  0.0.0.0:*                   LISTEN      2113/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1011/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1098/master         
tcp        0      0 0.0.0.0:4730                0.0.0.0:*                   LISTEN      4550/gearmand       
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      4486/php-fpm        
tcp        0      0 172.25.52.3:36063           172.25.52.4:3306            ESTABLISHED 1547/mysqld         
tcp        0      0 172.25.52.3:22              172.25.52.250:59876         ESTABLISHED 1211/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      1011/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1098/master         
tcp        0      0 :::4730 #               :::*                        LISTEN      4550/gearmand       
tcp        0      0 :::11111                    :::*                        LISTEN      1187/ricci          
tcp        0      0 :::3306                     :::*                        LISTEN      1547/mysqld         
[root@server3 redis]# scp gearman-mysql-udf-0.6.tar.gz  server5:
gearman-mysql-udf-0.6.tar.gz           100%  368KB 368.5KB/s   00:00    
[root@server3 redis]# scp lib_mysqludf_json-master.zip  server5:
lib_mysqludf_json-master.zip           100%   23KB  22.9KB/s   00:00    
[root@server5 ~]# ls
gearman-mysql-udf-0.6.tar.gz          redis-4.0.8
lib_mysqludf_json-master.zip          redis-4.0.8.tar.gz
mha4mysql-node-0.56-0.el6.noarch.rpm  test.sql
[root@server5 ~]# yum install unzip
[root@server5 ~]# ls
gearman-mysql-udf-0.6.tar.gz          redis-4.0.8
lib_mysqludf_json-master              redis-4.0.8.tar.gz
lib_mysqludf_json-master.zip          test.sql
mha4mysql-node-0.56-0.el6.noarch.rpm
[root@server5 ~]# cd lib_mysqludf_json-master
[root@server5 lib_mysqludf_json-master]# ls
lib_mysqludf_json.c     lib_mysqludf_json.so   README.md
lib_mysqludf_json.html  lib_mysqludf_json.sql
[root@server5 lib_mysqludf_json-master]# yum install gcc mysql-devel -y
[root@server5 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
[root@server5 lib_mysqludf_json-master]# ll
total 100
-rw-r--r-- 1 root root 25921 Feb 22  2013 lib_mysqludf_json.c
-rw-r--r-- 1 root root 20576 Feb 22  2013 lib_mysqludf_json.html
-rwxr-xr-x 1 root root 22223 Aug 12 15:04 lib_mysqludf_json.so
-rw-r--r-- 1 root root  1431 Feb 22  2013 lib_mysqludf_json.sql
-rw-r--r-- 1 root root 16416 Feb 22  2013 README.md
[root@server5 lib_mysqludf_json-master]# cd  /usr/lib64/mysql/plugin/
[root@server5 plugin]# ls
ha_archive.so        ha_blackhole.so.0.0.0  ha_federated.so.0
ha_archive.so.0      ha_example.so          ha_federated.so.0.0.0
ha_archive.so.0.0.0  ha_example.so.0        ha_innodb_plugin.so
ha_blackhole.so      ha_example.so.0.0.0    ha_innodb_plugin.so.0
ha_blackhole.so.0    ha_federated.so        ha_innodb_plugin.so.0.0.0
[root@server5 plugin]# cd -
/root/lib_mysqludf_json-master
[root@server5 lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
[root@server5 lib_mysqludf_json-master]# cd -
/usr/lib64/mysql/plugin
[root@server5 plugin]# ls
ha_archive.so          ha_example.so          ha_innodb_plugin.so
ha_archive.so.0        ha_example.so.0        ha_innodb_plugin.so.0
ha_archive.so.0.0.0    ha_example.so.0.0.0    ha_innodb_plugin.so.0.0.0
ha_blackhole.so        ha_federated.so        lib_mysqludf_json.so
ha_blackhole.so.0      ha_federated.so.0
ha_blackhole.so.0.0.0  ha_federated.so.0.0.0
[root@server5 plugin]# ll
total 1440
lrwxrwxrwx 1 root root      19 Aug 12 14:27 ha_archive.so -> ha_archive.so.0.0.0
lrwxrwxrwx 1 root root      19 Aug 12 14:27 ha_archive.so.0 -> ha_archive.so.0.0.0
-rwxr-xr-x 1 root root   54048 Aug  9  2013 ha_archive.so.0.0.0
lrwxrwxrwx 1 root root      21 Aug 12 14:27 ha_blackhole.so -> ha_blackhole.so.0.0.0
lrwxrwxrwx 1 root root      21 Aug 12 14:27 ha_blackhole.so.0 -> ha_blackhole.so.0.0.0
-rwxr-xr-x 1 root root   30288 Aug  9  2013 ha_blackhole.so.0.0.0
lrwxrwxrwx 1 root root      19 Aug 12 14:27 ha_example.so -> ha_example.so.0.0.0
lrwxrwxrwx 1 root root      19 Aug 12 14:27 ha_example.so.0 -> ha_example.so.0.0.0
-rwxr-xr-x 1 root root   33168 Aug  9  2013 ha_example.so.0.0.0
lrwxrwxrwx 1 root root      21 Aug 12 14:27 ha_federated.so -> ha_federated.so.0.0.0
lrwxrwxrwx 1 root root      21 Aug 12 14:27 ha_federated.so.0 -> ha_federated.so.0.0.0
-rwxr-xr-x 1 root root   58064 Aug  9  2013 ha_federated.so.0.0.0
lrwxrwxrwx 1 root root      25 Aug 12 14:27 ha_innodb_plugin.so -> ha_innodb_plugin.so.0.0.0
lrwxrwxrwx 1 root root      25 Aug 12 14:27 ha_innodb_plugin.so.0 -> ha_innodb_plugin.so.0.0.0
-rwxr-xr-x 1 root root 1258272 Aug  9  2013 ha_innodb_plugin.so.0.0.0
-rwxr-xr-x 1 root root   22223 Aug 12 15:07 lib_mysqludf_json.so ##

[root@server5 plugin]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
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 的模块目录:
mysql> show global variables like 'plugin_dir';
+---------------+-------------------------+
| Variable_name | Value                   |
+---------------+-------------------------+
| plugin_dir    | /usr/lib64/mysql/plugin |
+---------------+-------------------------+
1 row in set (0.00 sec)

mysql> create function json_object returns string soname 'lib_mysqludf_json.so';
Query OK, 0 rows affected (0.00 sec)

[root@server5 plugin]# cd 
[root@server5 ~]# ls
gearman-mysql-udf-0.6.tar.gz          redis-4.0.8
lib_mysqludf_json-master              redis-4.0.8.tar.gz
lib_mysqludf_json-master.zip          test.sql
mha4mysql-node-0.56-0.el6.noarch.rpm
[root@server5 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz 
[root@server5 ~]# cd gearman-mysql-udf-0.6
[root@server5 gearman-mysql-udf-0.6]# ls
aclocal.m4  config       configure.ac          m4           NEWS
AUTHORS     config.h.in  COPYING               Makefile.am  README
ChangeLog   configure    libgearman_mysql_udf  Makefile.in
##编译
[root@server5 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/
..........
configure: error: At least version 0.33 of libgearman is required for gearman-mysql-udf
[aaa@qq.com redis]# scp libgearman-* server5:
libgearman-1.1.8-2.el6.x86_64.rpm        100%   70KB  69.6KB/s   00:00    
libgearman-devel-1.1.8-2.el6.x86_64.rpm  100%  214KB 214.3KB/s   00:00    
[aaa@qq.com redis]# scp libevent-* server5:
libevent-1.4.13-4.el6.x86_64.rpm         100%   66KB  65.9KB/s   00:00    
libevent-devel-1.4.13-4.el6.x86_64.rpm   100%   74KB  73.9KB/s   00:00    
libevent-doc-1.4.13-4.el6.noarch.rpm     100%  194KB 193.8KB/s   00:00    
libevent-headers-1.4.13-4.el6.noarch.rpm 100%   30KB  29.8KB/s   00:00    
[aaa@qq.com gearman-mysql-udf-0.6]# cd 
[aaa@qq.com ~]# yum install libgearman-* -y
[aaa@qq.com ~]# cd gearman-mysql-udf-0.6
##没有报错即可
[aaa@qq.com gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/
[aaa@qq.com gearman-mysql-udf-0.6]# make && make install
[aaa@qq.com gearman-mysql-udf-0.6]# cd /usr/lib64/mysql/plugin/
[aaa@qq.com plugin]# ls
ha_archive.so          ha_federated.so.0
ha_archive.so.0        ha_federated.so.0.0.0
ha_archive.so.0.0.0    ha_innodb_plugin.so
ha_blackhole.so        ha_innodb_plugin.so.0
ha_blackhole.so.0      ha_innodb_plugin.so.0.0.0
ha_blackhole.so.0.0.0  libgearman_mysql_udf.la
ha_example.so          libgearman_mysql_udf.so
ha_example.so.0        libgearman_mysql_udf.so.0
ha_example.so.0.0.0    libgearman_mysql_udf.so.0.0.0
ha_federated.so        lib_mysqludf_json.so
[aaa@qq.com plugin]# ll
total 1556
lrwxrwxrwx 1 root root      19 Aug 12 14:27 ha_archive.so -> ha_archive.so.0.0.0
lrwxrwxrwx 1 root root      19 Aug 12 14:27 ha_archive.so.0 -> ha_archive.so.0.0.0
-rwxr-xr-x 1 root root   54048 Aug  9  2013 ha_archive.so.0.0.0
lrwxrwxrwx 1 root root      21 Aug 12 14:27 ha_blackhole.so -> ha_blackhole.so.0.0.0
lrwxrwxrwx 1 root root      21 Aug 12 14:27 ha_blackhole.so.0 -> ha_blackhole.so.0.0.0
-rwxr-xr-x 1 root root   30288 Aug  9  2013 ha_blackhole.so.0.0.0
lrwxrwxrwx 1 root root      19 Aug 12 14:27 ha_example.so -> ha_example.so.0.0.0
lrwxrwxrwx 1 root root      19 Aug 12 14:27 ha_example.so.0 -> ha_example.so.0.0.0
-rwxr-xr-x 1 root root   33168 Aug  9  2013 ha_example.so.0.0.0
lrwxrwxrwx 1 root root      21 Aug 12 14:27 ha_federated.so -> ha_federated.so.0.0.0
lrwxrwxrwx 1 root root      21 Aug 12 14:27 ha_federated.so.0 -> ha_federated.so.0.0.0
-rwxr-xr-x 1 root root   58064 Aug  9  2013 ha_federated.so.0.0.0
lrwxrwxrwx 1 root root      25 Aug 12 14:27 ha_innodb_plugin.so -> ha_innodb_plugin.so.0.0.0
lrwxrwxrwx 1 root root      25 Aug 12 14:27 ha_innodb_plugin.so.0 -> ha_innodb_plugin.so.0.0.0
-rwxr-xr-x 1 root root 1258272 Aug  9  2013 ha_innodb_plugin.so.0.0.0
-rwxr-xr-x 1 root root    1035 Aug 12 15:28 libgearman_mysql_udf.la
lrwxrwxrwx 1 root root      29 Aug 12 15:28 libgearman_mysql_udf.so -> libgearman_mysql_udf.so.0.0.0
lrwxrwxrwx 1 root root      29 Aug 12 15:28 libgearman_mysql_udf.so.0 -> libgearman_mysql_udf.so.0.0.0                      
-rwxr-xr-x 1 root root  114597 Aug 12 15:28 libgearman_mysql_udf.so.0.0.0  ##
-rwxr-xr-x 1 root root   22223 Aug 12 15:07 lib_mysqludf_json.so
[aaa@qq.com plugin]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
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 curren
#####注册 UDF 函数
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)
##指定 gearman 的服务信息
mysql> SELECT gman_servers_set('172.25.52.3:4730');
+--------------------------------------+
| gman_servers_set('172.25.52.3:4730') |
+--------------------------------------+
| 172.25.52.3:4730                     |
+--------------------------------------+
1 row in set (0.00 sec)

mysql> quit
Bye
[aaa@qq.com plugin]# cd
##编写 mysql 触发器
[aaa@qq.com ~]# vim test.sql

Redis集群

##导入已经编写号的 mysql 触发器
[root@server5 ~]# mysql < test.sql
[aaa@qq.com 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:80                  0.0.0.0:*                   LISTEN      2113/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1011/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1098/master         
tcp        0      0 0.0.0.0:4730                0.0.0.0:*                   LISTEN      4550/gearmand       
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      4486/php-fpm        
tcp        0      0 172.25.52.3:36063           172.25.52.4:3306            ESTABLISHED 1547/mysqld         
tcp        0      0 172.25.52.3:22              172.25.52.250:59876         ESTABLISHED 1211/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      1011/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1098/master         
tcp        0      0 :::4730                     :::*                        LISTEN      4550/gearmand       
tcp        0      0 :::11111                    :::*                        LISTEN      1187/ricci          
tcp        0      0 :::3306                     :::*                        LISTEN      1547/mysqld         
[aaa@qq.com redis]# ll worker.php 
-rw-r--r-- 1 root root 452 Aug 12 12:34 worker.php
[aaa@qq.com redis]# cp worker.php  /usr/local/
[aaa@qq.com redis]# ll gearman-1.1.2.tgz 
-rw-r--r-- 1 root root 30961 Aug 12 12:34 gearman-1.1.2.tgz
[aaa@qq.com redis]# tar zxf gearman-1.1.2.tgz 
[aaa@qq.com redis]# cd gearman-1.1.2
[aaa@qq.com gearman-1.1.2]# ls
ChangeLog  examples       php_gearman.h    tests
config.m4  LICENSE        README           test_worker.php
CREDITS    php_gearman.c  test_client.php
[aaa@qq.com gearman-1.1.2]# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[aaa@qq.com gearman-1.1.2]# ./configure
[aaa@qq.com gearman-1.1.2]# make && make install
[aaa@qq.com gearman-1.1.2]# cd 
[aaa@qq.com ~]# cd /usr/lib64/php/modules/
[aaa@qq.com modules]# ls
curl.so      gearman.so   mysqli.so     pdo.so         redis.so
fileinfo.so  json.so      mysql.so      pdo_sqlite.so  sqlite3.so
gd.so        mbstring.so  pdo_mysql.so  phar.so        zip.so
[aaa@qq.com modules]# ll gearman.so 
-rwxr-xr-x 1 root root 280186 Aug 12 15:44 gearman.so
[aaa@qq.com modules]# cd /etc/php.d/
[aaa@qq.com php.d]# ls
curl.ini      json.ini      mysql.ini      pdo_sqlite.ini  sqlite3.ini
fileinfo.ini  mbstring.ini  pdo.ini        phar.ini        zip.ini
gd.ini        mysqli.ini    pdo_mysql.ini  redis.ini
[aaa@qq.com php.d]# cp redis.ini  gearman.ini
[aaa@qq.com php.d]# vim gearman.ini 
#####################
更改: extension=gearman.so

Redis集群

[root@server3 php.d]# /etc/init.d/php-fpm reload
Reloading php-fpm: [12-Aug-2018 15:56:15] NOTICE: configuration file /etc/php-fpm.conf test is successful                                   [  OK  ]
[root@server3 php.d]# php -m |grep gearman
gearman
[root@server3 php.d]# cd /usr/local
[root@server3 local]# ls
bin      etc    include  lib64    sbin   src
cluster  games  lib      libexec  share  worker.php
[root@server3 local]# vim worker.php 
#############
$redis->connect('172.25.52.4', 6379); ##redis端的ip

Redis集群

##将进程打入后台
[aaa@qq.com local]# php /usr/local/worker.php &
[1] 6921
[aaa@qq.com local]# ps -ax
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
  PID TTY      STAT   TIME COMMAND            
 6921 pts/0    T      0:00 php /usr/local/worker.php
 6922 pts/0    R+     0:00 ps -ax

[1]+  Stopped                 php /usr/local/worker.php
[aaa@qq.com local]# kill -9 6921
[aaa@qq.com local]# ps ax  
 PID TTY      STAT   TIME COMMAND
 6934 pts/0    R+     0:00 ps ax
[1]+  Killed                  php /usr/local/worker.php
##将进程打入后台
[aaa@qq.com local]# nohup  php /usr/local/worker.php  &
[1] 6935
[aaa@qq.com local]# nohup: ignoring input and appending output to `nohup.out'

[aaa@qq.com local]# ps ax
  PID TTY      STAT   TIME COMMAND
 6935 pts/0    S      0:00 php /usr/local/worker.php
 6936 pts/0    R+     0:00 ps ax

[aaa@qq.com ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
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> 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    |          | aaa@qq.com | latin1               | latin1_swedish_ci    | latin1_swedish_ci  |
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

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='redhat';
Query OK, 9 rows affected (0.06 sec)
Rows matched: 9  Changed: 9  Warnings: 0

测试:
Redis集群

##再次更新数据
mysql> update test set name='westos';
Query OK, 9 rows affected (0.06 sec)
Rows matched: 9  Changed: 9  Warnings: 0

测试:

##查看 redis
[aaa@qq.com ~]# redis-cli 
127.0.0.1:6379> get test1
(nil)
127.0.0.1:6379> get 1
"westos"
127.0.0.1:6379> get 2
"westos"

刷新网页 此时网页上便可以获取到数据的变更即为数据同步
Redis集群