redhat6.5
iptables和selinux关闭
server2:172.25.35.52 (nginx、php)
server3:172.25.35.53 (redis)
server4:172.25.35.54 (mysql)
redis的具体安装见:https://blog.csdn.net/Ying_smile/article/details/81978306
本实验是基于上面链接的博客进行的
[root@server2 ~]# ls
nginx-1.8.0-1.el6.ngx.x86_64.rpm
php-5.3.3-38.el6.x86_64.rpm
php-cli-5.3.3-38.el6.x86_64.rpm
php-common-5.3.3-38.el6.x86_64.rpm
php-devel-5.3.3-38.el6.x86_64.rpm
php-fpm-5.3.3-38.el6.x86_64.rpm
php-gd-5.3.3-38.el6.x86_64.rpm
php-mbstring-5.3.3-38.el6.x86_64.rpm
php-mysql-5.3.3-38.el6.x86_64.rpm
php-pdo-5.3.3-38.el6.x86_64.rpm
[root@server2 ~]# yum install nginx-1.8.0-1.el6.ngx.x86_64.rpm
php-* -y
[root@server2 ~]# cd /etc/php.d
[root@server2 php.d]# ls
curl.ini json.ini mysql.ini pdo_sqlite.ini zip.ini
fileinfo.ini mbstring.ini pdo.ini phar.ini
gd.ini mysqli.ini pdo_mysql.ini sqlite3.ini
[root@server2 php.d]# vim /etc/php.ini
946 date.timezone = Asia/Shanghai
[root@server2 php.d]# cd /etc/php-fpm.d
[root@server2 php-fpm.d]# vim www.conf
[root@server2 php-fpm.d]# cd /etc/nginx/
[root@server2 nginx]# cd conf.d/
[root@server2 conf.d]# vim default.conf
[root@server2 conf.d]# nginx -t
[root@server2 conf.d]# nginx -s reload
[root@server2 conf.d]# cd /usr/share/nginx/html
[root@server2 html]# ls
50x.html index.html
[root@server2 html]# vim index.php
[root@server2 html]# /etc/init.d/php-fpm start
[root@server2 html]# vim test.php
connect('172.25.35.53',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.35.54','redis','haha'); //mysql主机ip、用户名、密码
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 "
";
for ($key = 1; $key < 10; $key++)
{
echo "number is $key";
echo "
";
}
?>
[root@server2 ~]# cd redis
[root@server2 redis]# unzip phpredis-master.zip
[root@server2 redis]# cd phpredis-master
[root@server2 phpredis-master]# phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@server2 phpredis-master]# ./configure
[root@server2 phpredis-master]# make
[root@server2 phpredis-master]# make install
Installing shared extensions: /usr/lib64/php/modules/
[root@server2 modules]# cd /etc/php.d/
[root@server2 php.d]# cp mysql.ini redis.ini
[root@server2 php.d]# vim redis.ini
extension=redis.so
[root@server2 php.d]# /etc/init.d/php-fpm reload
[root@server2 php.d]# php -m |grep redis
redis
[root@server3 ~]# /etc/init.d/redis_6379 start
[root@server3 ~]# vim /etc/redis/6379.conf //删除文件里面的这行内容
1310 slaveof 172.25.35.54 6379
[root@server3 ~]# /etc/init.d/redis_6379 restart
[root@server4 ~]# /etc/init.d/redis_6379 stop
[root@server4 ~]# yum install -y mysql-server
[root@server4 ~]# /etc/init.d/mysqld start
[root@server4 ~]# mysql
mysql> show databases;
mysql> grant all on test.* to redis@'%' identified by 'haha';
mysql> quit
[root@server4 ~]# vim test.sql
[root@server4 ~]# mysql < test.sql
[root@server4 ~]# /etc/init.d/mysqld restart
[root@server4 ~]# mysql
mysql> select * from test.test;
浏览器测试:刷新一次,可以查看到右边的内容:
如果数据库修改数据,浏览器没有访问到改变是因为Php先访问redis如果没有再访问mysql
**解决:**通过 mysql 触发器将改变的数据同步到 redis
[root@server2 redis]# yum install libevent-1.4.13-4.el6.x86_64.rpm libevent-devel-1.4.13-4.el6.x86_64.rpm libevent-doc-1.4.13-4.el6.noarch.rpm libevent-headers-1.4.13-4.el6.noarch.rpm libgearman-1.1.8-2.el6.x86_64.rpm libgearman-devel-1.1.8-2.el6.x86_64.rpm -y
[root@server2 redis]# /etc/init.d/gearmand start
[root@server4 ~]# unzip lib_mysqludf_json-master.zip
[root@server4 ~]# cd lib_mysqludf_json-master
[root@server4 lib_mysqludf_json-master]# yum install gcc mysql-devel -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]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
[root@server4 lib_mysqludf_json-master]# cd /usr/lib64/mysql/plugin/
[root@server4 plugin]# mysql
mysql> show global variables like 'plugin_dir';
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;
mysql> quit
Bye
[root@server4 plugin]# cd
[root@server4 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz
[root@server4 ~]# yum install libevent-1.4.13-4.el6.x86_64.rpm libevent-devel-1.4.13-4.el6.x86_64.rpm libevent-doc-1.4.13-4.el6.noarch.rpm libevent-headers-1.4.13-4.el6.noarch.rpm libgearman-1.1.8-2.el6.x86_64.rpm libgearman-devel-1.1.8-2.el6.x86_64.rpm -y
[root@server4 ~]# cd gearman-mysql-udf-0.6
[root@server4 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/
[root@server4 gearman-mysql-udf-0.6]# make && make install
[root@server4 gearman-mysql-udf-0.6]# mysql
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.23 sec)
mysql> select * from mysql.func;
mysql> SELECT gman_servers_set('172.25.35.52:4730');
[root@server2 redis]# tar zxf gearman-1.1.2.tgz
[root@server2 redis]# cd gearman-1.1.2
[root@server2 gearman-1.1.2]# phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@server2 gearman-1.1.2]# ./configure
[root@server2 gearman-1.1.2]# make && make install
[root@server2 gearman-1.1.2]# cd /usr/lib64/php/modules/
[root@server2 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@server2 modules]# cd /etc/php.d/
[root@server2 php.d]# cp redis.ini gearman.ini
[root@server2 php.d]# vim gearman.ini
extension=gearman.so
[root@server2 php.d]# /etc/init.d/php-fpm reload
[root@server2 php.d]# php -m |grep gearman
gearman
[root@server2 php.d]# cd /usr/local/
[root@server2 local]# vim worker.php
addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
$redis = new Redis();
$redis->connect('172.25.35.53', 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);
}
?>
[root@server2 local]# nohup php /usr/local/worker.php & //打入后台运行
[root@server4 gearman-mysql-udf-0.6]# cd
[root@server4 ~]# vim test.sql
[root@server4 ~]# mysql < test.sql
[root@server4 ~]# mysql
mysql> use test;
Database changed
mysql> update test set name='redhat';
Query OK, 9 rows affected (0.80 sec)
Rows matched: 9 Changed: 9 Warnings: 0
[root@server3 ~]# redis-cli
127.0.0.1:6379> get 1
"redhat"
127.0.0.1:6379> get 2
“redhat"
mysql> update test set name='haha';
Query OK, 9 rows affected (0.09 sec)
Rows matched: 9 Changed: 9 Warnings: 0
[root@server3 ~]# redis-cli
127.0.0.1:6379> get 1
"haha"
127.0.0.1:6379> get 2
“haha"