【1213】redis(下)慢查询日志,php安装redis扩展,储存session,主从配置
21.18 redis慢查询日志
21.19 php安装redis扩展
21.20 redis存储session
21.21 redis主从配置
21.18 redis慢查询日志
针对慢查询日志,可以设置两个参数,一个是执行时长,单位是微秒,另一个是慢查询日志的长度。当一个新的命令被写入日志时,最老的一条会从命令日志队列中被移除
1、编辑配置文件vim /etc/redis.conf
slowlog-log-slower-than 1000 //单位us,表示慢于1000us则记录日志
slowlog-max-len 128 //定义日志长度,表示最多存128条
2、测试
1)将 slowlog-log-slower-than 设置为10微秒,重启 redis
[root@arslinux-01 ~]# vim /etc/redis.conf
slowlog-log-slower-than 10 //实验环境任何一个查询都是大于10us的
[root@arslinux-01 ~]# killall redis-server
[root@arslinux-01 ~]# redis-server /etc/redis.conf
[root@arslinux-01 ~]# redis-cli -a 'arslinux>com'
127.0.0.1:6379> SLOWLOG get
1) 1) (integer) 0
2) (integer) 1563117351
3) (integer) 738
4) 1) "COMMAND"
5) "127.0.0.1:52340"
6) ""
127.0.0.1:6379> keys *
1) "k3"
2) "hash1"
3) "list2"
4) "hash2"
5) "seta"
6) "set1"
7) "list1"
8) "setd"
9) "setc"
10) "mykey"
11) "k1"
12) "sete"
13) "set3"
127.0.0.1:6379> get k3
"a"
127.0.0.1:6379> SLOWLOG get
1) 1) (integer) 2
2) (integer) 1563117380
3) (integer) 47
4) 1) "keys"
2) "*"
5) "127.0.0.1:52340"
6) ""
2) 1) (integer) 1
2) (integer) 1563117356
3) (integer) 34
4) 1) "SLOWLOG"
2) "get"
5) "127.0.0.1:52340"
6) ""
3) 1) (integer) 0
2) (integer) 1563117351
3) (integer) 738
4) 1) "COMMAND"
5) "127.0.0.1:52340"
6) ""
127.0.0.1:6379> SLOWLOG get 2 //只列出最新的2条
1) 1) (integer) 4
2) (integer) 1563117461
3) (integer) 20
4) 1) "SLOWLOG"
2) "get"
3) "1"
5) "127.0.0.1:52340"
6) ""
2) 1) (integer) 3
2) (integer) 1563117386
3) (integer) 48
4) 1) "SLOWLOG"
2) "get"
5) "127.0.0.1:52340"
6) ""
127.0.0.1:6379> SLOWLOG len //查看慢查询日志条数
(integer) 6
21.19 php安装redis扩展
1、下载、解压
[root@arslinux-01 ~]# cd /usr/local/src/
[root@arslinux-01 src]# wget https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/21NOSQL/phpredis.zip
解压 unzip phpredis.zip
2、生成 .config 文件
[root@arslinux-01 src]# cd phpredis-develop/
[root@arslinux-01 phpredis-develop]# /usr/local/php-fpm/bin/phpize
\Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
3、编译安装
[root@arslinux-01 phpredis-develop]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
[root@arslinux-01 phpredis-develop]# make && make install
4、编辑 php.ini,增加一行 extension=redis.so
[root@arslinux-01 phpredis-develop]# vim /usr/local/php-fpm/etc/php.ini
extension=redis.so
5、检查 redis 模块是否安装成功
[root@arslinux-01 phpredis-develop]# /usr/local/php-fpm/bin/php -m |grep redis
redis
6、重启 php-fpm 服务
[root@arslinux-01 phpredis-develop]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
21.20 redis存储session
1、根据之前的经验,直接在 php-fpm 配置对应的 pool 里增加
[root@arslinux-01 phpredis-develop]# vim /usr/local/php-fpm/etc/php-fpm.d/arslinux.conf
php_value[session.save_handler] = redis
php_value[session.save_path] = "tcp://127.0.0.1:6379"
2、重启 php-fpm 和 去除密码配置 重启redis
[root@arslinux-01 phpredis-develop]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@arslinux-01 phpredis-develop]# killall redis-server
[root@arslinux-01 phpredis-develop]# redis-server /etc/redis.conf
3、下载测试文件
因为之前已经下载了,在 /data/wwwroot/default/下并改名为 1.php
4、直接 curl
[root@arslinux-01 default.com]# curl localhost/1.php
1563118794<br><br>1563118794<br><br>5tih1g88dm4amd6614iihfuvb2
[root@arslinux-01 default.com]# curl localhost/1.php
1563118796<br><br>1563118796<br><br>70rnt7unik8eie33co32pdg155
[root@arslinux-01 default.com]# curl localhost/1.php
1563118799<br><br>1563118799<br><br>i9pmk3ht9nccqro6vh1c6bnh16
[root@arslinux-01 default.com]# curl localhost/1.php
1563118800<br><br>1563118800<br><br>3sgv8fmn2csf5vmcsbgbaqf351
[root@arslinux-01 default.com]# curl localhost/1.php
1563118801<br><br>1563118801<br><br>1lrs39of4hashv86t2rntr5tc0
5、进入 redis,查看 keys 对应的 value是否匹配
[root@arslinux-01 default.com]# redis-cli
127.0.0.1:6379> keys *
1) "set3"
2) "PHPREDIS_SESSION:i9pmk3ht9nccqro6vh1c6bnh16"
3) "setc"
4) "PHPREDIS_SESSION:1lrs39of4hashv86t2rntr5tc0"
5) "list1"
6) "sete"
7) "list2"
8) "hash1"
9) "PHPREDIS_SESSION:3sgv8fmn2csf5vmcsbgbaqf351"
10) "seta"
11) "PHPREDIS_SESSION:70rnt7unik8eie33co32pdg155"
12) "PHPREDIS_SESSION:5tih1g88dm4amd6614iihfuvb2"
13) "setd"
14) "mykey"
15) "set1"
16) "k1"
17) "k3"
18) "hash2"
127.0.0.1:6379> get PHPREDIS_SESSION:5tih1g88dm4amd6614iihfuvb2
"TEST|i:1563118794;TEST3|i:1563118794;"
127.0.0.1:6379> get PHPREDIS_SESSION:70rnt7unik8eie33co32pdg155
"TEST|i:1563118796;TEST3|i:1563118796;"
6、成功!!
还有两种方法:
1、vim /usr/local/php-fpm/etc/php.ini//更改或增加
session.save_handler = “redis”
session.save_path = “tcp://127.0.0.1:6379”
2、apache虚拟主机配置文件中也可以这样配置:
php_value session.save_handler " redis" php_value session.save_path " tcp://127.0.0.1:6379"
如果想用 php 连接 redis cluster,需要使用 predis 扩展
安装方法类似 phpredis,predis 扩展地址 https://github.com/nrk/predis(针对集群的)
21.21 redis主从配置
为了节省资源,我们可以在一台机器上启动两个redis服务
1、拷贝配置文件并根据实际更改,以区分两个 redis
[root@arslinux-01 ~]# cp /etc/redis.conf /etc/redis2.conf
[root@arslinux-01 ~]# vim /etc/redis2.conf
port 6380
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis2.log"
dir /data/redis2/
slaveof 127.0.0.1 6379 //增加一行主服务器的ip,端口号
如果主配置了 requirepass ,那么从上也需要配置同样的设置
2、创建 redis2 的目录
[root@arslinux-01 ~]# mkdir /data/redis2/
3、启动 reids2
[root@arslinux-01 ~]# redis-server /etc/redis2.conf
[root@arslinux-01 ~]# ps aux|grep redis
root 8128 0.3 0.9 147348 9856 ? Ssl 12:03 0:00 redis-server 127.0.0.1:6379
root 8151 0.0 0.9 147348 9736 ? Ssl 12:03 0:00 redis-server 127.0.0.1:6380
root 8158 0.0 0.0 112724 988 pts/0 R+ 12:03 0:00 grep --color=auto redis
[root@arslinux-01 ~]# netstat -lntp |grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 8128/redis-server 1
tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 8151/redis-server 1
4、测试
[root@arslinux-01 ~]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
1) "set1"
2) "setd"
3) "set3"
4) "mykey"
5) "sete"
6) "list2"
7) "setc"
8) "hash2"
9) "hash1"
10) "list1"
11) "seta"
12) "k3"
13) "k1"
[root@arslinux-01 ~]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> keys *
1) "set1"
2) "k1"
3) "seta"
4) "hash2"
5) "mykey"
6) "hash1"
7) "setc"
8) "sete"
9) "list1"
10) "k3"
11) "list2"
12) "set3"
13) "setd"
127.0.0.1:6380> CONFIG GET dir
(error) ERR unknown command 'CONFIG'
127.0.0.1:6380> arslinux get dir
1) "dir"
2) "/data/redis2"
127.0.0.1:6380> arslinux get dbfilename
1) "dbfilename"
2) "dump.rdb"
127.0.0.1:6380> arslinux get slaveof
1) "slaveof"
2) "127.0.0.1 6379"
因为配置文件中 slave-read-only yes 限制了只能读不可写,改为 no 则可写
127.0.0.1:6380> set key19 111
(error) READONLY You can't write against a read only slave.
注意: redis主从和mysql主从不一样,redis主从不用事先同步数据,它会自动同步过去