之前的工作中有几个项目的数据库架构中都以免费开源的内存数据库Redis作为辅助数据库(auxiliary database)。Redis的内存缓存和磁盘写出设计,保证了数据库系统赖以生存的性能和安全。
①安装
Linux上建议源码编译安装,一方面Redis非常小(1M+)源码安装很快很方便;另一方面高级软件包管理工具apt或yum可能并未更新到最新版的Redis。这里安装当下最新的4.0.1版本。
cd /usr/local/ wget http://download.redis.io/releases/redis-4.0.1.tar.gz tar xzf redis-4.0.1.tar.gz cd redis-4.0.1 make
②配置
做一些简单常用的配置。我这里主要是做一些启用远程访问、守护进程、修改文件目录和开启AOF记录的设置。
cp src/redis-server /usr/local/bin/redis-server cp src/redis-cli /usr/local/bin/redis-cli mkdir /etc/redis mkdir -p /var/redis/6379 cp redis.conf /etc/redis/6379.conf vim /etc/redis/6379.conf bind 0.0.0.0 unixsocket /tmp/redis.sock daemonize yes pidfile /var/run/redis_6379.pid logfile /var/log/redis_6379.log stop-writes-on-bgsave-error no dir /var/redis/6379 appendonly yes
③开启服务
grep ^# utils/redis_init_script > /etc/init.d/redis_6379 sed -ne '6, 19p' /etc/init.d/skeleton | sed -e '1, $s/skeleton/Redis/g' >> /etc/init.d/redis_6379 grep -v ^# utils/redis_init_script >> /etc/init.d/redis_6379 update-rc.d redis_6379 defaults service redis_6379 start
④连接调试
root@ubuntu:/usr/local/redis-4.0.1# redis-cli 127.0.0.1:6379> set welcome "Hello Redis" OK 127.0.0.1:6379> get welcome "Hello Redis" 127.0.0.1:6379> del welcome (integer) 1 127.0.0.1:6379> shutdown 8103:M 18 Sep 00:15:11.972 # User requested shutdown... 8103:M 18 Sep 00:15:11.972 * Calling fsync() on the AOF file. 8103:M 18 Sep 00:15:11.972 * Saving the final RDB snapshot before exiting. 8103:M 18 Sep 00:15:11.976 * DB saved on disk 8103:M 18 Sep 00:15:11.976 * Removing the pid file. 8103:M 18 Sep 00:15:11.977 # Redis is now ready to exit, bye bye... not connected> not connected> quit [1]+ 完成 ./src/redis-server redis.conf root@ubuntu:/usr/local/redis-4.0.1#
参考:
https://redis.io/topics/quickstart
《Redis in Action》( https://book.douban.com/subject/10597898/ )