Redis安装
最近在学redis+python,在centos7上装这两个应用,这篇分享redis安装,python安装会在python分类下分享。
参考博文地址:https://www.cnblogs.com/happywish/p/10944253.html https://www.cnblogs.com/lauhp/p/8487029.html
安装:
1.获取redis资源
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
2.解压
tar xzvf redis-4.0.8.tar.gz
3.安装
4个命令一次执行
cd redis-4.0.8 make cd src make install PREFIX=/usr/local/redis
4.移动配置文件到安装目录下
在redis安装目录下创建用于放置redis配置文件的etc目录
mkdir /usr/local/redis/etc
将配置文件转到etc目录
mv redis.conf /usr/local/redis/etc
5.配置redis为后台启动
vi /usr/local/redis/etc/redis.conf //将daemonize no 改成daemonize yes
6.将redis加入到开机启动
vi /etc/rc.local //在里面添加内容:/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf (意思就是开机调用这段开启redis的命令)
7.开启redis服务
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
8.将redis-cli,redis-server拷贝到bin下,让redis-cli指令可以在任意目录下直接使用
cp /usr/local/redis/bin/redis-server /usr/local/bin/ cp /usr/local/redis/bin/redis-cli /usr/local/bin/
9.启动redis服务
redis-server
[root@localhost ~]# redis-server 8762:C 05 Aug 21:39:30.352 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 8762:C 05 Aug 21:39:30.352 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8762, just started 8762:C 05 Aug 21:39:30.352 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 8762:M 05 Aug 21:39:30.353 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 8762 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 8762:M 05 Aug 21:39:30.360 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
10.使用python连接redis
[root@localhost ~]# python3 Python 3.6.8 (default, Apr 2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import redis >>> conn = redis.Redis(host="127.0.0.1", port=6379, password="") >>> conn.set('name', 'itxds') True >>> conn.get('name') b'itxds' >>>
11.设置redis密码
a.运行命令:
redis-cli
b.查看现有的redis密码(第一次安装,没有密码)
127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "" 127.0.0.1:6379>
c.设置密码
127.0.0.1:6379> config set requirepass '123456' OK 127.0.0.1:6379>
d.测试连接
重启redis服务重新连接,发现不能直接使用命令,系统需要验证密码
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> set name itxds (error) NOAUTH Authentication required. 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379>
12.让外网能够访问redis
a.配置防火墙: 开发6379端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent
b.重启防火墙以使配置即时生效
systemctl restart firewalld
c.查看系统所有开放的端口:
firewall-cmd --zone=public --list-ports
b.此时 虽然防火墙开放了6379端口,但是外网还是无法访问的,因为redis监听的是127.0.0.1:6379,并不监听外网的请求。
(一)把文件夹目录里的redis.conf配置文件里的bind 127.0.0.1前面加#注释掉
(二)命令:redis-cli连接到redis后,通过 config get daemonize和config get protected-mode 是不是都为no,如果不是,就用config set 配置名 属性 改为no。