一.架构图
server1:lnmp架构
server2:redis
server3 :mysql
1. 安装 lnmp 环境
[root@server1 ~]# yum install nginx-1.8.0-1.el6.ngx.x86_64.rpm php-*
nginx php php-fpm php-cli php-common php-gd php-mbstring php-mysql
php-pdo php-devel mysql mysql-server
1.1配置php
root@server1 php.d]# vim /etc/php.ini ## 修改时区
date.timezone = Asia/Shanghai
[root@server1 php.d]# cd /etc/php-fpm.d/
[root@server1 php-fpm.d]# ls
www.conf
[root@server1 php-fpm.d]# vim www.conf ##修改用户和组
[root@server1 php-fpm.d]# /etc/init.d/php-fpm restart ##重启服务
Stopping php-fpm: [ OK ]
Starting php-fpm:
1.2配置nginx
[root@server1 nginx]# cd conf.d/
[root@server1 conf.d]# ls
default.conf example_ssl.conf
[root@server1 conf.d]# vim default.conf
[root@server1 conf.d]# /etc/init.d/nginx start
Starting nginx: [ OK ]
1.3测试是否搭建成功
[root@server1 conf.d]# cd /usr/share/nginx/html/
[root@server1 html]# ls
50x.html index.html
[root@server1 html]# vim idenx.php
[root@server1 html]# cat idenx.php
<?php
phpinfo()
?>
2. 安装 php 的 redis 扩展
[root@server1 ~]# unzip phpredis-master.zip
[root@server1 ~]# cd phpredis-master
[root@server1 phpredis-master]# ls
common.h debian library.c php_redis.h redis_session.c tests
config.m4 debian.control library.h README.markdown redis_session.h
CREDITS igbinary mkdeb-apache2.sh redis.c serialize.list
[root@server1 phpredis-master]# phpize ##打开php扩展
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@server1 phpredis-master]# ./configure
[root@server1 phpredis-master]# make && make install
Installing shared extensions: /usr/lib64/php/modules/
root@server1 modules]# cd /etc/php.d/ ##php服务的默认目录
[root@server1 php.d]# cp mysql.ini redis.ini
[root@server1 php.d]# vim redis.ini
root@server1 php.d]# /etc/init.d/php-fpm reload ##刷新php是其平滑加载
Reloading php-fpm: [12-Aug-2018 12:05:12] NOTICE: configuration file /etc/php-fpm.conf test is successful
[ OK ]
[root@server1 php.d]# php -m |grep redis
redis
3.配置mysql
root@server3 mysql]# /etc/init.d/mysqld start
[root@server3 mysql]# mysql -p
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| test |
+——————–+
3 rows in set (0.00 sec)
mysql> grant all on test.* to redis@’%’ identified by ‘westos’; ##添加一个用户
Query OK, 0 rows affected (0.00 sec)
测试:
[root@server1 ~]# cp test.php /usr/share/nginx/html/index.php
cp: overwrite `/usr/share/nginx/html/index.php'? y
root@server1 ~]# cd /usr/share/nginx/html/
[root@server1 html]# ls
50x.html index.html index.php
[root@server1 html]# vim index.php
[root@server1 ~]# scp test.sql server3:/root
test.sql
server3测试
[root@server3 ~]# vim test.sql
[root@server3 ~]# mysql < test.sql
mysql> 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
mysql> select * from test
-> ;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
| 6 | test6 |
| 7 | test7 |
| 8 | test8 |
| 9 | test9 |
+----+-------+
9 rows in set (0.00 sec)
打开redis
浏览器测试是否成功
Server2:测试
[root@server2 ~]# redis-cli
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> get 3
"test3"
127.0.0.1:6379> get 4
"test4"
127.0.0.1:6379> get 8
"test8"
这样情况下mysql和woker是分开的,当mysql更新之后,redis不会更新
一.下面要编写的 mysql 触发器,就相当于 Gearman 的客户端。修改表,插入表就相当于直接下发任务。然后通过 lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格后在通过 gearman-mysql-udf 插件将任务加入到 Gearman 的任务队列中,最后通过redis_worker.php,也就是 Gearman 的 worker 端来完成 redis 数据库的更新。
1. 安装 gearman 软件包:
[root@server1 ~]# yum install -y gearmand-1.1.8-2.el6.x86_64.rpm libgearman-* libevent-*
[root@server1 ~]# service gearmand start
Starting gearmand: [ OK ]
[root@server1 ~]# netstat -antlp |grep gearmand
tcp 0 0 0.0.0.0:4730 0.0.0.0:* LISTEN 4410/gearmand
tcp 0 0 :::4730 :::* LISTEN 4410/gearmand
2. 安装 lib_mysqludf_json
lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式。通常,数据库中的数据映射为 JSON 格式,是通过程序来转换的
root@server3 ~]# unzip lib_mysqludf_json-master.zip
[root@server3 lib_mysqludf_json-master]# yum install -y gcc mysql-devel
[root@server3 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
[root@server3 lib_mysqludf_json-master]# ll lib_mysqludf_json.so
-rwxr-xr-x 1 root root 22223 Aug 12 14:45 lib_mysqludf_json.so
拷贝 lib_mysqludf_json.so 模块:
root@server3 lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
查看 mysql 的模块目录:
mysql> show global variables like 'plugin_dir';
+---------------+-------------------------+
| Variable_name | Value |
+---------------+-------------------------+
| plugin_dir | /usr/lib64/mysql/plugin |
+---------------+-------------------------+
1 row in set (0.00 sec)
mysql> CREATE FUNCTION json_object RETURNS STRING SONAME ##插入函数
-> 'lib_mysqludf_json.so';
Query OK, 0 rows affected (0.00 sec)
mysql> select * from mysql.func;
+-------------+-----+----------------------+----------+
| name | ret | dl | type |
+-------------+-----+----------------------+----------+
| json_object | 0 | lib_mysqludf_json.so | function |
+-------------+-----+----------------------+----------+
1 row in set (0.00 sec)
3. 安装 gearman-mysql-udf:
这个插件是用来管理调用 Gearman 的分布式的队列。
[root@server3 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz
[root@server3 ~]# cd gearman-mysql-udf-0.6
[root@server3 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/
[root@server3 ~]# yum install -y libgearman-* libevent-*
[root@server3 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/
[root@server3 gearman-mysql-udf-0.6]# make && make insta
4.注册 UDF 函数
mysql> CREATE FUNCTION gman_do_background RETURNS STRING SONAME
-> 'libgearman_mysql_udf.so';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME
-> 'libgearman_mysql_udf.so';
Query OK, 0 rows affected (0.00 sec)
查看函数
mysql> 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 |
+--------------------+-----+-------------------------+----------+
5.指定 gearman 的服务信息
mysql> SELECT gman_servers_set('172.25.24.1:4730');
+--------------------------------------+
| gman_servers_set('172.25.24.1:4730') |
+--------------------------------------+
| 172.25.24.1:4730 |
+--------------------------------------+
1 row in set (0.00 sec)
6. 编写 mysql 触发器(根据实际情况编写
[root@server3 ~]# vim test.sql
查看触发器
[root@server3 ~]# mysql < test.sql ##导入
mysql> SHOW TRIGGERS FROM test; ##查看触发器
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| datatoredis | UPDATE | test | BEGIN
SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`));
END | AFTER | NULL | | root@localhost | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+-------------+--------+-------+----------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
二.编写 gearman 的 worker 端(server1)
[root@server1 ~]# vim worker.php
[root@server1 ~]# cp worker.php /usr/local/
1.安装 php 的 gearman 扩展
[root@server1 ~]# tar zxf gearman-1.1.2.tgz
[root@server1 ~]# cd gearman-1.1.2
[root@server1 gearman-1.1.2]# ls
ChangeLog examples php_gearman.h tests
config.m4 LICENSE README test_worker.php
CREDITS php_gearman.c test_client.php
[root@server1 gearman-1.1.2]# phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@server1 gearman-1.1.2]# ./configure
[root@server1 gearman-1.1.2]# make && make install
[root@server1 gearman-1.1.2]# cd /usr/lib64/php/modules/
[root@server1 modules]# ls
curl.so gearman.so mysqli.so pdo.so redis.so
fileinfo.so json.so mysql.so pdo_sqlite.so sqlite3.so
gd.so mbstring.so pdo_mysql.so phar.so zip.so
[root@server1 modules]# ll gearman.so
-rwxr-xr-x 1 root root 280170 Aug 12 15:10 gearman.so
[root@server1 modules]# cd /etc/php.d/
[root@server1 php.d]# cp redis.ini gearman.ini
[root@server1 php.d]# vim gearman.ini
root@server1 php.d]# /etc/init.d/php-fpm reload
Reloading php-fpm: [12-Aug-2018 15:12:04] NOTICE: configuration file /etc/php-fpm.conf test is successful
[ OK ]
[root@server1 php.d]#
[root@server1 php.d]# php -m |grep gearman
gearman
2.将woker打入后台
root@server1 local]# nohup php /usr/local/worker.php &
[3] 6786
[root@server1 local]# nohup: ignoring input and appending output to `nohup.out'
测试:
Server3:
mysql> 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
mysql> update test set name='redhat';
Query OK, 9 rows affected (0.47 sec)
Rows matched: 9 Changed: 9 Warnings: 0
mysql> update test set name='xiaoqin';
Query OK, 9 rows affected (0.07 sec)
Rows matched: 9 Changed: 9 Warnings: