Centos 7 安装redis
步骤:
yum install epel-release
yum install redis
启动redis:
systemctl start redis
查看redis状态:
systemctl status redis
systemctl stop redis 停止服务
systemctl restart redis 重启服务
设置开机自启动:
systemctl enable redis
开发端口(没有启动firewalld的话启动一下):
systemctl start firewalld
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=6379/tcp --permanent
重启防火墙:
systemctl restart firewalld
查看端口(netstat没有安装的需要安装一下):
netstat -lnp|grep 6379
允许远程连接:
vim /etc/redis.con
注释 bind 127.0.0.1这一行,否则只有本机可以连接:
添加密码:
保护模式修改为no:
如果修改端口号,搜索port就行了,保存退出
重启redis:
systemctl restart redis
如果没有意外现在就能远程连接了!!!!
Python 连接redis
没有安装redis包的安装一下:
pip install redis
import redis
def main():
config = {
'host': 'xxxxxxxxxxxxxxx', # redis服务器地址
'port': 6379, # redis服务器端口
'db': 0, # 使用的数据库编号
'password': 'xxxxxxxxxxxxxxx' # 密码
}
client = redis.Redis(**config)
if client.ping(): # 判断是否连接成功
client.hmset('user_info',{'age':20, 'position':'xxxx'})
print(client.hgetall('user_info'))
# client.shutdown() # 会导致服务器redis进程退出
if __name__ == '__main__':
main()