Linux运维——redis集群,作 mysql 的缓存服务器

redis集群:

master【server2】

[[email protected] ~]# cd /etc/redis/
[[email protected] redis]# vim 6379.conf
Linux运维——redis集群,作 mysql 的缓存服务器   ##配置一个master最少要对应一个slave,用户才可以往里面写数据
[[email protected] redis]# /etc/init.d/redis_6379 restart

【server1】

[[email protected] ~]# /etc/init.d/redis_6379 stop
[[email protected] ~]# chkconfig redis_6379 off
[[email protected] ~]# cd /usr/local/
[[email protected] local]# mkdir rediscluster
[[email protected] local]# cd rediscluster/
[[email protected] rediscluster]# mkdir 7000 7001 7002 7003 7004 7005
[[email protected] rediscluster]# cd 7000
[[email protected] 7000]# vim redis.conf

        Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] 7000]# redis-server redis.conf
[[email protected] 7000]# ps aux|grep redis-server
root      2265  0.1  0.7 153824  7700 ?        Ssl  17:53   0:00 redis-server *:7000 [cluster]
root      2272  0.0  0.0 112648   964 pts/0    R+   17:54   0:00 grep --color=auto redis-server

[[email protected] 7000]# cd ..
[[email protected] rediscluster]# cp 7000/redis.conf 7001/
[[email protected] rediscluster]# cp 7000/redis.conf 7002
[[email protected] rediscluster]# cp 7000/redis.conf 7003
[[email protected] rediscluster]# cp 7000/redis.conf 7004
[[email protected] rediscluster]# cp 7000/redis.conf 7005
[[email protected] rediscluster]# cd 7001
[[email protected] 7001]# vim redis.conf

    Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] 7001]# redis-server redis.conf
[[email protected] 7001]# ps aux|grep redis-server
root      2265  0.1  0.7 153824  7700 ?        Ssl  17:53   0:00 redis-server *:7000 [cluster]
root      2295  0.0  0.7 153824  7696 ?        Ssl  17:55   0:00 redis-server *:7001 [cluster]
root      2300  0.0  0.0 112648   960 pts/0    R+   17:55   0:00 grep --color=auto redis-server

[[email protected] 7001]# cd ../7002
[[email protected] 7002]# vim redis.conf

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] 7002]# redis-server redis.conf
[[email protected] 7002]# ps aux|grep redis-server
root      2265  0.1  0.7 153824  7700 ?        Ssl  17:53   0:00 redis-server *:7000 [cluster]
root      2295  0.1  0.7 153824  7696 ?        Ssl  17:55   0:00 redis-server *:7001 [cluster]
root      2312  0.0  0.7 153824  7700 ?        Ssl  17:56   0:00 redis-server *:7002 [cluster]
root      2317  0.0  0.0 112648   964 pts/0    R+   17:56   0:00 grep --color=auto redis-server

[[email protected] 7002]# cd ../7003
[[email protected] 7003]# vim redis.conf

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] 7003]# redis-server redis.conf
[[email protected] 7003]# ps aux|grep redis-server
root      2265  0.1  0.7 153824  7700 ?        Ssl  17:53   0:00 redis-server *:7000 [cluster]
root      2295  0.0  0.7 153824  7696 ?        Ssl  17:55   0:00 redis-server *:7001 [cluster]
root      2312  0.1  0.7 153824  7700 ?        Ssl  17:56   0:00 redis-server *:7002 [cluster]
root      2328  0.0  0.7 153824  7696 ?        Ssl  17:56   0:00 redis-server *:7003 [cluster]
root      2333  0.0  0.0 112648   960 pts/0    R+   17:57   0:00 grep --color=auto redis-server

[[email protected] 7003]# cd ../7004
[[email protected] 7004]# vim redis.conf

           Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] 7004]# redis-server redis.conf
