安装redis
apt-get installl redis-server
安装php-redis
apt-get installl php-redis
检查redis进程
ps -aux|grep redis
通过命令行访问redis 基本操作
root@ubuntu16:~# redis-cli
# 查看所有的key列表
127.0.0.1:6319>keys *
(empty list or set)
# 增加一条记录key1
127.0.0.1:6379> set key1 "hello world"
OK
# 打印记录
redis 127.0.0.1:6379> get key1
"hello world"
# 增加一条数字记录
127.0.0.1:6379> set key2 1
OK
# 让数字自增
127.0.0.1:6379> INCR key2
(integer) 2
#增加一个列表记录key3
127.0.0.1:6379> LPUSH key3 a
(integer) 1
# 从左边插入列表
127.0.0.1:6379> LPUSH key3 b
(integer) 2
# 从右边插入列表
127.0.0.1:6379> RPUSH key3 c
(integer) 3
# 打印列表记录,按从左到右的顺序
127.0.0.1:6379> LRANGE key3 0 3
1) "b"
2) "a"
3) "c"
# 删除key1,key2
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> del key2
(integer) 1
设置redis 密码 123456
vi /etc/redis/redis.conf
#取消注释requirepass foobared
requirepass 123456
#保存退出
密码登录redis
redis-cli -h 127.0.0.1 -a 123456 -p 6379
# 切记 -h -a -p 后面的空格必须有
开启redis远程连接
vi /etc/redis/redis.conf
#注释bind 127.0.0.1
#bind 127.0.0.1
重启Redis服务器
service redis restart
# 或者
/etc/init.d/redis-server restart
检查Redis服务器占用端口
root@ubuntu16:~# netstat -nlt|grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
远程连接redis
root@ubuntu:~# redis-cli -h 192.168.159.129 -a 123456 -h 6379
192.168.129.129:6379>
命令号不带密码连接redis
root@ubuntu:~# redis-cli -h 192.168.159.129 -h 6379
192.168.129.129:6379>auth 123456
OK
192.168.129.129:6379>keys *
1) "key2"
1) "key1"
在Redis集群中使用认证密码
如果Redis服务器,使用了集群。除了在master
中配置密码外,也需要在slave
中进行相应配置。在slave
的配置文件中找到如下行,去掉注释并修改与master
相同的密码即可:
# masterauth master-password