今天給童鞋們介紹的是Redis資料庫,和之前介紹的MySQL不同,Redis是KV型資料庫,而MySQL關係型資料庫,區別在於Redis是不支持SQL語句操作的,而MySQL是支持SQL語句的。
KV型資料庫就是一個Key(鍵)對應一個Value(值),資料庫裡存儲著成千上萬組Key-Value數據,它們之間是獨立存在的,沒有任何關係。
關係型資料庫會有一個一個的Database(資料庫),每個Database裡會有多張Table(數據表),這些數據表在設計階段會存在一定的關聯,例如:user表和topic表之間就存在這一對多的關係,一個user(用戶)可以發布多個topic(主題),通過user的關聯鍵可以在topic表裡找出所有這個user發表的topic。這種就稱之為關係型資料庫,關係型資料庫是可以通過複雜的SQL語句進行關聯查詢的。
2. 基本信息2.1 安裝環境CentOS:CentOS Linux release 7.6.1810 (Core)
Linux:Linux version 3.10.0-1062.el7.x86_64
GCC:gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
redis-4.0.10.tar.gz
3. 準備工作3.1 本地化如果是新環境,我們需要設置時區以保證時間顯示正確
timedatectl set-timezone Asia/Shanghai3.2 安裝wget如果環境裡沒有wget,通過yum安裝一下
3.3 安裝gcc如果環境裡沒有編譯工具,通過yum安裝一下
yum -y install gcc gcc-c++ make3.4 建立環境根目錄mkdir -p /tongfu.net/env/3.5 建立安裝包目錄並進入mkdir /packagescd /packages4. 安裝Redis 44.1 準備redis-4.0.10.tar.gz
4.2 下載安裝包4.3 安裝redistar -xzvf redis-4.0.10.tar.gzcd redis-4.0.10makecd srcmake installmkdir -p /tongfu.net/env/redis-4.0.10/mkdir -p /tongfu.net/env/redis-4.0.10/bin/mkdir -p /tongfu.net/env/redis-4.0.10/conf/cp redis-server /tongfu.net/env/redis-4.0.10/bin/cp redis-cli /tongfu.net/env/redis-4.0.10/bin/cd ..cp redis.conf /tongfu.net/env/redis-4.0.10/conf/cd ..4.4 建立目錄mkdir /tongfu.net/env/redis-4.0.10/data/mkdir /tongfu.net/env/redis-4.0.10/logs/4.5 配置文件修改配置文件為 daemon啟動方式
允許通過本地所有IP訪問
設置初始密碼為 tongfu.net
設置 pidfile 和 logfile
設置資料庫目錄和文件名稱
[root@tfdev packages]# vi /tongfu.net/env/redis-4.0.10/conf/redis.conf
daemonize yes
bind 0.0.0.0
requirepass tongfu.net
pidfile /tongfu.net/env/redis-4.0.10/data/redis.pid
logfile /tongfu.net/env/redis-4.0.10/logs/redis.log
dbfilename dump.rdb
dir /tongfu.net/env/redis-4.0.10/data/4.6 啟動腳本編寫啟動腳本
[root@tfdev packages]#!/bin/sh
export PATH
redisroot=/tongfu.net/env/redis-4.0.10
\$redisroot/bin/redis-server \$redisroot/conf/redis.confEOF4.7 停止腳本編寫停止腳本
[root@tfdev packages]#!/bin/sh
export PATH
redisroot=/tongfu.net/env/redis-4.0.10pidfile=\$redisroot/data/redis.pid
pid="\`cat \${pidfile}\`"if [ "" = "\`ps -ax|awk '{print \$1}'|grep -e "^\${pid}\$"\`" ] ; then /bin/rm -f \${pidfile}else /bin/kill \$pidfiEOF授權腳本
chmod 0755 /tongfu.net/env/redis-4.0.10/bin/redis-startchmod 0755 /tongfu.net/env/redis-4.0.10/bin/redis-quit4.8 自動啟動添加自動啟動腳本
[root@tfdev packages][Unit]Description=redisAfter=network.target
[Service]Type=forkingPIDFile=/tongfu.net/env/redis-4.0.10/data/redis.pidExecStart=/tongfu.net/env/redis-4.0.10/bin/redis-startExecStop=/tongfu.net/env/redis-4.0.10/bin/redis-quitPrivateTmp=true
[Install]WantedBy=multi-user.targetEOF使用 systemctl 管理 redis 服務
systemctl enable redis
systemctl start redis
systemctl stop redis
systemctl restart redis 4.9 服務命令添加到系統目錄ln -s /tongfu.net/env/redis-4.0.10/bin/redis-cli /usr/bin/5. 使用5.1 連接資料庫伺服器使用redis-cli連接資料庫伺服器
[root@tfdev ~]# redis-cli 127.0.0.1:6379>5.2 驗證資料庫伺服器密碼使用auth命令驗證資料庫伺服器密碼
127.0.0.1:6379> auth tongfu.netOK5.3 普通鍵
5.3.1 創建鍵使用set命令創建一個普通鍵
127.0.0.1:6379> set tongfunet fugeOK5.3.2 查看鍵使用get命令查看鍵的值
127.0.0.1:6379> get tongfunet"fuge"5.3.3 刪除鍵使用del命令刪除一個鍵,下面的(integer)1表示有一個鍵被刪除了,如果沒有刪除成功就是(integer)0
127.0.0.1:6379> del tongfunet(integer) 15.4 有效期5.4.1 創建帶有效期的普通鍵使用set命令創建一個普通鍵,通過expire給它設置一個有效期,在有效期之內鍵可以訪問,過期後鍵就會被自動刪除了
127.0.0.1:6379> set tongfunet fugeOK127.0.0.1:6379> expire tongfunet 6(integer) 1127.0.0.1:6379> get tongfunet"fuge"127.0.0.1:6379> get tongfunet(nil)127.0.0.1:6379>6. 總結Redis資料庫是一種基於純內存為存儲介質的資料庫,因此Redis的存取效率的極高的,針對那些訪問量巨大的情況,Redis是非常好的解決方案。但是作為NoSQL資料庫,它無法應對那些眾多模塊之間的複雜關係,這個方面MySQL是強項。因此,使用MySQL作為主要資料庫引擎,使用Redis作為輔助資料庫引擎,這樣的搭配成為了一種主流。
當然,如果我們的日訪問量在10000以內的話,不使用Redis也是完全可以應付的了了。
免費看文章,自己學技術
每一篇文章都是福哥一個字一個字地敲出來的,都是福哥原創的。
每一篇文章都是經過了福哥的反覆驗證的,都是可以正常使用的。