Operación de conexión entre redis y mysql (redis acelera mysql)

1. Arquitectura de configuración

1.1 Configurar la conexión de mysql y redis

[root@server2 ~]# yum install -y httpd php php-mysql php-redis   ##php-redis是没有带的
[root@server2 ~]# rpm -q php  ##所有依赖必须和版本一致
php-5.4.16-46.el7.x86_64
[root@server2 ~]# which phpize 
/usr/bin/phpize
[root@server2 ~]# rpm -qf /usr/bin/phpize 
php-cli-5.4.16-46.el7.x86_64

[root@server2 ~]# lftp 172.25.254.250
lftp 172.25.254.250:~> cd pub/docs/redis/
lftp 172.25.254.250:/pub/docs/redis> mirror rhel7/

[root@server2 ~]# rpm -qa | grep php
php-common-5.4.16-46.el7.x86_64
php-pdo-5.4.16-46.el7.x86_64
php-mysql-5.4.16-46.el7.x86_64
php-cli-5.4.16-46.el7.x86_64
php-5.4.16-46.el7.x86_64

[root@server2 ~]# cd rhel7/
[root@server2 ~]# cd rhel7/
[root@server2 rhel7]# ls
gearmand-1.1.12-18.el7.x86_64.rpm          php-fpm-5.4.16-46.el7.x86_64.rpm
libevent-devel-2.0.21-4.el7.x86_64.rpm     php-mysql-5.4.16-46.el7.x86_64.rpm
libgearman-1.1.12-18.el7.x86_64.rpm        php-pdo-5.4.16-46.el7.x86_64.rpm
libgearman-devel-1.1.12-18.el7.x86_64.rpm  php-pecl-gearman-1.1.2-1.el7.x86_64.rpm
libzip-0.10.1-8.el7.x86_64.rpm             php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm
openssl-1.0.2k-16.el7.x86_64.rpm           php-pecl-redis-2.2.8-1.el7.x86_64.rpm
openssl-libs-1.0.2k-16.el7.x86_64.rpm      php-process-5.4.16-46.el7.x86_64.rpm
php-cli-5.4.16-46.el7.x86_64.rpm           php-xml-5.4.16-46.el7.x86_64.rpm
php-common-5.4.16-46.el7.x86_64.rpm
[root@server2 rhel7]# yum install -y php-fpm-5.4.16-46.el7.x86_64.rpm php-pecl-redis-2.2.8-1.el7.x86_64.rpm php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm

[root@server2 ~]# php -m | grep mysql 
mysql
mysqli
pdo_mysql
[root@server2 ~]# php -m | grep redis
redis


[root@server2 ~]# scp test.sql server4:    ##传到server4上的redis
test.sql                                               100%  493   139.3KB/s   00:00    

[root@server2 ~]# vim test.php 
[root@server2 ~]# cat test.php 
<?php
        $redis = new Redis();
        $redis->connect('172.25.13.3',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.13.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 "<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>";
        }
?>
[root@server2 ~]# cp test.php /var/www/html/
[root@server2 ~]# systemctl start httpd.service    ##启动http

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí

1.2 Configurar la base de datos mysql

[root@server4 ~]# yum install mariadb-server -y 
[root@server4 ~]# systemctl start mariadb.service    ##启动
[root@server2 ~]# cat 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 ~]# mysql < test.sql    ##数据写入数据库

MariaDB [(none)]> grant all on test.* to redis@'%' identified by 'westos';

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

1.3 Instalar la base de datos redis

[root@server3 ~]# ll redis-5.0.8.tar.gz 
-rw-r--r-- 1 root root 1985757 Mar 24  2020 redis-5.0.8.tar.gz
[root@server3 ~]# ll redis-5.0.8.tar.gz 
-rw-r--r-- 1 root root 1985757 Mar 24  2020 redis-5.0.8.tar.gz
[root@server3 ~]# tar zxf redis-5.0.8.tar.gz 
[root@server3 ~]# cd redis-5.0.8/
[root@server3 redis-5.0.8]# ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-moduleapi  src
BUGS             deps     MANIFESTO  runtest          runtest-sentinel   tests
CONTRIBUTING     INSTALL  README.md  runtest-cluster  sentinel.conf      utils
[root@server3 redis-5.0.8]# yum install gcc -y 
[root@server3 redis-5.0.8]# make 
[root@server3 redis-5.0.8]# make install  
[root@server3 redis-5.0.8]# cd utils/
[root@server3 utils]# ls
build-static-symbols.tcl  graphs             redis-copy.rb          speed-regression.tcl
cluster_fail_time.tcl     hashtable          redis_init_script      whatisdoing.sh
corrupt_rdb.c             hyperloglog        redis_init_script.tpl
create-cluster            install_server.sh  redis-sha1.rb
generate-command-help.rb  lru                releasetools
[root@server3 utils]# ./install_server.sh 
[root@server3 utils]# /etc/init.d/redis_6379 start
##配置端口
[root@server3 redis-5.0.8]# cd /etc/redis/
[root@server3 redis]# ls
6379.conf
[root@server3 redis]# vim 6379.conf 

##重启
[root@server3 redis]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@server3 redis]# redis-cli 
127.0.0.1:6379> CONFIG GET all
(empty list or set)
127.0.0.1:6379> CONFIG GET bind
1) "bind"
2) "0.0.0.0"
127.0.0.1:6379> 





Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