[[email protected] 7004]# ps aux|grep redis-server
root      2265  0.1  0.7 153824  7700 ?        Ssl  17:53   0:00 redis-server *:7000 [cluster]
root      2295  0.0  0.7 153824  7696 ?        Ssl  17:55   0:00 redis-server *:7001 [cluster]
root      2312  0.1  0.7 153824  7700 ?        Ssl  17:56   0:00 redis-server *:7002 [cluster]
root      2328  0.1  0.7 153824  7696 ?        Rsl  17:56   0:00 redis-server *:7003 [cluster]
root      2344  0.0  0.7 153824  7700 ?        Ssl  17:57   0:00 redis-server *:7004 [cluster]
root      2349  0.0  0.0 112648   964 pts/0    R+   17:57   0:00 grep --color=auto redis-server

[[email protected] 7004]# cd ../7005
[[email protected] 7005]# vim redis.conf

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] 7005]# redis-server redis.conf
[[email protected] 7005]# ps aux|grep redis-server
root      2265  0.1  0.7 153824  7700 ?        Ssl  17:53   0:00 redis-server *:7000 [cluster]
root      2295  0.0  0.7 153824  7696 ?        Ssl  17:55   0:00 redis-server *:7001 [cluster]
root      2312  0.1  0.7 153824  7700 ?        Ssl  17:56   0:00 redis-server *:7002 [cluster]
root      2328  0.1  0.7 153824  7696 ?        Rsl  17:56   0:00 redis-server *:7003 [cluster]
root      2344  0.1  0.7 153824  7700 ?        Ssl  17:57   0:00 redis-server *:7004 [cluster]
root      2353  0.0  0.7 153824  7700 ?        Ssl  17:58   0:00 redis-server *:7005 [cluster]
root      2358  0.0  0.0 112648   964 pts/0    R+   17:58   0:00 grep --color=auto redis-server

[[email protected] 7005]# cd

[[email protected] ~]# cd redis-5.0.3/
[[email protected] redis-5.0.3]# cd src/
[[email protected] src]# cp redis-trib.rb /usr/local/bin/
[[email protected] src]# cd
[[email protected] ~]# yum install ruby -y
[[email protected] ~]# redis-cli  --cluster help
[[email protected] ~]# redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7000 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

Linux运维——redis集群,作 mysql 的缓存服务器

测试:
[[email protected] ~]# redis-cli --cluster check 127.0.0.1:7000


[[email protected] ~]# redis-cli --cluster check 127.0.0.1:7001
[[email protected] ~]# redis-cli --cluster check 127.0.0.1:7002
[[email protected] ~]# redis-cli --cluster check 127.0.0.1:7003
[[email protected] ~]# redis-cli --cluster check 127.0.0.1:7004
[[email protected] ~]# redis-cli --cluster check 127.0.0.1:7005

[[email protected] ~]# redis-cli -c -p 7000
127.0.0.1:7000> info

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] ~]# redis-cli -c -p 7005
127.0.0.1:7005> INFO

Linux运维——redis集群,作 mysql 的缓存服务器
127.0.0.1:7005> set name westos
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get name
"westos"

当主宕机,从替代成为了主;当原主恢复,作为了新主的从机:


[[email protected] ~]# redis-cli -c -p 7001
127.0.0.1:7001> SHUTDOWN
not connected>
[[email protected] ~]# ps aux|grep redis-server
root      2265  0.0  1.4 163040 14280 ?        Ssl  17:53   0:00 redis-server *:7000 [cluster]
root      2312  0.1  0.7 156384  8084 ?        Ssl  17:56   0:01 redis-server *:7002 [cluster]
root      2328  0.0  0.7 156384  8032 ?        Ssl  17:56   0:00 redis-server *:7003 [cluster]
root      2344  0.0  0.7 156384  8056 ?        Ssl  17:57   0:00 redis-server *:7004 [cluster]
root      2353  0.0  1.2 163040 12540 ?        Ssl  17:58   0:00 redis-server *:7005 [cluster]
root      2422  0.0  0.0 112648   964 pts/0    R+   18:10   0:00 grep --color=auto redis-server

