编辑配置文件/etc/redis.conf
针对慢查询日志,可以设置两个参数,一个是执行时长,单位是微秒,另一个是慢查询日志的长度。当一个新的命令被写入日志时,最老的一条会从命令日志队列中被移除。
slowlog-log-slower-than 1000 //单位ms,表示慢于1000ms则记录日志
slowlog-max-len 128 //定义日志长度,表示最多存128条
slowlog get //列出所有的慢查询日志
slowlog get 2 //只列出2条
slowlog len //查看慢查询日志条数
##redis慢查询日志默认是开启的: [root@Dasoncheng ~]# vim /etc/redis.conf # The following time is expressed in microseconds, so 1000000 is equivalent # to one second. Note that a negative number disables the slow log, while # a value of zero forces the logging of every command. slowlog-log-slower-than 10000 # There is no limit to this length. Just be aware that it will consume memory. # You can reclaim memory used by the slow log with SLOWLOG RESET. slowlog-max-len 128
这里我们将执行时常修改为10微秒,看看效果:
# The following time is expressed in microseconds, so 1000000 is equivalent # to one second. Note that a negative number disables the slow log, while # a value of zero forces the logging of every command. slowlog-log-slower-than 10 ##正常实验环境下,查询都会超过10微秒 # There is no limit to this length. Just be aware that it will consume memory. # You can reclaim memory used by the slow log with SLOWLOG RESET. slowlog-max-len 128 #保存退出 [root@Dasoncheng ~]# killall redis-server [root@Dasoncheng ~]# redis-server /etc/redis.conf [root@Dasoncheng ~]# redis-cli 127.0.0.1:6379> slowlog get ##这里显示0为1条慢查询日志 1) 1) (integer) 0 2) (integer) 1507942013 3) (integer) 899 4) 1) "COMMAND" 5) "127.0.0.1:40790" 6) "" 127.0.0.1:6379> keys * 1) "k3" 2) "key2" 3) "setc" 4) "hash1" 5) "s1" 6) "lista" 7) "set1" 8) "l1" 9) "setd" 10) "set2" 11) "list" 12) "key10" 13) "setb" 14) "key1" 15) "k2" 16) "sete" 17) "k1" 18) "seta" 19) "set3" 127.0.0.1:6379> get k3 "3" 127.0.0.1:6379> get key2 "200" 127.0.0.1:6379> slowlog get 1) 1) (integer) 3 2) (integer) 1507942047 3) (integer) 58 4) 1) "get" 2) "key2" 5) "127.0.0.1:40790" 6) "" 2) 1) (integer) 2 2) (integer) 1507942031 3) (integer) 29 4) 1) "keys" 2) "*" 5) "127.0.0.1:40790" 6) "" 3) 1) (integer) 1 2) (integer) 1507942021 3) (integer) 22 4) 1) "slowlog" 2) "get" 5) "127.0.0.1:40790" 6) "" 4) 1) (integer) 0 2) (integer) 1507942013 3) (integer) 899 4) 1) "COMMAND" 5) "127.0.0.1:40790" 6) "" 127.0.0.1:6379> slowlog get 2 ##只获取最近两条慢查询日志; 1) 1) (integer) 4 2) (integer) 1507942052 3) (integer) 41 4) 1) "slowlog" 2) "get" 5) "127.0.0.1:40790" 6) "" 2) 1) (integer) 3 2) (integer) 1507942047 3) (integer) 58 4) 1) "get" 2) "key2" 5) "127.0.0.1:40790" 6) "" 127.0.0.1:6379> slowlog len ##显示所有慢查询日志条数; (integer) 6 21.19 php安装redis扩展模块
cd /usr/local/src wget https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/21NOSQL/phpredis.zip unzip phpredis.zip cd phpredis-develop /usr/local/php-fpm/bin/phpize ./configure --with-php-config=/usr/local/php-fpm/bin/php-config make make install vim /usr/local/php.ini//增加extension=redis.so /usr/local/php-fpm/bin/php -m|grep redis//看是否有redis模块 重启php-fpm服务 21.20 redis存储session vim /usr/local/php-fpm/etc/php.ini//更改或增加
session.save_handler = "redis"
session.save_path = "tcp://127.0.0.1:6379" 或者apache虚拟主机配置文件中也可以这样配置:
php_value session.save_handler " redis"
php_value session.save_path " tcp://127.0.0.1:6379" 或者php-fpm配置文件对应的pool中增加:
php_value[session.save_handler] = redis
php_value[session.save_path] = " tcp://127.0.0.1:6379 "
wgt http://study.lishiming.net/.mem_se.txt
mv .mem_se.txt /usr/local/apache2/htdocs/session.php
其中session.php内容可以参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php
curl localhost/session.php //结果类似于1443702394
1443702394
i44nunao0g3o7vf2su0hnc5440
命令行连接redis,也可以查看到该key以及对应的值
如果想用php连接redis cluster,需要使用predis扩展
安装方法类似phpredis,predis扩展地址https://github.com/nrk/predis
为了节省资源,我们可以在一台机器上启动两个redis服务
cp /etc/redis.conf /etc/redis2.conf
vim /etc/redis2.conf //需要修改port,dir,pidfile,logfile
还要增加一行
slaveof 127.0.0.1 6379
如果主上设置了密码,还需要增加
masterauth aminglinux>com //设置主的密码
启动之前不要忘记创建新的dir目录
redis-server /etc/redis2.conf
测试:在主上创建新的key,在从上查看
注意:redis主从和mysql主从不一样,redis主从不用事先同步数据,它会自动同步过去
[root@Dasoncheng ~]# vim /etc/redis2.conf [root@Dasoncheng ~]# mkdir /data/redis2 [root@Dasoncheng ~]# redis-server /etc/redis2.conf [root@Dasoncheng ~]# ps aux |grep redis root 6411 0.2 0.2 142920 2284 ? Ssl 08:46 0:02 redis-server 127.0.0.1:6379 root 6498 0.1 0.2 142788 2136 ? Ssl 09:06 0:00 redis-server 127.0.0.1:6333 root 6505 0.0 0.0 112660 964 pts/2 S+ 09:06 0:00 grep --color=auto redis [root@Dasoncheng ~]# redis-cli -h 127.0.0.1 -p 6333 127.0.0.1:6333> keys * 1) "set1" 2) "seta" 3) "key2" 4) "l1" 5) "key10" 6) "lista" 7) "s1" 8) "k1" 9) "k2" 10) "set3" 11) "key1" 12) "k3" 13) "setd" 14) "set2" 15) "sete" 16) "list" 17) "setc" 18) "setb" 19) "hash1"