linux系统下:redis的安装、启动和连接

一、安装Redis


 

1.创建redis组,redis用户

创建redis组

[root@localhost home]# groupadd  redis 

创建redis用户

[root@localhost home]# useradd -d /home/redis -g redis -m redis

[root@localhost /]# passwd redis

Changing password for user redis.

New password:

 

2.下载redis安装包

[redis@localhost ~]$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz

或者用浏览器下载也行

 

3.解压

[redis@localhost ~]$ pwd
/home/redis

[redis@localhost ~]$ tar zxvf redis-3.2.8.tar.gz

 

 

4.编译redis

进入安装目录下,执行make命令,编译成功后会显示下面内容。

编译后在目录下生成一个src目录,里面会有编译好的执行文件

 

[redis@localhost ~]$ cd redis-3.2.8
[redis@localhost redis-3.2.8]$ make
cd src && make all
make[1]: Entering directory `/home/redis/redis-3.2.8/src'

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/home/redis/redis-3.2.8/src'

 

5.安装redis(安装到redis用户下)

进入src目录下,使用vi编辑Makefile文件,在文件中找到“PRIFIX?=/usr/local”,修改为你需要安装的目录,这里使用局对路径。

然后执行make install命令进行安装,安装完毕后会在安装目录下生成一些执行文件。

 

[redis@localhost src]$ make install

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

 

[redis@localhost bin]$ pwd
/home/redis/redis_home/bin
[redis@localhost bin]$ ls
dump.rdb         redis-check-aof  redis-cli       redis-server
redis-benchmark  redis-check-rdb  redis-sentinel

 

6、编辑环境变量

在 .bash_profile文件中,注册下面的环境变量,注册后可以在任何位置直接执行redis-server命令。

在REDIS_HOME目录下创建一个conf目录,把启动Redis的conf文件放在里面。

redis.conf文件在redis-3.2.8的源码目录里面。

export REDIS_HOME=/home/redis/redis_home
export PATH=${REDIS_HOME}/bin:${PATH};
[redis@localhost conf]$ pwd
/home/redis/redis_home/conf
[redis@localhost conf]$ ls
redis.conf

 

二、如何启动Redis


 Redis 启动很简单,直接在任意位置执行redis-server命令即可,但是由于没有运行在后台,关闭终端后Redis的进程会终止。

因此,需要对Redis的启动进行设置,使用vi编辑redis.conf文件,找到daemonize属性,把后面的no修改为yes后保存。

在redis-server的后面指定redis.conf的路径就可以了。

 

[redis@localhost conf]$ redis-server
21209:C 29 Apr 02:32:54.052 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
21209:M 29 Apr 02:32:54.052 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
21209:M 29 Apr 02:32:54.052 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
21209:M 29 Apr 02:32:54.052 # Current maximum open files is 1024. maxclients has been reduced to 992 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 21209
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

21209:M 29 Apr 02:32:54.053 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
21209:M 29 Apr 02:32:54.053 # Server started, Redis version 3.2.8
21209:M 29 Apr 02:32:54.053 # 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.
21209:M 29 Apr 02:32:54.054 * DB loaded from disk: 0.000 seconds
21209:M 29 Apr 02:32:54.054 * The server is now ready to accept connections on port 6379

下面是在$REDIS_HOME/conf目录下,以deamon的方式执行redis-server。

可以看到TCP=127.0.0.1,PORT=6379,这两个参数都可以在redis.conf中进行修改。

 

[redis@localhost conf]$ redis-server redis.conf 
[redis@localhost conf]$ ps -ef | grep redis
root      20887  20647  0 02:20 pts/1    00:00:00 su - redis
redis     20888  20887  0 02:20 pts/1    00:00:00 -bash
redis     21322      1  0 02:42 ?        00:00:00 redis-server 127.0.0.1:6379
redis     21330  20888  0 02:42 pts/1    00:00:00 ps -ef
redis     21331  20888  0 02:42 pts/1    00:00:00 grep redis

 

三、使用客户端连接Redis


 在$REDIS_HOME/bin目录下,有名为redis-cli的客户端程序。

[server@chery bin]$ ls
dump.rdb  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server
[server@chery bin]$ pwd
/home/server/redis_home/bin
[server@chery bin]$ redis-cli
127.0.0.1:6379> set name "jim"
OK
127.0.0.1:6379> get name
"jim"
127.0.0.1:6379> 

 

redis-cli连接另一个redis-server服务的ip端口方法如下:

修改redis.conf  中的 port 6379 → 7001

运行命令 [server@chery bin]$ redis-cli  -h 127.0.0.1 -p 7001

——注意,集群模式下,运行: redis-cli -c -h yourhost -p yourpost

 

 

到此为止,Redis3.2.8的安装就完成了,下面就可以学习如何使用了。

[redis@bogon ~]$ redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值