Redis优化MySQL-爱代码爱编程
文章目录
1.安装 gcc*
设置yum源可以参考之前的文章
https://blog.csdn.net/w918589859/article/details/109191537
[root@localhost ~]$ yum -y install gcc*
2.安装所需要的包
软件包百度网盘:
链接:https://pan.baidu.com/s/1rQ7_9XU2xSsg691bp-QrPw 提取码:u1mi
[root@localhost ~]$ yum -y install lrzsz unzip #安装lrzsz,源码包直接拖进shell即可
[root@localhost ~]$ unzip redis-mysql.zip && cd redis-mysql
#之前存在的删除掉
[root@localhost ~]$ rpm -qa | grep -i mysql
[root@localhost ~]$ yum remove mysql-libs
[root@localhost ~]$ rpm -qa | grep libstdc++
[root@localhost ~]$ rpm -e libstdc++-4.4.7-23.el6.x86_64 --nodeps
[root@localhost ~]$ yum -y install * #安装所有软件包
3.配置网站 nginx 并启动 nginx
配置server
[root@localhost ~]$ cd /etc/nginx/
[root@localhost ~]$ vim nginx.conf
include /etc/nginx/conf.d/*.conf;
[root@localhost ~]$ vim /etc/nginx/conf.d/default.conf
server { #具体的某一网站的配置信息
listen 80; #监听端口
server_name www.abc.com; #修改这里,域名
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /www; #网页根目录(/usr/local/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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index 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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#取消注释
location ~ \.php$ {
root /www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
启动 nginx
[root@localhost ~]$ service nginx start
修改www.conf文件
[root@localhost ~]$ vim /etc/php-fpm.d/www.conf
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
4.启动 php 和数据库
[root@localhost ~]$ service php-fpm start
[root@localhost ~]$ service mysqld start
5.授权,使登录数据库时使用‘123456’密码
[root@localhost ~]$ mysqladmin -uroot password 123456
[root@localhost ~]$ mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
6.测试网站和 php 的连通性
[root@localhost ~]$ mkdir /www
[root@localhost ~]$ echo "<?php phpinfo();" > /www/index.php #php测试页面
浏览器输入服务器的ip测试
7.安装 redis
[root@localhost ~]$ cd /root/redis-mysql
[root@localhost ~]$ tar -zxvf redis-2.8.19.tar.gz && cd redis-2.8.19
[root@localhost ~]$ make #编译
[root@localhost ~]$ make PREFIX=/usr/local/redis install
#指定安装位置,如果没有指定安装位置PREFIX=/usr/local/redis
#则 make install 会把 redis 安装到/usr/local/bin/目录下
[root@localhost ~]$ mkdir /usr/local/redis/etc
[root@localhost ~]$ cp -a ./redis.conf /usr/local/redis/
#复制 Redis 的配置文件到/usr/local/redis/下,便于管理。
8.安装提供 php 和 redis 联系的软件
[root@localhost ~]$ cd /root/redis-mysql
[root@localhost ~]$ unzip phpredis-master.zip && cd phpredis-master && phpize
#检查依赖关系,检查编译工具,指定安装的功能,位置
[root@localhost ~]$ ./configure --with-php-config=/usr/bin/php-config
[root@localhost ~]$ make && make install #编译安装
让php支持redis
[root@localhost ~]$ vim /etc/php.ini
#最末尾添加一行
extension=redis.so #在php文件中添加redis
[root@localhost ~]$ service php-fpm restart
浏览器输入ip地址访问搜索redis
9.进入 mysql 插数据
[root@localhost ~]$ mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database mytest;
Query OK, 1 row affected (0.00 sec)
mysql> use mytest;
Database changed
mysql> create table test(id int not null auto_increment,name char(20) default null,primary key (id))engine=innodb auto_increment=10 default charset=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> describe test;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into test values (1,'a1'),(2,'a2'),(3,'a3');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
10.开启 redis,并编写脚本
[root@localhost ~]$ ln -s /usr/local/redis/bin/* /usr/local/bin/ #添加软连接
[root@localhost ~]$ vim /usr/local/redis/redis.conf
daemonize no #修改为 yes #后台启动
[root@localhost ~]$ pkill redis
[root@localhost ~]$ redis-server /usr/local/redis/redis.conf #启动从服务redis
[root@localhost ~]$ vim /www/redis.php
<?php
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
//开启debug
// mysql 库: mytest 表:test
$redis = new redis();
$redis->connect('127.0.0.1',6379);
$query = "select * from test limit 5";
for ($key=1;$key<=5;$key++)
{
if (!$redis->get($key))
//判断redis中是否有1 2 3 4 5 的键,没有连接数据库查询mytest库的test表,然后插入到redis中
{
$connect = mysql_connect('127.0.0.1','root','123456');
mysql_select_db(mytest);
$result = mysql_query($query);
var_dump ($result);
while ($row = mysql_fetch_assoc($result))
{
$redis->setex($row['id'],30,$row['name']);
//从MySQL中获取的资源插入到redis中,并设置有效时间为30s
}
$myserver = 'mysql';
break;
}
else
//判断redis中是否有1 2 3 4 5 的键,有直接打印redis中的 1 2 3 4 5 键的值。
{
$myserver = "redis";
$data[$key] = $redis->get($key);
}
}
echo $myserver;
echo "<br>";
for ($key=1;$key<=5;$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>";
}
?>
11.验证 php 访问 redis 和 mysql
浏览器输入ip地址/redis.php
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/w918589859/article/details/111087329