0%

redis数据库学习总结

安装redis-server

sudo apt-get install redis-server

redis服务命令

1
2
3
sudo service redis-server start #开启服务器
sudo service redis-server stop #停止服务
sudo service redis-server restart #重启服务

最好是用指定配置文件的方式开启redis-server。

1
2
3
4
root@dana:~# redis-server /etc/redis/redis.conf
root@dana:~# ps -ef |grep redis
redis 4003 1 0 09:27 ? 00:00:00 /usr/bin/redis-server *:6379
root 4011 2276 0 09:28 pts/0 00:00:00 grep --color=auto redis

设置开启守护进程:(这样我们启动服务,redis进程就在后台运行)
修改redis配置文件(/etc/redis/redis.conf),将daemonize no改为daemonize yes
image.png
image.png
设置守护进程之后,用kill -9停止进程。

1
2
3
4
5
6
root@dana:~# ps -ef |grep redis
root 4327 1 0 09:37 ? 00:00:00 redis-server *:6379
root 4421 2276 0 09:43 pts/0 00:00:00 grep --color=auto redis
root@dana:~# kill -9 4327
root@dana:~# ps -ef |grep redis
root 4423 2276 0 09:43 pts/0 00:00:00 grep --color=auto redis

连接redis

  • 本地连接
    1
    2
    redis-cli #本地连接
    127.0.0.1:6379>
  • 远程连接
    1
    2
    redis-cli -h 192.168.0.1 -p 6379 #远程连接
    192.168.0.1:6379>
    这里远程连接遇到问题:Connection confused
    image.png
    解决方法:修改redis配置文件(/etc/redis/redis.conf),注释掉bind 127.0.0.1::1这一句。这句的意思是绑定IP为127.0.0.1,绑了之后就无法远程连接,因此我们把这一句注释掉。
    image.png
    重启redis服务,配置即可生效。
    sudo service redis-server restart

    参考博文

    https://blog.csdn.net/t544846450/article/details/93504156
    https://blog.csdn.net/NRlovestudy/article/details/90234501
-------------本文结束感谢您的阅读-------------