Install redis and build the environment

Here is an example of redis-4.0.9
 
For the convenience of managing my own software packages, I created a software directory in the /usr/local/ directory.
 
mkdir  /usr/local/software
cd  /usr/local/software
 
Download redis:
 
 
Unzip:
 
tar -zxvf redis-4.0.9.tar.gz
 
Compile, install:
 
cd redis-4.0.9
 
#Create a redis installation directory
mkdir /usr/local/redis
 
#Install
make install PREFIX=/usr/local/redis 
 
 
Note that if the compilation and installation fails, it may be that gcc is not installed
yum install gcc #Install online, and then reinstall redis. If the installation fails after that, you can solve it by yourself according to the error.
 
cd /usr/local/redis/bin
 
Enter /usr/local/redis/bin to see
 

 

Among them, redis-server is the server, which is used to start the redis service; redis-cli is the client, which connects to the server and performs corresponding operations.
 
Start redis:
 
./redis-server
 

 

 
You can see the default port number port: 6379 of redis.
Here, because the default configuration has not been modified, after the service is started, other commands cannot be executed on the same interface. Here, the first method of opening a new interface is used, and then the second method of modifying the default configuration is used to connect to the server.
 
1. You can open another window to connect to the server, as shown in the figure:
 

 

Connection service:
 
 

 

cd /usr/local/redis/bin
./redis-cli
 
 
After connecting, you can execute the corresponding redis command:
 

 

 
 
2. Enable background redis service
First copy the configuration file redis.conf to the installation directory
 
cp /usr/local/software/redis-4.0.9/redis.conf     /usr/local/redis/bin/
 
Modify redis.conf
cd /usr/local/redis/bin/
 
vim   redis.conf            #修改daemonize yes
 
再次启动redis服务,后面必须跟自己修改后的配置文件:
./redis-server         ./redis.conf
 
连接是一样的操作
./redis-cli
 
 
 
 
配置redis自启动:
 
cp /usr/local/software/redis-4.0.9/utils/redis_init_script     /etc/rc.d/init.d/redis
#redis_init_script看名字就知道是redis的初始化脚本,把其拷贝到系统初始化目录下。
 
添加注册服务:
chkconfig --add redis
#这时会出现:redis服务不支持chkconfig
 
更改redis脚本 
vim /etc/rc.d/init.d/redis
 

 

 
和原配置文件相比(以下几处都要更改,特别是第一个#chkconfig,否则会导致无法添加配置): 
1.需要添加第二行内容如以上脚本中第二行: 
  #chkconfig: 2345 80 90  
 
2.原文件EXEC、CLIEXEC参数,也是有所更改。
  EXEC=/usr/local/redis/bin/redis-server
  CLIEXEC=/usr/local/redis/bin/redis-cli
 
3.redis开启的命令,以后台运行的方式执行。
  $EXEC $CONF &
 
ps:注意后面的那个“&”,即是将服务转到后面运行的意思,否则启动服务时,Redis服务将 占据在前台,占用了主用户界面,造成其它的命令执行不了。 
 
4.将redis配置文件拷贝到/etc/redis/${REDISPORT}.conf 
 
mkdir /etc/redis
cp /usr/local/redis/bin/redis.conf /etc/redis/6379.conf
 
 
这样,redis服务脚本指定的CONF就存在了。默认情况下,Redis未启用认证,可以通过开启6379.conf的requirepass 指定一个验证密码。 
以上操作完成后,即可注册yedis服务:
 
chkconfig --add redis
 
------------将Redis的命令所在目录添加到环境变量PATH中----------------------
 
修改profile文件: 
vi /etc/profile
 
在最后行追加:  
export PATH="$PATH:/usr/local/redis/bin"
 
  
  应用这个文件
. /etc/profile //或者source /etc/profile
 
打开配置文件:# vi /etc/redis/6379.conf
 
vi /etc/redis/6379.conf
 
进入命令模式查找"requirepass"字符串,找到这一段,去掉前面的#号,后面密码就自定义了,我这里使用dingxu
 
要使配置生效可以重启一下,重启后redis服务确认是自启动的,可以执行如下命令查看
 
ps -ef | grep redis
 
 

 

因为配置了环境变量,这样就可以直接调用redis-cli的命令连接了,不用到指定目录/usr/local/redis/bin目录下运行
 

 

因为设置了密码,所以需要验证:
 

 

 
验证成功后,环境就算搭建好了,就开始系统的学习redis吧,建议到redis中文网去学习常见命令http://www.redis.cn/commands.html,你也可以到官网去,如果你能看懂英文的话。
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324901942&siteId=291194637