redis安装、redis扩展安装、redis简单使用

来源:互联网 发布:锦城骄阳网络剧预告 编辑:程序博客网 时间:2024/05/17 05:14
一、Redis在Linux Ubuntu中安装
1.apt-get install redis-server
2.# 检查Redis服务器系统进程
ps -aux|grep redis
redis     8399  0.2  0.3  31340  1560 ?        Ssl  17:49   0:00 /usr/bin/redis-server 127.0.0.1:6379       
root      8412  0.0  0.1   6120   828 pts/0    S+   17:49   0:00 grep --color=auto redis
3.# 通过启动命令检查Redis服务器状态
netstat -nlt|grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN
4.# 通过启动命令检查Redis服务器状态
/etc/init.d/redis-server status
redis-server is running


二、通过命令行客户端访问Redis
安装Redis服务器,会自动地一起安装Redis命令行客户端程序。

在本机输入redis-cli命令就可以启动,客户端程序访问Redis服务器。

连接远程服务器方式:redis-cli -h 10.110.12.3 -p 22 -a 6kkd1BeeeeeeezSogYIV

redis-cli
127.0.0.1:6379> help
redis-cli 2.8.4
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit


# 查看所有的key列表
redis 127.0.0.1:6379> keys *
(empty list or set)


# 增加一条记录key1
redis 127.0.0.1:6379> set key1 "hello"
OK
# 打印记录
redis 127.0.0.1:6379> get key1
"hello"


# 增加一条数字记录key2
set key2 1
OK


# 让数字自增
redis 127.0.0.1:6379> INCR key2
(integer) 2
redis 127.0.0.1:6379> INCR key2
(integer) 3


# 打印记录
redis 127.0.0.1:6379> get key2
"3"


# 增加一个列表记录key3
redis 127.0.0.1:6379> LPUSH key3 a
(integer) 1


# 从左边插入列表
redis 127.0.0.1:6379> LPUSH key3 b
(integer) 2


# 从右边插入列表
redis 127.0.0.1:6379> RPUSH key3 c
(integer) 3


# 打印列表记录,按从左到右的顺序
redis 127.0.0.1:6379> LRANGE key3 0 3
1) "b"
2) "a"
3) "c"




# 增加一个哈希记表录key4
redis 127.0.0.1:6379> HSET key4 name "John Smith"
(integer) 1


# 在哈希表中插入,email的Key和Value的值
redis 127.0.0.1:6379> HSET key4 email "abc@gmail.com"
(integer) 1


# 打印哈希表中,name为key的值
redis 127.0.0.1:6379> HGET key4 name
"John Smith"


# 打印整个哈希表
redis 127.0.0.1:6379> HGETALL key4
1) "name"
2) "John Smith"
3) "email"
4) "abc@gmail.com"




# 增加一条哈希表记录key5,一次插入多个Key和value的值
redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3
OK


# 打印哈希表中,username和age为key的值
redis 127.0.0.1:6379> HMGET key5 username age
1) "antirez"
2) "3"


# 打印完整的哈希表记录
redis 127.0.0.1:6379> HGETALL key5
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "3"




# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
4) "key5"
5) "key1"


# 删除key1,key5
redis 127.0.0.1:6379> del key1
(integer) 1
redis 127.0.0.1:6379> del key5
(integer) 1


# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"




三、在linux环境安装redis扩展
https://github.com/phpredis/phpredis#installation


1.先安装 apt-get install php5-dev
这样就可以使用phpize命令了
phpize 
./configure --with-php-config=/usr/bin/php-config
make && make install
安装成功后,我们需要修改php.ini配置文件.
添加如下内容:
extension = zend.so
我们可以使用 php -m命令查看下是否加载了zend模块。

PS:有时候可能扩展开启了 在phpinfo()中也看不到 所以靠谱的方法还是看php -m

四、php中redis的简单应用


$redis = new Redis(); 
$redis->connect('192.168.109.159',6379);
$redis->set('name','test');
$name = $redis->get('name');
echo $name;



0 0
原创粉丝点击