linux下安装Redis

/ 技术 / 0 条评论 / 260浏览

进入安装文件夹

cd /www/server/myredis

下载安装包

wget http://download.redis.io/releases/redis-5.0.5.tar.gz

解压安装包

tar -xzf redis-5.0.5.tar.gz

进入解压的redis目录,通过make命令进行编译

make

然后执行make test验证编译是否成功

make test

若不成功 安装tcl

yum install -y tcl

若出现时间大于小于的错误

找到对应文件中的代码,调整参数重新 make test

若出现tcl错误如下:

Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl

重新 make test

编译成功状态提示:

All tests passed without errors!

编译成功后安装

make install cd src && make install

修改配置文件

① 配置允许所有ip都可以访问redis,在bind 127.0.0.1前加“#”将其注释掉
② 默认为保护模式,把 protected-mode yes 改为 protected-mode no
③ 默认为不守护进程模式,把daemonize no 改为daemonize yes
④ 将 requirepass foobared前的“#”去掉,密码改为你想要设置的密码(练习设置为123456,即将foobared改为123456)

指定redis.conf文件启动

redis-server /www/server/myredis/redis-5.0.5/redis.conf

关闭redis进程

查:ps -ef |grep redis  
关:kill xxxx

检查是否开启了所有Ip访问

netstat -lunpt

====设置redis开机自启动====

1.在/etc目录下新建redis目录

[root@CentOS7 redis-5.0.5]# mkdir -pv /etc/redis

2.将配置文件复制进/etc/redis/下

并命名为6379.conf
[root@CentOS7 redis]# cp /opt/redis-5.0.5/redis.conf /etc/redis/6379.conf

3.创建服务

用service来管理服务的时候,是在/etc/init.d/目录中创建一个脚本文件,来管理服务的启动和停止.
在systemctl中,也类似,文件目录有所不同,在/etc/systemd/system目录下创建一个脚本文件redis.service,里面的内容如下:
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/6379.conf
ExecStop=/usr/locl/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown

RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

4.刷新配置,让systemctl识别得到

[root@CentOS7 redis]# systemctl daemon-reload

5.启动关闭redis

[root@CentOS7 system]# systemctl start redis    #启动redis服务 
[root@CentOS7 system]# systemctl stop redis     #关闭redis服务

6.设置redis开机启动

[root@CentOS7 system]# systemctl enable redis