[root@server12 ~]# cd rhel7/
[root@server12 rhel7]# yum install -y php-* libgearman-* libevent-* ##server12上安装php-fpm
[root@server12 rhel7]# systemctl start php-fpm.service ##启动php-fpm
[root@server12 rhel7]# netstat -antulp ##查看是否开启相关端口
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4121/php-fpm: maste
[root@server11 local]# rsync -a nginx server12:/usr/local/ ##把server11上配置好的nginx复制到server12指定目录中
[root@server12 ~]# cd /usr/local/nginx/
[root@server12 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@server12 nginx]# cd conf/
[root@server12 conf]# cp nginx.conf.default nginx.conf
cp: overwrite ‘nginx.conf’? y
[root@server12 conf]# vim nginx.conf ##打开php站点注释
[root@server12 ~]# vim .bash_profile ##配置环境变量
[root@server12 ~]# source .bash_profile
[root@server12 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server12 ~]# nginx ##开启nginx
[root@server12 ~]# php -m | grep mysql
mysql
mysqli
pdo_mysql
[root@server12 ~]# php -m | grep redis
redis
37
server13上启动redis
[root@server13 ~]# systemctl start redis_server
[root@server13 ~]# redis-cli
server14安装mysql
[root@server14 local]# yum install -y mariadb-server
[root@server14 local]# systemctl start mariadb
[root@server14 local]# mysql
[root@server12 ~]# cd rhel7
[root@server12 rhel7]# ll test.php
-rw-r--r-- 1 root root 1369 Apr 16 10:34 test.php
[root@server12 rhel7]# cp test.php /usr/local/nginx/html/ ##复制测试文件到nginx默认发布目录
[root@server12 rhel7]# cd /usr/local/nginx/html/
[root@server12 html]# vim test.php
[root@server14 local]# mysql
MariaDB [(none)]> show databases;
MariaDB [(none)]> grant all on test.* to redis@'%' identified by 'westos'; ##授权远程用户连接
[root@server12 rhel7]# scp test.sql server14: ##将测试数据复制到mysql主机中
[root@server14 ~]# cat test.sql
[root@server14 ~]# mysql < test.sql ##测试数据导入数据库
[root@server14 ~]# mysql
MariaDB [(none)]> use test
MariaDB [test]> show tables;
MariaDB [test]> select * from test; ##查看test库中生成的测试内容
[root@server13 ~]# redis-cli
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
server14
MariaDB [test]> update test set name='westos' where id=1;
server13
127.0.0.1:6379> DEL 1
(integer) 1
lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式。通常,数据库中的数据映射为 JSON 格式,是通过程序来转换的。
在mysql主机上(server14):
unzip lib_mysqludf_json-master.zip
cd lib_mysqludf_json-master/
yum install -y mysql-devel gcc
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c ##生成so文件
拷贝 lib_mysqludf_json.so 模块:
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 |
+---------------+-------------------------+
注册 UDF 函数
mysql> CREATE FUNCTION json_object RETURNS STRING SONAME
'lib_mysqludf_json.so';
查看函数
mysql> select * from mysql.func;
+--------------------+-----+-------------------------+----------+
| name | ret | dl | type |
+--------------------+-----+-------------------------+----------+
| json_object | 0 | lib_mysqludf_json.so | function |
+--------------------+-----+-------------------------+----------+
server14上:
yum install -y libgearman-* libevent-devel-2.0.21-4.el7.x86_64.rpm
tar zxf gearman-mysql-udf-0.6.tar.gz
cd gearman-mysql-udf-0.6
./configure --libdir=/usr/lib64/mysql/plugin/
make
make install
注册 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';
查看函数
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 |
+--------------------+-----+-------------------------+----------+
指定 gearman 的服务信息
mysql> SELECT gman_servers_set('192.168.0.12:4730'); %指定gearman后台,负责接收mysql更改,但不负责处理,处理交给指定的worker
[root@server12 rhel7]# yum install -y gearmand-1.1.12-18.el7.x86_64.rpm
[root@server12 rhel7]# systemctl start gearmand.service
[root@server12 rhel7]# netstat -antlp | grep 4730
tcp 0 0 0.0.0.0:4730 0.0.0.0:* LISTEN 19099/gearmand
tcp6 0 0 :::4730 :::* LISTEN 19099/gearmand
[root@server12 rhel7]# vim worker.php
[root@server12 rhel7]# php -m | grep redis
redis
[root@server12 rhel7]# nohup php worker.php & ##worker打入后台执行
[root@server12 rhel7]# ps ax
[root@server14 ~]# vim test.sql
use test;
ELIMITER $$
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@server14 ~]# mysql < test.sql
[root@server14 ~]# mysql
MariaDB [(none)]> SHOW TRIGGERS FROM test;
MariaDB [(none)]> use test
MariaDB [test]> update test set name='redhat' where id=1;
[root@server13 ~]# redis-cli
127.0.0.1:6379> get 1
"redhat"
欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) | Powered by Discuz! X2.5 |