1.4 Prueba

  • Bajo la configuración actual, después de que se actualice mysql, redis no se actualizará en tiempo real

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

2. Sincronización en tiempo real de redis y mysql

2.1 Instalar lib_mysqludf_json

[root@server4 ~]# ll lib_mysqludf_json-master.zip    #找到包
-rw-r--r-- 1 root root 23473 Mar 15  2016 lib_mysqludf_json-master.zip
[root@server4 ~]# unzip lib_mysqludf_json-master.zip 
[root@server4 lib_mysqludf_json-master]# yum install -y gcc   ##安装gcc
[root@server4 lib_mysqludf_json-master]# yum install mariadb-devel.x86_64 -y   ##装开发包

[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]# ll lib_mysqludf_json.so 
-rwxr-xr-x 1 root root 17528 Mar 11 00:02 lib_mysqludf_json.so

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

2.2 Ver el directorio del módulo mysql

MariaDB [(none)]> show global variables like 'plugin_dir';

2.3 Copiar el módulo lib_mysqludf_json.so

## 3. 拷贝 lib_mysqludf_json.so 模块
[root@server4 lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
[root@server4 lib_mysqludf_json-master]# cd /usr/lib64/mysql/plugin/
[root@server4 plugin]# ls

Inserte la descripción de la imagen aquí

2.4 Registrar la función UDF

## 4. 注册 UDF 函数
mysql> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
MariaDB [(none)]> select * from mysql.func;

Inserte la descripción de la imagen aquí

2.5 Instalar gearman-mysql-udf

	- 这个插件是用来管理调用 Gearman 的分布式的队列。
		https://launchpad.net/gearman-mysql-udf
		
		# tar zxf gearman-mysql-udf-0.6.tar.gz
		# cd gearman-mysql-udf-0.6
		# ./configure --with-mysql=/usr/bin/mysql_config
		--libdir=/usr/lib64/mysql/plugin/
		# make
		# make install
		
		gearmand-1.1.12-18.el7.x86_64.rpm 分发框架
[root@server4 plugin]# ll gearman-mysql-udf-0.6.tar.gz 
-rw-r--r-- 1 root root 377301 Mar 15  2016 gearman-mysql-udf-0.6.tar.gz
[root@server4 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz    
[root@server4 ~]# cd gearman-mysql-udf-0.6/         
[root@server4 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/  ##执行
[root@server4 ~]# ll 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
-rw-r--r-- 1 root root  86564 Jan 20  2019 libevent-devel-2.0.21-4.el7.x86_64.rpm
-rw-r--r-- 1 root root  82136 Jan 19  2019 libgearman-1.1.12-18.el7.x86_64.rpm
-rw-r--r-- 1 root root 225208 Jan 20  2019 libgearman-devel-1.1.12-18.el7.x86_64.rpm
[root@server4 ~]# 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 
[root@server4 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/
[root@server4 gearman-mysql-udf-0.6]# make
[root@server4 gearman-mysql-udf-0.6]# make install 
[root@server4 gearman-mysql-udf-0.6]# cd /usr/lib64/mysql/plugin/
[root@server4 plugin]# ls


## 注册 UDF 函数
mysql> CREATE FUNCTION gman_do_background RETURNS STRING SONAME
'libgearman_mysql_udf.so';
mysql> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME
'libgearman_mysql_udf.so';
MariaDB [(none)]> select * from mysql.func;

Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

2.6 Especificar la información de servicio de Gearman

[root@server2 ~]# yum install -y gearmand-1.1.12-18.el7.x86_64.rpm libgearman-devel-1.1.12-18.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.rpm libevent-devel-2.0.21-4.el7.x86_64.rpm
[root@server2 ~]# systemctl start gearmand.service 
[root@server2 ~]# netstat -antlp | grep :4730

##mysql数据库端指定gearman 服务(安装在server2)信息
MariaDB [(none)]> SELECT gman_servers_set('172.25.13.2:4730');
+--------------------------------------+
| gman_servers_set('172.25.13.2:4730') |
+--------------------------------------+
| 172.25.13.2:4730                     |
+--------------------------------------+

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí

2.7 Escriba el disparador de mysql (escriba de acuerdo con la situación real)

[root@server4 ~]# cat 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 ~]# mysql < test.sql 
[root@server4 ~]# mysql
MariaDB [(none)]> SHOW TRIGGERS FROM test;

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

2.8 Escriba el extremo trabajador de gearman

[root@server2 ~]# yum install gearmand-1.1.12-18.el7.x86_64.rpm libevent-devel-2.0.21-4.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.rpm  libgearman-devel-1.1.12-18.el7.x86_64.rpm   ##这些东西必须有


#后台运行 worker
[root@server2 ~]# nohup php worker.php &   #现在直接运行会直接退出,因为缺少依赖,会提示缺少什么
[root@server2 ~]# yum install php-pecl-gearman-1.1.2-1.el7.x86_64.rpm -y   ##安装依赖

[root@server2 ~]# php -m | grep gearman  ##查找到服务
gearman
[root@server2 ~]# nohup php worker.php &   ##运行成功,后台查看
[2] 4216
[root@server2 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@server2 ~]# ps ax 

Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

2.9 Prueba si la actualización está sincronizada

Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

2.10 Métodos de actualización violentos

  • Elimine los datos directamente en el lado de redis, y redis irá a la base de datos mysql para leer los datos y almacenarlos en redis.

Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/qwerty1372431588/article/details/114652810
Recomendado
Clasificación