相關概念
數據庫類型:
-
關係型數據庫RDBMS:按照預先設置的
組織結構
,將數據存儲在物理介質上,數據之間可以做關聯操作 -
非關係型數據庫NOSQL:
- Not Only SQL
- 不需要預先定義數據存儲結構
- 每條記錄可以有不同的數據類型和字段個數
- 主流軟件:redis/memcached/mongodb
部署Redis
- Remote Dictionary Server(遠程字典服務器)
- 高性能的(Key/Values)分佈式內存數據庫
- 支持數據持久化(定期把數據存儲到硬盤)
- 支持多種數據類型string/list/hash
- 支持master-slave模式數據備份
- 中文官網www.redis.cn
1)安裝軟件
[root@redis ~] yum -y install gcc
[root@redis ~] tar -xvf redis-4.0.8.tar.gz
[root@redis redis-4.0.8] cd redis-4.0.8
[root@redis redis-4.0.8] make
[root@redis redis-4.0.8] make install
2)初始化配置
[root@redis redis-4.0.8] ./utils/install_server.sh #根據提示完成初始化
信息 | 說明 |
---|---|
默認端口 | 6379 |
主配置文件 | /etc/redis/6379.conf |
日誌文件 | /var/log/redis_6379.log |
數據庫目錄 | /var/lib/redis/6379/ |
服務啓動程序 | /usr/local/bin/redis-server |
命令行連接命令 | /usr/local/bin/redis-cli |
3)管理服務
/etc/init.d/redis_6379
:開發者提供的shell腳本
] /etc/init.d/redis_6379 stop #停止服務
] /etc/init.d/redis_6379 start #啓動服務
] /etc/init.d/redis_6379 status #查看服務狀態
] ps -C redis-server #查看進程
] ss -antup |grep 6379 #查看端口
] redis-cli #連接redis
> ping
PONG
> set KEY VALUES #存儲數據(straing類型)
> mset KEY1 VALUES1 KEY2 VALUES2 ... #存儲多個key值
> get KEY #獲取key值(string類型)
> mget KEY1 KEY2 ... #獲取多個key值
> select 數據庫編號0-15 #切換庫
> keys * #顯示所有key名
> keys a??|age #顯示指定key名(?匹配任意一個字符)
> exists KEY #檢測key是否存在(返回值爲0代表不存在,返回值爲1代表存在)
> ttl KEY #查看key生存時間(返回值爲-1代表永不過期,-2代表已過期)
> type KEY #查看key類型
> move KEY 庫編號 #移動key到指定庫
> expire KEY 數字 #設置key有效時間(單位:s)
> del KEY #刪除指定key
> flushall #刪除內存裏的所有key
> flushdb #刪除所在庫的所有key
> save #保存所有key到磁盤(阻塞寫)
> bgsave #不阻塞寫存盤
> shutdown #停止服務
4)配置文件
/etc/redis/6379.conf
分類:
名稱 | 說明 |
---|---|
NETWORK | 網絡 |
GENERAL | 常規 |
SNAPSHOTTING | 快照 |
REPLICATION | 複製 |
SECURITY | 安全 |
CLIENTS | 客戶端 |
MEMORY MANAGEMENT | 內存管理 |
數據單位:
12 # 1k => 1000 bytes
13 # 1kb => 1024 bytes
14 # 1m => 1000000 bytes
15 # 1mb => 1024*1024 bytes
16 # 1g => 1000000000 bytes
17 # 1gb => 1024*1024*1024 bytes
18 #
19 # units are case insensitive so 1GB 1Gb 1gB are all the same.
常用配置:
70 bind 127.0.0.1 #ip地址
93 port 6350 #端口
137 daemonize yes #以守護進程方式運行
172 logfile /var/log/redis_6379.log #日誌文件
187 databases 16 #數據庫數量
264 dir /var/lib/redis/6379 #數據庫目錄
501 requirepass 123456 #登錄密碼
533 # maxclients 10000 #併發連接數
內存管理:
- MEMORY MANAGEMENT 內存清除策略
565 # volatile-lru -> 刪除最近最少使用(針對設置了TTL的key)
566 # allkeys-lru -> 刪除最少使用的key(針對所有key)
567 # volatile-lfu -> 從所有配置了過期時間的key中清除使用頻率最少的
568 # allkeys-lfu -> 從所有key中清除使用頻率最少的key
569 # volatile-random -> 在設置了TTL的key裏隨機移除
570 # allkeys-random -> 隨機移除key
571 # volatile-ttl -> 移除最近過期的key
572 # noeviction -> 不刪除
573 #
574 # LRU means Least Recently Used
575 # LFU means Least Frequently Used
- 優化
560 maxmemory <bytes> #最大內存
591 maxmemory-policy volatile-lru #定義使用的策略
602 maxmemory-samples 5 #選取key模板的個數(針對lru和ttl策略)
- 持久化:
206 # In the example below the behaviour will be to save:
207 # after 900 sec (15 min) if at least 1 key changed
208 # after 300 sec (5 min) if at least 10 keys changed
209 # after 60 sec if at least 10000 keys changed
219 save 900 1
220 save 300 10
221 save 60 10000
設置密碼/ip/端口:
] vim /etc/redis/6379.conf
requirepass 123456
bind 192.168.4.50
port 6350
] /etc/init.d/redis_6379 stop
] /etc/init.d/redis_6379 start
] redis-cli -h 192.168.4.50 -p 6350 -a 123456 #連接
] redis-cli -h 192.168.4.50 -p 6350 -a 123456 stop #停止服務
- 修改了密碼/ip/端口,
/etc/init.d/redis_6379
腳本將不能正常執行stop
,可以修改腳本內容,也可使用redis-cli
實現服務的停止操作
LNMP+Redis
[root@lnmp60] yum -y install gcc pcre-devel zlib-devel #安裝依賴
[root@lnmp60] tar -zxvf nginx-1.12.2.tar.gz #解壓
[root@lnmp60] cd nginx-1.12.2 #進源碼目錄
[root@lnmp60] ./configure #配置
[root@lnmp60] make && make install
[root@lnmp60] yum -y install php php-fpm
[root@lnmp60] vim +65 /usr/local/nginx/conf/nginx.conf
:65,71s/#//
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 include fastcgi.conf;
70 }
[root@lnmp60] /usr/local/nginx/sbin/nginx -t #測試配置文件格式
[root@lnmp60] systemctl start php-fpm
[root@lnmp60] /usr/local/nginx/sbin/nginx
配置支持Redis
[root@redis50] 部署redis,啓動服務
[root@lnmp60] yum -y install php php-devel
[root@lnmp60] tar -xvf php-redis-2.2.4.tar.gz
[root@lnmp60] cd phpredis-2.2.4
[root@lnmp60] phpize #生成配置文件php-config及configure命令
[root@lnmp60] ./configure --with-php-config=/usr/bin/php-config
[root@lnmp60] make && make install
[root@lnmp60] vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/" #模塊目錄
729 ; On windows:
730 extension = "redis.so" #模塊名
[root@lnmp60] systemctl restart php-fpm
[root@lnmp60] vim /usr/local/nginx/html/test.php #測試腳本
<?php
$redis = new redis();
$redis->connect( '192.168.4.50','6350' );
$redis->auth( '123456' ); #ip/端口/密碼與redis的配置要一致
$redis->set( 'address','xian' );
echo 'OK';
echo $redis->get( 'address' );
?>
[root@lnmp60] curl localhost/test.php