您当前的位置: 首页 >  centos

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Redis入门介绍——centos7安装Redis及连接成功示例

小志的博客 发布时间:2019-04-26 16:12:36 ,浏览量:0

一、安装gcc

gcc编译c的,因为redis是c编写的,所以我们先安装下gcc

yum install gcc-c++

否则安装使用make编译的时候会报下图错误: 在这里插入图片描述

二、本人目录解释:

(tool是在根目录下自己创建的文件夹,作用:存放软件安装包) (devsoftware是在根目录下自己创建的文件夹,作用:存放安装后的软件)

在这里插入图片描述在这里插入图片描述

三、wget方式下载redis压缩包到指定目录,并解压,以及编译

wget -P 指定目录 下载地址

[root@localhost /]# wget -P /tool http://download.redis.io/releases/redis-3.2.9.tar.gz

解压

[root@localhost /]# tar -zxvf redis-3.2.9.tar.gz

这样/tool/目录下就有解压后的redis-3.2.9了

编译:

[root@localhost /]# cd /tool/redis-3.2.9/     #进入redis-3.2.9目录
[root@localhost redis-3.2.9]# make      #编译
四、安装redis
[root@localhost /]# cd /tool/redis-3.2.9/      #进入redis-3.2.9目录
[root@localhost redis-3.2.9]# make PREFIX=/devsoft/redis install    #安装到/devsoft/redis/目录下

已经有redis

[rootot@localhost /]# cd /devsoft/redis/  
[root@localhost redis]# ll
total 0
drwxr-xr-x. 2 root root   134 Apr 26 11:17 bin

redis里有个bin,bin里是一些工具

[root@localhost redis]# cd bin
[root@localhost bin]# ll
total 15060
-rwxr-xr-x. 1 root root 2432936 Apr 26 11:17 redis-benchmark
-rwxr-xr-x. 1 root root   25080 Apr 26 11:17 redis-check-aof
-rwxr-xr-x. 1 root root 5181832 Apr 26 11:17 redis-check-rdb
-rwxr-xr-x. 1 root root 2585880 Apr 26 11:17 redis-cli
lrwxrwxrwx. 1 root root      12 Apr 26 11:17 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5181832 Apr 26 11:17 redis-server

cd回到/tool/redis-3.2.9/目录下的,我们需要把一个redis.conf配置文件 复制到/devsoft/redis/下 后台启动用到

[root@localhost redis-3.2.9]# cp redis.conf  /devsoft/redis/

[root@localhost redis-3.2.9]# ll /devsoft/redis/
总用量 48
drwxr-xr-x. 2 root root   134 7月   2 16:44 bin
-rw-r--r--. 1 root root 46695 7月   2 16:49 redis.conf
五、启动和关闭redis服务

启动redis就是执行 /devsoft/redis/里的bin里的redis-server命令

进入redis目录 执行

[root@localhost /]# cd /devsoft/redis/
[root@localhost redis]# bin/redis-server

在这里插入图片描述在这里插入图片描述 出现这种图标,说明启动成功;

但是 ,这种启动是前端或者前台启动,假如退出 程序就终止或者退出了。

所以这种服务程序,必须后端运行;

我们通过修改配置文件redis.conf

操作,

我们ctrl+c 退出当前程序;

vi打开redis.conf

vi /devsoft/redis/redis.conf 

找到

在这里插入图片描述把no改成yes

esc退出 wq!保存;

然后进入redis目录,然后加载配置文件运行;

[root@localhost ~]# cd  /devsoft/redis/
[root@localhost redis]# ./bin/redis-server ./redis.conf 
[root@localhost redis]# 

通过ps -ef | grep -i redis命令来搜索redis服务

[root@localhost redis]# ps -ef | grep -i redis
root      22941      1  0 11:55 ?        00:00:00 ./bin/redis-server 127.0.0.1:6379
root      22959  22819  0 11:57 pts/0    00:00:00 grep --color=auto -i redis
[root@localhost redis]# 

通过shutdown命令来停止redis服务的运行

[root@localhost redis]# ./bin/redis-cli shutdown
[root@localhost redis]# ps -ef | grep -i redis
root      22993  22819  0 11:58 pts/0    00:00:00 grep --color=auto -i redis
[root@localhost redis]# 
六、redis连接操作

进入/devsoft/redis目录,启动并且查看进程,启动成功后,./bin/redis-cli进入了连接

[root@localhost /]# cd /devsoft/redis/
[root@localhost redis]# ./bin/redis-server ./redis.conf 
[root@localhost redis]# ps -ef | grep -i redis
root      23065      1  1 12:00 ?        00:00:00 ./bin/redis-server 127.0.0.1:6379
root      23075  22819  0 12:00 pts/0    00:00:00 grep --color=auto -i redis
[root@localhost redis]# 

进入客户端

[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> 
127.0.0.1:6379> set key1 value1       #通过set设置,存储 key:value格式的数据
OK
127.0.0.1:6379> get key1            #通过get获取值
"value1"
127.0.0.1:6379> 

以上说明redis连接成功。

以下为简单的语法操作:

127.0.0.1:6379> keys *          #keys  * 显示所有keys
1) "key1"
127.0.0.1:6379> 

127.0.0.1:6379> del key1      #通过del删除key
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 

七、退出redis连接及停止redis服务

1、退出方式一: 先shutdown,在exit

[root@localhost redis]# ./bin/redis-server ./redis.conf 
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> shutdown
not connected> exit
[root@localhost redis]# ps -ef |grep redis
root      20091  18948  0 22:59 pts/0    00:00:00 grep --color=auto redis
[root@localhost redis]# 

1、退出方式二:

按Ctrl+c 退出redis命令行

退出redis命令行后,输入./bin/redis-cli shutdown 停止redis服务 代码如下:

[root@localhost redis]# ./bin/redis-server ./redis.conf 
[root@localhost redis]# ./bin/redis-cli 
127.0.0.1:6379> ping
PONG
[root@localhost redis]# ./bin/redis-cli shutdown
[root@localhost redis]# ps -ef |grep redis
root      20040  18948  0 22:56 pts/0    00:00:00 grep --color=auto redis
关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0433s