基于Gearman的MySQL缓存服务器(lnmp架构)

mac2024-10-28  9

文章目录

Redis+lnmp实现MySQL的缓存服务器数据访问流程实验环境rhel7.3lnmp+redis做mysql缓存服务器配置使用Gearmand完善mysql缓存架构为什么要使用Gearmand? 什么是GearmandGearmand配置

Redis+lnmp实现MySQL的缓存服务器

数据访问流程

数据访问流程:client -> app -> redis -> mysql -> redis -> client 客户端用app访问,先在redis里读数据,因为快,redis没有才去mysql读,读完保存在redis里,然后返回客户端,下次再读就快 换言之,一般情况下,用户只有第一次访问的数据是来自mysql,随后再访问相同数据时,就会直接读取redis中缓存好的数据。

实验环境rhel7.3

主机功能172.25.65.1nginx+php172,25.65.2redis172.25.65.3mysql

lnmp+redis做mysql缓存服务器配置

1.关闭所有之前和redis有关的进程

killall redis-server

#没有killall命令

yum whatprovides /usr/bin/killall [root@server1 ~]# yum install -y psmisc-22.20-11.el7.x86_64

随后再次执行killall命令 2.在server1配置好nginx 下载nginx软件

[root@server1 ~]#tar zxf nginx-1.17.4.tar.gz [root@server1 ~]#cd nginx-1.17.4 [root@server1 ~]#vim auto.cc.gcc 添加:user nginx nginx [root@server1 ~]#useradd nginx [root@server1 ~]#./configure -=-prefix=/usr/local/nginx [root@server1 ~]#make && make install

对于编译的时候,需要注意!!不需要编译openssl模块,原因是在这里nginx并不需要openssl模块,再者,如果在这里安装openssl模块可能会和后面安装php时候的openssl版本产生冲突,会出现问题

[root@server1 ~]#cd /usr/local/nginx/conf/nginx.conf location / { root 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 /scripts$fastcgi_script_name; include fastcgi.conf; }

3.启动并测试nginx

/usr/local/ngiinx/sbin/nginx

4.启动php-fpm

[root@server1 ~]# systemctl start php-fpm

5.在server2上 配置redis为master,因为之前做了主从,关闭server1的redis

删除原来的key [root@server2 ~]# redis-cli 127.0.0.1:6379> get name "dd" 127.0.0.1:6379> DEL name (integer) 1 127.0.0.1:6379> get name (nil)

6.配置测试页面

[root@server1 ~]# mv test.php /usr/local/nginx/html/ [root@server1 html]# mv test.php index.php

7.再开启server3,关闭原来的mysql

查看mysql安装后的软件名称 [root@server3 ~]# rpm -qa | grep mysql 卸载原来的mysql [root@server3 ~]# rpm -e `rpm -qa | grep mysql` --nodeps

8.安装mariadb,这里试验用这个就行

[root@server3 ~]# yum install -y mariadb-server 清除原来数据目录里的内容 [root@server3 ~]# cd /var/lib/mysql [root@server3 mysql]# rm -fr * 启动mariadb [root@server3 mysql]# systemctl start mariadb 安全初始化 mysql_secure_installation ##这里密码可以设置为简单的,如redhat 登录数据库,授权用户 MariaDB [(none)]> create database test; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all on test.* to redis@'%' identified by 'redhat'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)

9.修改server1上nginx的发布文件test.php

vim test.php 3 $redis->connect('172.25.0.2',6379) or die ("could net connect redis server"); 10 $connect = mysql_connect('172.25.0.3','redis',' redhat');

10.为server3上的mysql的test库加入一些数据

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 ;

注释掉的目前用不到,是创建查询的触发器的 11.导入数据

[root@server3 ~]# mysql -predhat < test.sq

12.浏览器测试

172.25.65.1 ##访问nginx所在的主机

可以看到server3中mysql导入的数据

mysql number is 1 name is number is 2 name is number is 3 name is number is 4 name is number is 5 name is number is 6 name is number is 7 name is number is 8 name is number is 9 name is

但是刷新一次后,可以看到后面就从server2的redis中读取数据

redis number is 1 name is test1 number is 2 name is test2 number is 3 name is test3 number is 4 name is test4 number is 5 name is test5 number is 6 name is test6 number is 7 name is test7 number is 8 name is test8 number is 9 name is test9

使用Gearmand完善mysql缓存架构

为什么要使用Gearmand?

根据上面虽然redis缓存了mysql的数据,减轻了mysql的压力,但是如果这时候mysql数据发生变更,redis会同步吗?

在server3上 MariaDB [test]> update test set name='westos' where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 变更后发现,redis里没有变,浏览器也看不到变化,但是在redis里做的变化能看到 127.0.0.1:6379> set 2 redhat OK 刷新浏览器,看到变化,这样显然不合理

什么是Gearmand

Gearmand是一个用来把工作委派给其它机器、分布式的调用更适合做某项工作的机器、并发的做某项工作在多个调用间做负载均衡、或用来调用其它语言的函数的系统。

简单来讲,就是客户端程序把请求提交给 gearmand,gearmand 会把请求转发给合适的 worker 来处理这个请求,最后还通过 gearmand 返回结果。

运行流程 Client --> Job --> Worker 1.Client 请求发起者,客户端程序可以是任何一种语言,C 、PHP 、Perl 、Python 等。 2.Job 请求调度者,负载协调把 Client 发出的请求转发给合适的 Worker。 3.Worker 请求处理者,处理 Job 分发来的请求,可以是任何一种语言

Gearmand配置

在server1上启动

[root@server1 ~]# systemctl start gearmand

在server3上解压

unzip lib_mysqludf_json-master.zip

在server3安装mariadb-devel

[root@server3]lib_mysqludf_json-master]# yum install -y mariadb-devel.x86_64

编译模块

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

将模块放到mysql插件目录

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

在server3上查看

MariaDB [(none)]> show global variables like 'plugin_dir'; +---------------+--------------------------+ | Variable_name | Value | +---------------+--------------------------+ | plugin_dir | /usr/lib64/mysql/plugin/ | +---------------+--------------------------+ 1 row in set (0.00 sec)

注册udf函数

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

安装插件管理gearman的分布式队列

[root@server3 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz

先安装libgearman,从server1上scp

[root@server3 ~]# yum install -y libevent-devel-2.0.21-4.el7.x86_64.rpm libgearman-*

编译安装gearman插件

[root@server3 redis]# tar zxf gearman-mysql-udf-0.6.tar.gz [root@server3 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/ --with-mysql [root@server3 gearman-mysql-udf-0.6]# make && make install

注册udf函数

mysql -p 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 | +--------------------+-----+-------------------------+----------+ 3 rows in set (0.00 sec)

指定gman服务信息

MariaDB [(none)]> SELECT gman_servers_set('172.25.0.1:4730');

编写mysql触发器

[root@server3 ~]# 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@server3 ~]# mysql -p < test.sql

查看触发器

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

编写gman的worker端

[root@server1 ~]# cp worker.php /usr/local/ [root@server1 local]# vim worker.php 7 $redis->connect('172.25.0.2', 6379);

后台运行worker

[root@server1 ~]# nohup php /usr/local/worker.php &> /dev/null & 测试修改数据库内容 MariaDB [test]> update test set name='westos' where id=2; 在redis上查看 127.0.0.1:6379> get 2 "westos" 浏览器查看,数据同步 redis number is 1 name is redhat number is 2 name is westos number is 3 name is test3 number is 4 name is test4 number is 5 name is test5 number is 6 name is test6 number is 7 name is test7 number is 8 name is test8 number is 9 name is test9
最新回复(0)