使用Redis做mysql缓存

redis做mysql缓存服务器

清理环境
server1:

killall redis-server
若没有killall
yum install psmisc-22.20-11.el7.x86_64
[[email protected] ~]# cd rhel7/
[[email protected] rhel7]# yum install -y *
[[email protected] rhel7]# systemctl start php-fpm
[[email protected] rhel7]# netstat -anltp    

使用Redis做mysql缓存

[[email protected] html]# mv test.php index.php
[[email protected] html]# ls
index.php
[[email protected] html]# vim index.php 
<?php
        $redis = new Redis();
        $redis->connect('172.25.24.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.24.3','redis','redhat');
                        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>";
        }
?>

使用Redis做mysql缓存

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx 

在server2:
配置一个redis

netstat -anltp   ##查看6379端口

使用Redis做mysql缓存

在server3:

/etc/init.d/redis_6379 stop
[[email protected] ~]# yum install mariadb-server -y
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysql_secure_installation
mysql -p
MariaDB [(none)]> create database test;
MariaDB [(none)]> grant all on test.* to [email protected]'%' identified by 'redhat';
MariaDB [(none)]> flush privileges;

使用Redis做mysql缓存

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

使用Redis做mysql缓存

测试:
在浏览器中访问172.25.24.1
使用Redis做mysql缓存
刷新

使用Redis做mysql缓存

更新mysql 同时更新redis

首先要编写的 mysql 触发器,就相当于 Gearman 的客户端。修改表,插入表就相当于直接下发任务。然后通过 lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式,然后在通过 gearman-mysql-udf 插件将任务加入到 Gearman 的任务队列中,最后通过redis_worker.php,也就是 Gearman 的 worker 端来完成 redis 数据库的更新。

[[email protected] lib_mysqludf_json-master]# yum install mariadb-devel

[[email protected] lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c 

[[email protected] lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
[[email protected] lib_mysqludf_json-master]# mysql -p
Enter password: 

#查看mysql 模块目录
MariaDB [(none)]> show global variables like 'plugin_dir';
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| plugin_dir    | /usr/lib64/mysql/plugin/ |
+---------------+--------------------------+

#注册函数
MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select * from mysql.func;
+-------------+-----+----------------------+----------+
| name        | ret | dl                   | type     |
+-------------+-----+----------------------+----------+
| json_object |   0 | lib_mysqludf_json.so | function |
+-------------+-----+----------------------+----------+

安装gearman软件包 安装php的gearman扩展

[[email protected] ~]# tar zxf gearman-mysql-udf-0.6.tar.gz

[[email protected] ~]# yum install libgearman-* libevent-devel-2.0.21-4.el7.x86_64.rpm -y

[[email protected] gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/ --with-mysql
[[email protected] gearman-mysql-udf-0.6]# make 
[[email protected] gearman-mysql-udf-0.6]# make install

#注册函数

[[email protected] gearman-mysql-udf-0.6]# mysql -p
Enter password: 
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
    -> ;
+--------------------+-----+-------------------------+----------+
| 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 |
+--------------------+-----+-------------------------+----------+

#指定gearman服务信息
MariaDB [(none)]> SELECT gman_servers_set('172.25.24.1:4730');
+--------------------------------------+
| gman_servers_set('172.25.24.1:4730') |
+--------------------------------------+
| 172.25.24.1:4730                     |
+--------------------------------------+

修改触发器

[[email protected] ~]# 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 ;

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

#server1:

[[email protected] ~]# systemctl start gearmand

[[email protected] ~]# vim worker.php 
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
 
$redis = new Redis();
$redis->connect('172.25.24.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] ~]# cp worker.php /usr/local/
[[email protected] ~]# nohup php /usr/local/worker.php &> /dev/null & 
[1] 1968

[[email protected] ~]# mysql -p
Enter password: 
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='redhat' where id=1;

刷新网页查看
使用Redis做mysql缓存