环境设定
base2 172.25.78.12 nginx php
base3 172.25.78.13 redis端
base4 172.25.78.14 mysql端
# 1.在base2(nginx php)上
配置nginx和php(这里的nginx只是提供了负载均衡,所以版本要求不高)
[[email protected] ~]# killall redis-server
[[email protected] ~]# 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
[[email protected] ~]# yum install -y *.rpm # 安装php
注:当服务器上有openssl-devel开发包时,会有冲突,所以要先删除
[[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
171 # debug
172 #CFLAGS="$CFLAGS -g"
[[email protected] nginx-1.14.2]# vim src/core/nginx.h
14 #define NGINX_VER "nginx/"
[[email protected] nginx-1.14.2]# ./configure --prefix=/usr/local/nginx
[[email protected] nginx-1.14.2]# make && make install
[[email protected] ~]# cd /usr/local/nginx/conf/
[[email protected] conf]# vim 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;
}
[[email protected] conf]# ln -s /usr/local/nginx/sbin/nginx /sbin
[[email protected] conf]# nginx -t
[[email protected] conf]# nginx # 开启nginx服务
[[email protected] conf]# vim /etc/php.ini
878 date.timezone =Asia/Shanghai
[[email protected] conf]# vim /etc/php-fpm.d/www.conf
39 user = nginx
41 group = nginx
[[email protected] conf]# systemctl start php-fpm
[[email protected] conf]# systemctl status php-fpm # 开启php服务
[[email protected] conf]# netstat -antlp # 查看php的端口
# nginx服务搭建成功
[[email protected] conf]# cd /usr/local/nginx/html/
[[email protected] html]# vim index.php # 编写php网页,测试php
<?php
phpinfo()
?>
# php配置成功
[[email protected] html]# vim index.php # 重新编写php页面,把redis服务器和mysql服务器建立练习
<?php
$redis = new Redis();
$redis->connect(‘172.25.78.13‘,6379) or die ("could net connect redis server"); # 这是redis服务器
# $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.78.14‘,‘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>";
}
# 2.redis服务端
[[email protected] ~]# tar zxf redis-5.0.3.tar.gz
[[email protected] ~]# cd redis-5.0.3
[[email protected] redis-5.0.3]# yum install gcc -y
[[email protected] redis-5.0.3]# make && make install
[[email protected] redis-5.0.3]# cd utils/
[[email protected] utils]# ./install_server.sh
[[email protected] utils]# vim /etc/redis/6379.conf
70 bind 0.0.0.0
[[email protected] ~]# systemctl restart redis_6379
[[email protected] ~]# redis-cli
127.0.0.1:6379> info replication
[r[email protected] ~]# netstat -antlp
# 3.mysql服务端
[[email protected] ~]# yum install -y mariadb-server
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysql_secure_installation
[[email protected] ~]# mysql -p
Enter password:
MariaDB [(none)]> show databases;
MariaDB [(none)]> create database test;
MariaDB [(none)]> grant all on test.* to [email protected]‘%‘ identified by ‘redhat‘;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> quit
Bye
[[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; # 在test数据库里创建表
INSERT INTO `test` VALUES (1,‘test1‘),(2,‘test2‘),(3,‘test3‘),(4,‘test4‘),(5,‘test5‘),(6,‘test6‘),(7,‘test7‘),(8,‘test8‘),(9,‘test9‘); # 给表里添加内容
[[email protected] ~]# mysql -predhat < test.sql
[[email protected] ~]# mysql -p
Enter password:
MariaDB [(none)]> select * from test.test;
# 测试:
这是mysql服务器通过php页面利用redis缓存来显示数据库中的内容
# 但是有一个问题,mysql更新后,redis无法同步
MariaDB [(none)]> use test;
MariaDB [test]> update test set name=‘lala‘ where id=1;
MariaDB [test]> select * from test.test;
[[email protected] ~]# redis-cli # 发现redis服务器并没有同步mysql上的数据
2.gearman 实现redis和mysql的数据同步
Gearman 是一个支持分布式的任务分发框架:
Gearman Job Server: Gearman 核心程序,需要编译安装并以守护进程形式运行在后台。
Gearman Client:可以理解为任务的请求者。
Gearman Worker:任务的真正执行者,一般需要自己编写具体逻辑并通过守护进程方式
运行,Gearman Worker 接收到 Gearman Client 传递的任务内容后,会按顺序处理。
大致流程:下面要编写的 mysql 触发器,就相当于 Gearman 的客户端。修改表,插入表就相当于直接
下发任务。然后通过 lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式,然后
在通过 gearman-mysql-udf 插件将任务加入到 Gearman 的任务队列中,最后通过
[[email protected] ~]# systemctl start gearmand
[[email protected] ~]# netstat -antlp
安装 php 的 gearman 扩展,安装 lib_mysqludf_json,lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式。通常,数据库中的数据映射为 JSON 格式,是通过程序来转换的。
[[email protected] ~]# ls
lib_mysqlud_json-master.zip
[[email protected] ~]# yum install -y unzip
[[email protected] ~]# unzip lib_mysqludf_json-master.zip
[[email protected] ~]# cd lib_mysqludf_json-master
[[email protected] lib_mysqludf_json-master]# yum install -y gcc
[[email protected] lib_mysqludf_json-master]# yum list mariadb-devel
[[email protected] lib_mysqludf_json-master]# yum install -y mariadb-devel.x86_64
[[email protected] lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
[[email protected] ~]# cp lib_mysqludf_json-master/lib_mysqludf_json.so /usr/lib64/mysql/plugin/
[[email protected] lib_mysqludf_json-master]# cd /usr/lib64/mysql/
[[email protected] ~]# cd /usr/lib64/mysql/plugin/
[[email protected] plugin]# mysql -p
Enter password:
MariaDB [(none)]> show global variables like ‘plugin_dir‘; # 查看 mysql 的模块目录:
MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME ‘lib_mysqludf_json.so‘; # 注册 UDF 函数
MariaDB [(none)]> select * from mysql.func;
MariaDB [(none)]> quit
Bye
# 安装 gearman-mysql-udf,这个插件是用来管理调用 Gearman 的分布式的队列。
[[email protected] ~]# ls
gearman-mysql-udf-0.6.tar.gz
[[email protected] ~]# cd gearman-mysql-udf-0.6
[[email protected] gearman-mysql-udf-0.6]# ./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib64/mysql/plugin/ # 有报错,依赖性没解决完
[[email protected] gearman-mysql-udf-0.6]# cd
[[email protected] ~]# yum install -y libgearman-devel-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 # 解决依赖性
[[email protected] ~]# cd gearman-mysql-udf-0.6
[[email protected] gearman-mysql-udf-0.6]# ./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib64/mysql/plugin/
[[email protected] gearman-mysql-udf-0.6]# make && make install
[[email protected] gearman-mysql-udf-0.6]# mysql -p
Enter password:
# 注册 UDF 函数
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; # 查看函数
MariaDB [(none)]> SELECT gman_servers_set(‘172.25.78.12:4730‘); # 指定 gearman 的服务信息
mysql> Bye
# 编写 mysql 触发器(根据实际情况编写)
[[email protected] gearman-mysql-udf-0.6]# cd
[[email protected] ~]# vim test.sql
use test;
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 -predhat < test.sql
[[email protected] ~]# mysql -p
Enter password:
MariaDB [(none)]> SHOW TRIGGERS FROM testG; # 查看触发器
# 在base2上,编写 gearman 的 worker 端
[[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.78.13‘, 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);
# 这条语句就是将 id 作 KEY 和name 作 VALUE 分开存储,需要和前面写的 php 测试代码的存取一致。
}
?>
[[email protected] ~]# nohup php /usr/local/worker.php &> /dev/null &
3.测试:
# 在mysql端(base4)更新 mysql 中的数据
MariaDB [test]> update test set name=‘lala‘ where id=1;
MariaDB [test]> update test set name=‘lala‘ where id=2;
MariaDB [test]> update test set name=‘lala‘ where id=3;
MariaDB [test]> select * from test;
# 在redis端(base3)查看是否同步成功
# 刷新测试页面数据同步