redis的安装
下载redis: wget http://download.redis.io/releases/redis-4.0.2.tar.gz
安装redis:
tar xf redis-4.0.2.tar.gz
cd redis-4.0.2
less README.md
make MALLOC=jemalloc
make PREFIX=/application/redis-4.0.2/ install
做软连接和调整redis配置文件的位置
ln -s /application/redis-4.0.2/ /application/redis
mkdir /application/redis/conf
cp redis.conf /application/redis/conf/
当然这里可以设置redis配置文件redis.conf为后台启动:daemonize yes
将redis的命令放到profile的配置文件中
echo "export PATH=/application/redis/bin:$PATH"
source /etc/profile
启动redis
redis-server /application/redis/conf/redis.conf &
启动的时候会有一个警告提示:
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
执行命令sysctl vm.overcommit_memory=1便可
[root@mysql-server redis-4.0.2]# sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1
这个命令执行只是当前环境生效,如果要让其永久生效,应该将vm.overcommit_memory = 1放到sysctl.conf的配置文件中即可
这时候再启动redis,又会报一个警告如下:
WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
按照提示操作如下:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
如果要让redis-server重启生效就要将此命令加入到/etc/rc.local中即可,如下:
vim /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
这样再启动redis就没有问题了
[root@mysql-server redis-4.0.2]# redis-server /application/redis/conf/redis.conf &
redis支持的数据类型很多,比如list、string、 set、hash、sorted_set等,那么如何查看这些类型所支持的命令呢,可以通过help来查看各个类型对应的命令方法
[root@mysql-server redis-4.0.2]# redis-cli
127.0.0.1:6379> help @list
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0
BRPOP key [key ...] timeout
summary: Remove and get the last element in a list, or block until one is available
since: 2.0.0
BRPOPLPUSH source destination timeout
summary: Pop a value from a list, push it to another list and return it; or block until one is available
since: 2.2.0
LINDEX key index
summary: Get an element from a list by its index
since: 1.0.0
LINSERT key BEFORE|AFTER pivot value
summary: Insert an element before or after another element in a list
since: 2.2.0
LLEN key
summary: Get the length of a list
since: 1.0.0
LPOP key
summary: Remove and get the first element in a list
since: 1.0.0
LPUSH key value [value ...]
summary: Prepend one or multiple values to a list
since: 1.0.0
LPUSHX key value
summary: Prepend a value to a list, only if the list exists
since: 2.2.0
LRANGE key start stop
summary: Get a range of elements from a list
since: 1.0.0
LREM key count value
summary: Remove elements from a list
since: 1.0.0
LSET key index value
summary: Set the value of an element in a list by its index
since: 1.0.0
LTRIM key start stop
summary: Trim a list to the specified range
since: 1.0.0
RPOP key
summary: Remove and get the last element in a list
since: 1.0.0
RPOPLPUSH source destination
summary: Remove the last element in a list, prepend it to another list and return it
since: 1.2.0
RPUSH key value [value ...]
summary: Append one or multiple values to a list
since: 1.0.0
RPUSHX key value
summary: Append a value to a list, only if the list exists
since: 2.2.0
为php添加redis客户端扩展
下载php的redis扩展工具:https://github.com/nicolasff/php-hiredis/archive/master.zip
安装redis扩展
[root@lnmp01 tools]# unzip master.zip
[root@lnmp01 tools]# cd php-hiredis-master/
[root@lnmp01 php-hiredis-master]# /application/php/bin/phpize
[root@lnmp01 php-hiredis-master]# ./configure --with-php-config=/application/php/bin/php-config
[root@lnmp01 php-hiredis-master]# make && make install
[root@lnmp01 php-hiredis-master]# ll /application/php/lib/php/extensions/no-debug-non-zts-20090626/
总用量 204
-rwxr-xr-x 1 root root 207697 10月 12 16:26 hiredis.so
接下来php连接redis的配置,修改php全局配置文件php.ini,在php.ini文件中修改和添加如下内容:
[root@lnmp01 blog]# vim /application/php/lib/php.ini
extension_dir = "/application/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension = hiredis.so
重启php,使php配置文件生效
pkill php-fpm
/application/php/sbin/php-fpm
redis主从复制
修改从库的配置文件
[root@mysql-server redis-4.0.2]# vim /application/redis/conf/redis.conf
#slaveof <masterip> <masterport>
slaveof 192.168.182.144 6379
重启redis,这样主从复制就可以同步了
可以在从库上监控从库的变化
redis-cli -h localhost -p 6379 monitor
查看redis的统计信息
[root@mysql-server redis-4.0.2]# redis-cli -h localhost -p 6379 info
# Server
redis_version:4.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b6b7a8185ace1117
redis_mode:standalone
os:Linux 2.6.32-504.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:sync-builtin
gcc_version:4.4.7
process_id:17590
run_id:9921ed0c686d4789cd6ab4a7444fd50f0fba186e
tcp_port:6379
uptime_in_seconds:7874
uptime_in_days:0
hz:10
lru_clock:14626962
executable:/home/oldboy/tools/redis-4.0.2/redis-server
config_file:/application/redis/conf/redis.conf
# Clients
使用redis主要要会使用help,不清楚的地方用help查看帮助就行了
redis实现了主从复制后,这样就可以对redis做lvs负载均衡处理了,前台来访问redis缓存服务就可以通过lvs对redis做轮训访问了。