redis为mysql数据加速测试

 

安装php nginx环境

安装nginx
yum install nginx
安装PHP
yum install -y php php-devel php-fpm php-mysql php-common php-devel php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel

systemctl restart nginx && systemctl enable nginx #设置启动和开机启动nginx
systemctl restart php-fpm && systemctl enable php-fpm #设置启动和开机启动php-fpm

 

安装mysql客户端

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-client

 


配置 php支持 nginx
A) 修改php.ini的配置

vi /etc/php.ini 
cgi.fix_pathinfo=1 #将注释去掉,开启PHP的pathinfo伪静态功能。
max_execution_time = 0 #脚本运行的最长时间,默认30秒
max_input_time = 300#脚本可以消耗的时间,默认60秒
memory_limit = 128M#脚本运行最大消耗的内存,根据你的需求更改数值,默认128M
post_max_size = 100M #单提交的最大数据,此项不是限制上传单个文件的大小,而是针对整个表单的提交数据进行限制的。限制范围包括表单提交的所有内容.例如:发表贴子时,贴子标题,内容,附件等…默认8M
upload_max_filesize = 10M#上载文件的最大许可大小 ,默认2M

 


B) 修改php-fpm的配置

vi /etc/php-fpm.d/www.conf 
找到以下两行,解除注释 
listen.owner = nobody 
listen.group = nobody


找下以下两行,将各自的apache改为nginx 
user = apache -> user = nginx 
group = apache -> group = nginx

 

C) 修改nginx的配置

vi /etc/nginx/conf.d/default.conf 
server {
listen 80;
server_name 192.168.1.63;


root /usr/share/nginx/html;


location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}


error_page 404 /404.html;


# redirect server error pages to the static page /50x.html 
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}


location ~ \.php$ {
root /usr/share/nginx/html;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}


}

 


vi /usr/share/nginx/html/index.php

<?php
phpinfo();
?>

 

systemctl restart nginx

systemctl restart php-fpm

清空旧版本数据库

yum -y remove mariadb*
rm -rf /var/lib/mysql/*

yum -y install mysql-community-server
systemctl start mysqld
grep "password" /var/log/mysqld.log
mysql -uroot -p'&B=o4Fp!Rq_X' #注意临时密码要引号

set global validate_password_policy=0; #定义复杂度
set global validate_password_length=1; #定义长度 默认是8
set password for 'root'@'localhost'=password('123456'); #修改密码正常生产中用复杂密码
flush privileges;

 

exit #退出
Vi /etc/my.cnf
validate-password=OFF #可关闭密码强度审计插件,重启mysql服务

 

安装remi源

yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
安装Redis
yum --enablerepo=remi install -y redis

 

报错mnt目录没挂载光盘
failure: repodata/repomd.xml from CentOS7: [Errno 256] No more mirrors to try.
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"

mount /dev/cdrom /mnt

cp /etc/redis.conf /etc/redis.conf.bak

vi /etc/redis.conf

改:69 bind 127.0.0.1
为:69 bind 0.0.0.0    #redis监听的地址,改为0.0.0.0表示在所有网卡接口上进行监听 
systemctl restart redis

 

[root@web63 ~]# redis-cli
127.0.0.1:6379> exit


Redis-benchmark 压力测试工具
Redis-check-aof 检查redis持久化命令文件的完整性
Redis-check-dump 检查redis持久化数据文件的完整性
Redis-cli redis在linux上的客户端
Redis-sentinel redis-sentinel是集群管理工具,主要负责主从切换。
Redis-server Redis服务器的daemon启动程序


-- 安装php的redis扩展

yum install wget unzip -y
[root@redis ~]# wget -c -t 0 https://github.com/owlient/phpredis/archive/master.zip
[root@redis ~]# unzip master.zip

[root@redis ~]# cd phpredis-master/
[root@redis phpredis-master]# phpize
yum install apr-util apr-util-devel apr apr-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel gcc cmake gcc-c++ -y
[root@redis phpredis-master]# ./configure --with-php-config=/usr/bin/php-config
[root@redis phpredis-master]# make && make install

 

#修改php的配置文件,如果没有“extension=redis.so”,就加上这一行

[root@redis ~]# vi /etc/php.ini
extension=redis.so

systemctl restart php-fpm

 

 

查看php是否已经成功支持 redis


调整部分redis的配置文件

vim /usr/local/redis/redis.conf

daemonize yes
#可修改默认监听端口
port 6379
#修改生成默认日志文件位置
logfile "/var/logs/redis/redis.log"
#配置持久化文件存放位置
dir /usr/local/redis/redisData

systemctl restart redis

五、PHP模拟Redis给mysql数据库加速过程
1、在mysql中插入一些测试数据

[root@redis ~]# mysql -uroot -p123456
mysql> create database mytest;
use mytest;
mysql> CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
mysql> INSERT INTO `test` VALUES (1,'sven'),(2,'jim'),(3,'zhu'),(4,'wang'),(5,'ftd'),(6,'test'),(7,'test01'),(8,'test02'),(9,'test03');
mysql> select * from mytest.test;

 


+----+--------+
| id | name |
+----+--------+
|1 | sven |
|2 | jim |
|3 | zhu |
|4 | wang |
|5 | ftd |
|6 | test |
|7 | test01 |
|8 | test02 |
|9 | test03 |
+----+--------+

编写php的测试代码

vi /usr/share/nginx/html/index2.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379) or die ("could net connect redis server");
$query = "select * from test limit 8";
//为了简单一点,这里就读取了8条数据
for ($key = 1; $key < 9; $key++)
{
if (!$redis->get($key))
{
$connect = mysql_connect('127.0.0.1','root','123456');
mysql_select_db(mytest);
$result = mysql_query($query);
//如果没有找到$key,就将该查询sql的结果缓存到redis
echo "RESULTS FROM MYSQL";
echo "<hr>";
while ($row = mysql_fetch_assoc($result))
{
$redis->set($row['id'],$row['name']);
$id=$row['id'];$name=$row['name'];
echo "number is <b><font color=#225511>$id</font></b>";
echo "<br>";
echo "name is <b><font color=#225511>$name</font></b>";
echo "<br>";

}
$myserver = 'mysql';
exit ;
}
else
{
$myserver = "redis";
$data[$key] = $redis->get($key);
}
}

echo "RESULTS FROM REDIS ";
echo "<hr>";

for ($key = 1; $key < 9; $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>";
}
?>

 

posted @ 2018-11-25 21:54  夜辰雪扬  阅读(982)  评论(0)    收藏  举报