Redis数据库如何实现读写分离
source link: https://www.linuxprobe.com/redis-read-write.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Redis是一种NoSQL的文档数据库,通过key-value的结构存储在内存中,Redis读的速度是110000次/s,写的速度是81000次/s,性能很高,使用范围也很广。
下面用一个实例实现redis的读写分离,步骤如下:
第一步:下载redis
官网下载地址: https://redis.io/download
下载最新的稳定版,解压拷贝到 ~/redis 中
编译代码:
$ make $ test
第二步:配置redis
编辑redis.conf文件
bind 127.0.0.1 port 6379
拷贝redis.conf文件,改名为slave.conf,打开编辑
bind 127.0.0.1 port 6380 slaveof 127.0.0.1 6379
第三步:运行服务
开启【主服务】
$ src/redis-server
开启【从服务】
$ src/redis-server slave.conf
运行【主服务】的客户端
$ src/redis-cli
运行【从服务】的客户端
$ src/redis-cli -h 127.0.0.1 -p 6380
查看主从服务的关系
$ src/redis-cli info replication
第四步:测试服务器
下面实例演示:在主服务器中存储一些数据,然后在从服务器中查询出来
可以看出,从服务器成功的获取到了主服务器的备份数据。
假如我们在从服务器中保存数据,看结果如何?
提示错误:
(error) READONLY You can't write against a read only slave.
说明从服务器只能读数据,而不能写入数据。
数据在【从服务器】里【读】,在【主服务器】里【写】。
这样就实现了redis数据库的读写分离功能。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK