CentOS服务器安装配置redis环境

云服务器:阿里云CentOS7.3 64

Redis的安装有两种方式,一种通过yum安装,一种通过下载Redis源代码编译安装。

一、yum安装Redis

1、下载安装Redis

[root@sihan ~]# yum install redis

2、尝试启动Redis

[root@sihan ~]# service redis start
Redirecting to /bin/systemctl start  redis.service
[root@sihan ~]# service redis status
Redirecting to /bin/systemctl status  redis.service
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: active (running) since Fri 2019-02-01 11:12:08 CST; 5s ago
 Main PID: 21559 (redis-server)
   CGroup: /system.slice/redis.service
           └─21559 /usr/bin/redis-server 127.0.0.1:6379

Feb 01 11:12:08 sihan systemd[1]: Starting Redis persistent key-value database...
Feb 01 11:12:08 sihan systemd[1]: Started Redis persistent key-value database.

可以看到这种方式非常的简单,redis安装完成之后默认是没有设置密码的,并且无法远程连接。我们可以直接本地不用密码进入redis。

[root@sihan ~]# redis-cli
127.0.0.1:6379> auth
(error) ERR wrong number of arguments for 'auth' command
127.0.0.1:6379> keys *
(empty list or set)
Redis注释了需要密码选项

Redis绑定了本机连接

无法远程连接redis

3、修改Redis默认密码,启用远程连接

使用命令vim /etc/redis.conf修改redis配置文件,添加requirepass,注释bind 127.0.0.1。

[root@sihan ~]# vim /etc/redis.conf

注意:Redis默认的端口是6379,一般新服务器是没有对此端口开放的,所以需要自行上云提供商后台配置安全组规则,开放6379端口。

远程连接测试


二、下载Redis源代码编译方式安装

1、查看并下载、解压redis

Redis版本查看:http://download.redis.io/releases/

[root@sihan ~]# wget http://download.redis.io/releases/redis-5.0.3.tar.gz
--2019-02-01 11:38:02--  http://download.redis.io/releases/redis-5.0.3.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1959445 (1.9M) [application/x-gzip]
Saving to: ‘redis-5.0.3.tar.gz’

100%[==============================================================================================>] 1,959,445   1.31MB/s   in 1.4s   

2019-02-01 11:38:04 (1.31 MB/s) - ‘redis-5.0.3.tar.gz’ saved [1959445/1959445]
[root@sihan ~]# tar -zxvf redis-5.0.3.tar.gz -C /usr/local/
...

2、安装gcc依赖

先通过gcc -v是否有安装gcc,如果没有安装,执行命令sudo yum install -y gcc安装gcc。我这边已经默认装了就不在演示安装了

[root@sihan ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 

3、编译安装

进入解压目录编译安装,安装的默认目录是 /usr/local/bin/,需要root权限执行命令。可以使用 PREFIX=/XXX 参数指定安装目录,那有这个目录权限的普通用户也可以执行安装命令

[root@sihan ~]# cd /usr/local/redis-5.0.3/
[root@sihan redis-5.0.3]# make
[root@sihan redis-5.0.3]# cd src/
[root@sihan src]# mkdir /usr/local/redis/
[root@sihan src]# make PREFIX=/usr/local/redis install
[root@sihan src]# mkdir /usr/local/redis/etc/
[root@sihan src]# cp /usr/local/redis-5.0.3/redis.conf /usr/local/redis/etc/

4、配置Redis

[root@sihan bin]# vi /usr/local/redis/etc/redis.conf 

修改一下参数

  • 1、daemonize yes
    redis以守护进程的方式运行
    no表示不以守护进程的方式运行(会占用一个终端)
  • 2、requirepass foobared
    requirepass默认注释掉了,所以无需密码。这边需要设置下密码。
  • 3、bind 127.0.0.1
    绑定ip,默认开启了只能本机连接,注释掉方便远程连接。

5、配置环境变量

[root@sihan bin]# vi /etc/profile
#set redis environment
export PATH="$PATH:/usr/local/redis/bin"
[root@sihan bin]# source /etc/profile

6、配置redis启动脚本

  • redis的启动脚本可从redis源代码中拷贝出来,并将其注册为服务
[root@sihan bin]# cp /usr/local/redis-5.0.3/utils/redis_init_script /etc/init.d/redis
[root@sihan init.d]# cd /etc/init.d/
[root@sihan init.d]# chkconfig redis on
[root@sihan init.d]# service redis start
Starting Redis server...
/etc/init.d/redis: line 28: /usr/local/bin/redis-server: No such file or directory
  • 可以发现默认的启动脚本路径与我们安装路径并不一致,这时我们需要修改一下配置
[root@sihan init.d]# ls /usr/local/redis/bin/
dump.rdb  redis  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server
[root@sihan init.d]# ls /usr/local/redis/etc/
redis.conf
[root@sihan init.d]# vim redis
  • 将以下路径修改好保存
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/redis.conf"
  • 启动成功。
[root@sihan init.d]# service redis start
Starting Redis server...
26537:C 01 Feb 2019 13:53:32.383 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26537:C 01 Feb 2019 13:53:32.383 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=26537, just started
26537:C 01 Feb 2019 13:53:32.383 # Configuration loaded
[root@sihan init.d]# redis-cli
127.0.0.1:6379> auth foobared
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 
  • 但是停止的时候又发现报错了,这是因为设置了密码所以没有权限操作。
[root@sihan init.d]# service redis stop
Stopping ...
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...

此时我们只好kill掉redis进程给配置文件配置上密码了

[root@sihan init.d]# ps -ef|grep redis
root     26538     1  0 13:53 ?        00:00:00 /usr/local/redis/bin/redis-server *:6379
root     26558 25862  0 13:56 pts/2    00:00:00 grep --color=auto redis
[root@sihan init.d]# kill -9 26538
[root@sihan init.d]# vim redis 

添加AUTH变量,并修改停止语句。

AUTH="foobared"

$CLIEXEC -p $REDISPORT -a $AUTH shutdown

重新启动报错/var/run/redis_6379.pid exists, process is already running or crashed,直接删除掉它即可

[root@sihan init.d]# service redis start
/var/run/redis_6379.pid exists, process is already running or crashed
[root@sihan init.d]# rm -rf /var/run/redis_6379.pid 
[root@sihan init.d]# service redis start
Starting Redis server...
26583:C 01 Feb 2019 13:58:52.977 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26583:C 01 Feb 2019 13:58:52.977 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=26583, just started
26583:C 01 Feb 2019 13:58:52.977 # Configuration loaded
[root@sihan init.d]# service redis stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped

可以看到自己打包编译还是比较麻烦的。比较适合linux运维老手去操作。

三、yum安装的redis的卸载

如果是通过第二种方式安装的,直接删除以下文件夹移除redis环境变量即可。

[root@sihan init.d]# cd /usr/local/
[root@sihan local]# ls
aegis  bin  etc  games  include  lib  lib64  libexec  redis  redis-5.0.3  sbin  share  src  tomcat-base  tomcat-production
[root@sihan redis-5.0.3]# rm -rf /usr/local/redis
[root@sihan redis-5.0.3]# rm -rf /usr/local/redis-5.0.3
[root@sihan redis-5.0.3]# rm -rf /etc/init.d/redis 
[root@sihan init.d]# vi /etc/profile
[root@sihan init.d]# source /etc/profile

下面展示的是yum安装的卸载

1、查看安装的redis

[root@sihan ~]# yum list installed | grep redis
redis.x86_64                         3.2.12-2.el7                    @epel     

2、卸载redis

[root@sihan ~]# yum remove redis
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package redis.x86_64 0:3.2.12-2.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================
 Package                       Arch                           Version                               Repository                     Size
========================================================================================================================================
Removing:
 redis                         x86_64                         3.2.12-2.el7                          @epel                         1.4 M

Transaction Summary
========================================================================================================================================
Remove  1 Package

Installed size: 1.4 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : redis-3.2.12-2.el7.x86_64                                                                                            1/1 
warning: /etc/redis.conf saved as /etc/redis.conf.rpmsave
  Verifying  : redis-3.2.12-2.el7.x86_64                                                                                            1/1 

Removed:
  redis.x86_64 0:3.2.12-2.el7                                                                                                           

Complete!

3、验证

[root@sihan ~]# redis-cli
-bash: /usr/bin/redis-cli: No such file or directory

你可能感兴趣的:(CentOS服务器安装配置redis环境)