[[email protected] ~]# redis-cli -c -p 7004
127.0.0.1:7004> INFO

           Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] ~]# cd /usr/local/rediscluster/7001
[[email protected] 7001]# redis-server redis.conf
[[email protected] 7001]# ps aux|grep redis-server
root      2265  0.1  1.4 163040 14288 ?        Ssl  17:53   0:01 redis-server *:7000 [cluster]
root      2312  0.1  0.7 156384  8092 ?        Ssl  17:56   0:01 redis-server *:7002 [cluster]
root      2328  0.0  0.7 156384  8040 ?        Ssl  17:56   0:00 redis-server *:7003 [cluster]
root      2344  0.1  1.2 163040 12540 ?        Ssl  17:57   0:00 redis-server *:7004 [cluster]
root      2353  0.0  1.4 163040 14520 ?        Ssl  17:58   0:00 redis-server *:7005 [cluster]
root      2432  0.1  0.9 156896 10012 ?        Ssl  18:11   0:00 redis-server *:7001 [cluster]
root      2439  0.0  0.0 112648   960 pts/0    R+   18:11   0:00 grep --color=auto redis-server
[[email protected] 7001]# redis-cli -c -p 7004
127.0.0.1:7004> INFO

            Linux运维——redis集群,作 mysql 的缓存服务器
127.0.0.1:7004> get name
"westos"
[[email protected] 7001]# redis-cli --cluster check 127.0.0.1:7004

加减节点

[[email protected] rediscluster]# mkdir 7006
[[email protected] rediscluster]# mkdir 7007
[[email protected] rediscluster]# cp 7000/redis.conf 7006
[[email protected] rediscluster]# cp 7000/redis.conf 7007
[[email protected] rediscluster]# cd 7006
[[email protected] 7006]# vim redis.conf

                Linux运维——redis集群,作 mysql 的缓存服务器
 [[email protected] rediscluster]# redis-server redis.conf
 [[email protected] rediscluster]# cd ../7007
 [[email protected] 7007]# vim redis.conf

                              Linux运维——redis集群,作 mysql 的缓存服务器
 [[email protected] 7007]# redis-server redis.conf

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] rediscluster]# redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000
[[email protected] rediscluster]# redis-cli --cluster info  127.0.0.1:7006
 Linux运维——redis集群,作 mysql 的缓存服务器

[[email protected] rediscluster]# redis-cli --cluster add-node --cluster-slave --cluster-master-id

73901351b9303f07bd69c890117062db0c4df571 127.0.0.1:7007 127.0.0.1:7000
[[email protected] rediscluster]# redis-cli --cluster reshard 127.0.0.1:7004
[[email protected] rediscluster]# redis-cli --cluster info 127.0.0.1:7001

Linux运维——redis集群,作 mysql 的缓存服务器

哈细槽不完整或过半master宕机都会导致整个集群不可用
不完整:kill一个master、slave进程对

 

二、redis做mysql的缓存器

1.搭建lnmp

1)PHP的rpm包安装
gearmand-1.1.12-18.el7.x86_64.rpm
libgearman-1.1.12-18.el7.x86_64.rpm
libzip-0.10.1-8.el7.x86_64.rpm
openssl-1.0.2k-16.el7.x86_64.rpm
openssl-libs-1.0.2k-16.el7.x86_64.rpm
php-cli-5.4.16-46.el7.x86_64.rpm
php-common-5.4.16-46.el7.x86_64.rpm
php-fpm-5.4.16-46.el7.x86_64.rpm
php-mysql-5.4.16-46.el7.x86_64.rpm
php-pdo-5.4.16-46.el7.x86_64.rpm
php-pecl-gearman-1.1.2-1.el7.x86_64.rpm
php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm
php-pecl-redis-2.2.8-1.el7.x86_64.rpm
php-process-5.4.16-46.el7.x86_64.rpm
php-xml-5.4.16-46.el7.x86_64.rpm
[[email protected] rhel7]# yum install * -y
[[email protected] rhel7]# cd /etc/php-fpm.d/
[[email protected] php-fpm.d]# vim www.conf

  Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] php-fpm.d]# systemctl start php-fpm

Linux运维——redis集群,作 mysql 的缓存服务器

2)nginx的原码安装
nginx-1.14.2.tar.gz
[[email protected] ~]# yum install gcc pcre-devel zlib-devel -y
[[email protected] ~]# tar zxf nginx-1.14.2.tar.gz
[[email protected] ~]# cd nginx-1.14.2/
[[email protected] nginx-1.14.2]# vim auto/cc/gcc

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] nginx-1.14.2]# ./configure --prefix=/usr/local/nginx
[[email protected] nginx-1.14.2]# make
[[email protected] nginx-1.14.2]# make install
[[email protected] nginx-1.14.2]# cd /usr/local/nginx/conf/
[[email protected] conf]# vim nginx.conf

  Linux运维——redis集群,作 mysql 的缓存服务器

  Linux运维——redis集群,作 mysql 的缓存服务器

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] conf]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/bin/
[[email protected] conf]# nginx -t
[[email protected] conf]# nginx

Linux运维——redis集群,作 mysql 的缓存服务器

[[email protected] conf]# cd /usr/local/nginx/html/
[[email protected] html]# vim index.php

 Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] html]# ls
50x.html  index.html  index.php  test.php  test.sql  worker.php

Linux运维——redis集群,作 mysql 的缓存服务器

[[email protected] html]# mv worker.php /usr/local/
[[email protected] html]# mv test.sql ~
[[email protected] html]# ls
50x.html  index.html  index.php  test.php
[[email protected] html]# rm -f index.php
[[email protected] html]# mv test.php index.php
[[email protected] html]# vim index.php
<?php
        $redis = new Redis();
        $redis->connect('172.25.19.2',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.19.3','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>";
        }
?>

5)redis
[[email protected] redis]# redis-cli
127.0.0.1:6379> INFO [section]

Linux运维——redis集群,作 mysql 的缓存服务器

4)maridb
[[email protected] ~]# cd /var/lib/mysql
[[email protected] mysql]# rm -fr *
[[email protected] mysql]# ls
[[email protected] mysql]# cd
[[email protected] ~]# rpm -e `rpm -qa|grep mysql` --nodeps  ##卸载mysql相关的包
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave

[[email protected] ~]# yum install mariadb -y
[[email protected] ~]# yum install mariadb-server -y
[[email protected] ~]# vim /etc/my.cnf

[[email protected] ~]# systemctl start mariadb.service
[[email protected] ~]# scp test.sql server3:   
[[email protected] ~]# mysql
MariaDB [(none)]> show databases;

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] ~]# mysql < test.sql
[[email protected] ~]# mysql
MariaDB [(none)]> use test;
MariaDB [test]> select * from test;

Linux运维——redis集群,作 mysql 的缓存服务器

MariaDB [test]> grant all on test.* to [email protected]'%' identified by 'westos';

测试缓存:访问172.25.19.1 刷两次

Linux运维——redis集群,作 mysql 的缓存服务器

Linux运维——redis集群,作 mysql 的缓存服务器
【server2】
[[email protected] redis]# redis-cli
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379>

测试存在的数据同步问题:
MariaDB [test]> desc test;

Linux运维——redis集群,作 mysql 的缓存服务器

MariaDB [test]> update test set name='westos' where id=1;
MariaDB [test]> select * from test;                 ##数据库已经更新

Linux运维——redis集群,作 mysql 的缓存服务器

[[email protected] redis]# redis-cli             ##redis缓存的还是更新前的数据
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379>

三、Gearman数据库更新触发更改

client:mysql ->update -> trigger(触发器生效)->mysqludf_json(转化为json数据结构)->gman_do_background(gearmand:4730)(把通识的json模块发给gearmand)
server:gearmand gearmanip:4730
worker:php->php-gesrman:4730(连接server4370拿更新任务) -> 传给php-redis->sync_to_redis(更新redis)

1.安装gearman软件

[[email protected] redis]# yum install -y gearmand-1.1.8-2.el6.x86_64.rpm libgearman-* libevent-*

[[email protected] redis]# systemctl start gearmand

2.安装php的gearman扩展

[[email protected] mysql]# yum install unzip -y
[[email protected] ~]# unzip lib_mysqludf_json-master.zip
[[email protected] ~]# cd lib_mysqludf_json-master/
[[email protected] lib_mysqludf_json-master]# yum install gcc mariadb-devel -y

3. 安装 lib_mysqludf_json

[[email protected] lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/ #拷贝 lib_mysqludf_json.so 模块
查看 mysql 的模块目录:

[[email protected] lib_mysqludf_json-master]# mysql
MariaDB [(none)]> show global variables like 'plugin_dir';   #查看模块目录

Linux运维——redis集群,作 mysql 的缓存服务器
MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so'; #注册 UDF 函数
MariaDB [(none)]> select * from mysql.func;   #查看函数

Linux运维——redis集群,作 mysql 的缓存服务器
4. 安装 gearman-mysql-udf

这个插件是用来管理调用 Gearman 的分布式的队列
[[email protected] ~]# tar zxf gearman-mysql-udf-0.6.tar.gz
[[email protected] ~]# yum install libgearman-1.1.12-18.el7.x86_64.rpm libgearman-devel-1.1.12-18.el7.x86_64.rpm  libevent-devel-2.0.21-4.el7.x86_64.rpm -y
[[email protected] ~]# cd gearman-mysql-udf-0.6/
[[email protected] gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/ --with-mysql
[[email protected] gearman-mysql-udf-0.6]# make && make install
[[email protected] gearman-mysql-udf-0.6]# cd /usr/lib64/mysql/plugin/
注册 UDF 函数:
[[email protected] plugin]# mysql
MariaDB [(none)]> CREATE FUNCTION gman_do_background RETURNS STRING SONAME 'libgearman_mysql_udf.so';

MariaDB [(none)]> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so';

MariaDB [(none)]> select * from mysql.func;    #查看函数

Linux运维——redis集群,作 mysql 的缓存服务器
MariaDB [(none)]> SELECT gman_servers_set('172.25.19.1:4730');    #指定 gearman 的服务信息

Linux运维——redis集群,作 mysql 的缓存服务器
5. 编写 mysql 触发器

[[email protected] plugin]# cd
[[email protected] ~]# vim test.sql

Linux运维——redis集群,作 mysql 的缓存服务器

[[email protected] ~]# mysql < test.sql

6.编写 gearman 的 worker 端

 [[email protected] ~]# rpm -q gearmand
gearmand-1.1.12-18.el7.x86_64
[[email protected] ~]# systemctl start gearmand

Linux运维——redis集群,作 mysql 的缓存服务器

[[email protected] ~]# cd /usr/local/
[[email protected] local]# vim worker.php
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');

$redis = new Redis();
$redis->connect('172.25.19.2', 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);
}
?>

[[email protected] local]# which php
/usr/bin/php

7. 后台运行 worker

[[email protected] local]# nohup php /usr/local/worker.php &
[1] 5539
[[email protected]er1 local]# nohup: ignoring input and appending output to ‘nohup.out’

测试:更新 mysql 中的数据

 [[email protected] ~]# mysql

MariaDB [(none)]> SHOW TRIGGERS FROM test;

Linux运维——redis集群,作 mysql 的缓存服务器

MariaDB [(none)]> 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
MariaDB [test]> update test set name='new' where id=1;

MariaDB [test]> select * from test;

Linux运维——redis集群,作 mysql 的缓存服务器
[[email protected] redis]# redis-cli
127.0.0.1:6379> get 1
"new"

刷新测试页面数据同步

Linux运维——redis集群,作 mysql 的缓存服务器