21.1NoSQL介紹
什麼是NoSQL
非關係型數據庫就是NoSQL,關係型數據庫代表MySQL 對於關係型數據庫來說,是需要把數據存儲到庫、表、行、字段裏,查詢的時候根據條件一行一行地去匹配,當量非常大的時候就很耗費時間和資源,尤其是數據是需要從磁盤裏去檢索
NoSQL數據庫存儲原理非常簡單(典型的數據類型爲k-v),不存在繁雜的關係鏈,比如mysql查詢的時候,需要找到對應的庫、表(通常是多個表)以及字段。
NoSQL數據可以存儲在內存裏,查詢速度非常快
NoSQL在性能表現上雖然能優於關係型數據庫,但是它並不能完全替代關係型數據庫
NoSQL因爲沒有複雜的數據結構,擴展非常容易,支持分佈式
常見NoSQL數據庫
k-v形式的:memcached、redis 適合儲存用戶信息,比如會話、配置文件、參數、購物車等等。這些信息一般都和ID(鍵)掛鉤,這種情景下鍵值數據庫是個很好的選擇。
文檔數據庫:mongodb 將數據以文檔的形式儲存。每個文檔都是一系列數據項的集合。每個數據項都有一個名稱與對應的值,值既可以是簡單的數據類型,如字符串、數字和日期等;也可以是複雜的類型,如有序列表和關聯對象。數據存儲的最小單位是文檔,同一個表中存儲的文檔屬性可以是不同的,數據可以使用XML、JSON或者JSONB等多種形式存儲。
列存儲 Hbase
圖 Neo4J、Infinite Graph、OrientDB
NoSQL數據庫的四大分類
鍵值(Key-Value)存儲數據庫
這一類數據庫主要會使用到一個哈希表,這個表中有一個特定的鍵和一個指針指向特定的數據。Key/value模型對於IT系統來說的優勢在於簡單、易部署。但是如果DBA只對部分值進行查詢或更新的時候,Key/value就顯得效率低下了。舉例如:Tokyo Cabinet/Tyrant, Redis, Voldemort, Oracle BDB. 列存儲數據庫。
這部分數據庫通常是用來應對分佈式存儲的海量數據。鍵仍然存在,但是它們的特點是指向了多個列。這些列是由列家族來安排的。如:Cassandra, HBase, Riak. 文檔型數據庫
文檔型數據庫的靈感是來自於Lotus Notes辦公軟件的,而且它同第一種鍵值存儲相類似。該類型的數據模型是版本化的文檔,半結構化的文檔以特定的格式存儲,比如JSON。文檔型數據庫可 以看作是鍵值數據庫的升級版,允許之間嵌套鍵值。而且文檔型數據庫比鍵值數據庫的查詢效率更高。如:CouchDB, MongoDb. 國內也有文檔型數據庫SequoiaDB,已經開源。
圖形(Graph)數據庫
圖形結構的數據庫同其他行列以及剛性結構的SQL數據庫不同,它是使用靈活的圖形模型,並且能夠擴展到多個服務器上。NoSQL數據庫沒有標準的查詢語言(SQL),因此進行數據庫查詢需要制定數據模型。許多NoSQL數據庫都有REST式的數據接口或者查詢API。如:Neo4J, InfoGrid, Infinite Graph.
因此,我們總結NoSQL數據庫在以下的這幾種情況下比較適用:1、數據模型比較簡單;2、需要靈活性更強的IT系統;3、對數據庫性能要求較高;4、不需要高度的數據一致性;5、對於給定key,比較容易映射覆雜值的環境。
21.2 memcached介紹
Memcached是國外社區網站LiveJournal團隊開發,目的是爲了通過緩存數據庫查詢結果,減少數據庫訪問次數,從而提高動態web站點性能。
官方站點http://www.memcached.org/
數據結構簡單(k-v),數據存放在內存裏
多線程
基於c/s架構,協議簡單
基於libevent的事件處理
自主內存存儲處理(slab allowcation)
數據過期方式:Lazy Expiration 和LRU
Slab allocation
Slab Allocation的原理
將分配的內存分割成各種尺寸的塊(chunk), 並把尺寸相同的塊分成組(chunk的集合),每個chunk集合被稱爲slab。
Memcached的內存分配以Page爲單位,Page默認值爲1M,可以在啓動時通過-I參數來指定。
Slab是由多個Page組成的,Page按照指定大小切割成多個chunk。
Growth factor
Memcached在啓動時通過-f選項可以指定 Growth Factor因子。該值控制chunk大小的差異。默認值爲1.25。
通過memcached-tool命令查看指定Memcached實例的不同slab狀態,可以看到各Item所佔大小(chunk大小)差距爲1.25
命令:# memcached-tool 127.0.0.1:11211 display
Memcached的數據過期方式
Lazy Expiration
Memcached 內部不會監視記錄是否過期,而是在get時查看記錄的時間戳,檢查記錄是否過期。這種技術被稱爲lazy(惰性)expiration。因此,Memcached不會在過期監視上耗費CPU時間。
LRU
Memcached會優先使用已超時的記錄的空間,但即使如此,也會發生追加新記錄時空間不足的情況,此時就要使用名爲Least Recently Used(LRU)機制來分配空間。顧名思義,這是刪除“最近最少使用”的記錄的機制。因此,當內存空間不足時(無法從slab class獲取到新的空間時),就從最近未被使用的記錄中搜索,並將其空間分配給新的記錄。從緩存的實用角度來看,該模型十分理想。
21.3 安裝memcached
yum install -y memcached libmemcached libevent
systemctl start memcached
vim /etc/sysconfig/memcached 可以配置參數
比如加上監聽的ip,可以把OPTIONS="" 改爲OPTIONS="127.0.0.1"
其中-m指定memcached分配內存
-c指定最大併發數
-u指定運行memcached服務的用戶
先看下
[root@aming-01 ~]# yum list |grep memcache Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast libmemcached.i686 1.0.16-5.el7 base libmemcached.x86_64 1.0.16-5.el7 base libmemcached-devel.i686 1.0.16-5.el7 base libmemcached-devel.x86_64 1.0.16-5.el7 base memcached.x86_64 1.4.15-10.el7_3.1 base memcached-devel.i686 1.4.15-10.el7_3.1 base memcached-devel.x86_64 1.4.15-10.el7_3.1 base pcp-pmda-memcache.x86_64 3.11.8-7.el7 base php-pecl-memcache.x86_64 3.0.8-4.el7 base python-memcached.noarch 1.48-4.el7 base [root@aming-01 ~]# yum install -y memcached總下載量:85 k安裝大小:176 k Downloading packages: memcached-1.4.15-10.el7_3.1.x86_64.rpm | 85 kB 00:00:00 Running transaction check Running transaction testTransaction test succeeded Running transaction 正在安裝 : memcached-1.4.15-10.el7_3.1.x86_64 1/1 驗證中 : memcached-1.4.15-10.el7_3.1.x86_64 1/1 已安裝: memcached.x86_64 0:1.4.15-10.el7_3.1 完畢![root@aming-01 ~]# rpm -qa libevent libevent-2.0.21-4.el7.x86_64 [root@aming-01 ~]#
啓動memcached
[root@aming-01 ~]# systemctl start memcached [root@aming-01 ~]# ps aux |grep memcache memcach+ 2681 0.0 0.1 325568 1188 ? Ssl 21:06 0:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024 root 2688 0.0 0.0 112680 980 pts/0 R+ 21:07 0:00 grep --color=auto memcache [root@aming-01 ~]#
看戲端口
11211 端口就是memcached的端口
[root@aming-01 ~]# netstat -lntpActive Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 2681/memcached tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 1698/rpc.mountd tcp 0 0 0.0.0.0:50834 0.0.0.0:* LISTEN 1441/rpc.statd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1577/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2182/master tcp 0 0 0.0.0.0:45180 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 2326/zabbix_server tcp6 0 0 :::3306 :::* LISTEN 2373/mysqld tcp6 0 0 :::45418 :::* LISTEN 1441/rpc.statd tcp6 0 0 :::11211 :::* LISTEN 2681/memcached tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::80 :::* LISTEN 978/httpd tcp6 0 0 :::20048 :::* LISTEN 1698/rpc.mountd tcp6 0 0 :::22 :::* LISTEN 1577/sshd tcp6 0 0 ::1:25 :::* LISTEN 2182/master tcp6 0 0 :::33629 :::* LISTEN - tcp6 0 0 :::2049 :::* LISTEN - tcp6 0 0 :::10051 :::* LISTEN 2326/zabbix_server [root@aming-01 ~]#
想要更改啓動的配置選項有倆種辦法
1,可以自己自定義區啓動它,用命令行的形式去啓動,啓動的過程中更改參數
/usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
2 , 還有一種 進入配置文件 /etc/sysconfig/memcached 裏面修改參數
[root@aming-01 ~]# vi /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="" ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "/etc/sysconfig/memcached" 5L, 71C
memcached還支持哪些選項,可以memcached -h
memcached -l 指定監聽的ip,平時不用配置太多東西,基本上只要改下-m 就差不多了
[root@aming-01 ~]# memcached -hmemcached 1.4.15-p <num> TCP port number to listen on (default: 11211) -U <num> UDP port number to listen on (default: 11211, 0 is off) -s <file> UNIX socket path to listen on (disables network support) -a <mask> access mask for UNIX socket, in octal (default: 0700) -l <addr> interface to listen on (default: INADDR_ANY, all addresses) <addr> may be specified as host:port. If you don't specify a port number, the value you specified with -p or -U is used. You may specify multiple addresses separated by comma or by using -l multiple times -d run as a daemon -r maximize core file limit -u <username> assume identity of <username> (only when run as root) -m <num> max memory to use for items in megabytes (default: 64 MB) -M return error on memory exhausted (rather than removing items) -c <num> max simultaneous connections (default: 1024) -k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u <username> user; under sh this is done with 'ulimit -S -l NUM_KB').-v verbose (print errors/warnings while in event loop) -vv very verbose (also print client commands/reponses) -vvv extremely verbose (also print internal state transitions) -h print this help and exit-i print memcached and libevent license -P <file> save PID in <file>, only used with -d option-f <factor> chunk size growth factor (default: 1.25) -n <bytes> minimum space allocated for key+value+flags (default: 48) -L Try to use large memory pages (if available). Increasing the memory page size could reduce the number of TLB misses and improve the performance. In order to get large pages from the OS, memcached will allocate the total item-cache in one large chunk. -D <char> Use <char> as the delimiter between key prefixes and IDs. This is used for per-prefix stats reporting. The default is ":" (colon). If this option is specified, stats collection is turned on automatically; if not, then it may be turned on by sending the "stats detail on" command to the server. -t <num> number of threads to use (default: 4) -R Maximum number of requests per event, limits the number of requests process for a given connection to prevent starvation (default: 20) -C Disable use of CAS -b <num> Set the backlog queue limit (default: 1024) -B Binding protocol - one of ascii, binary, or auto (default) -I Override the size of each slab page. Adjusts max item size (default: 1mb, min: 1k, max: 128m) -S Turn on Sasl authentication -o Comma separated list of extended or experimental options - (EXPERIMENTAL) maxconns_fast: immediately close new connections if over maxconns limit - hashpower: An integer multiplier for how large the hash table should be. Can be grown at runtime if not big enough. Set this based on "STAT hash_power_level" before a restart. [root@aming-01 ~]#
21.4 查看memcached狀態
memcached-tool 127.0.0.1:11211 stats
或者echo stats |nc 127.0.0.1 11211 需要安裝nc工具yum install -y nc
若安裝libmemcached後,可以使用命令
memstat --servers=127.0.0.1:11211 查看memcached服務狀態
memcached 有個自帶的工具
主要關注curr_items 和 get_hits 用get_hits 除以 curr_items 查看命中率有多高
[root@aming-01 ~]# memcached-tool 127.0.0.1:11211 stats #127.0.0.1:11211 Field Value accepting_conns 1 auth_cmds 0 auth_errors 0 bytes 0 bytes_read 7 bytes_written 0 cas_badval 0 cas_hits 0 cas_misses 0 cmd_flush 0 cmd_get 0 cmd_set 0 cmd_touch 0 conn_yields 0 connection_structures 11 curr_connections 10 curr_items 0 decr_hits 0 decr_misses 0 delete_hits 0 delete_misses 0 evicted_unfetched 0 evictions 0 expired_unfetched 0 get_hits 0 get_misses 0 hash_bytes 524288 hash_is_expanding 0 hash_power_level 16 incr_hits 0 incr_misses 0 libevent 2.0.21-stable limit_maxbytes 67108864 listen_disabled_num 0 pid 2681 pointer_size 64 reclaimed 0 reserved_fds 20 rusage_system 0.035281 rusage_user 0.035281 threads 4 time 1514380964 total_connections 11 total_items 0 touch_hits 0 touch_misses 0 uptime 951 version 1.4.15[root@aming-01 ~]#
curr_items memcached裏面的項目 平常關注的是 get_hist/curr_items 得出的結果,計算出命中率數值
命中率的目的,是要確認,memcached緩存了數據,而且還有請求,如果光緩存卻沒有請求,那麼做這個緩存也就沒有意義。
還可以使用nc命令 沒有安裝一下
[root@aming-01 ~]# nc -bash: nc: 未找到命令 [root@aming-01 ~]# yum install nc 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.163.com正在解決依賴關係--> 正在檢查事務---> 軟件包 nmap-ncat.x86_64.2.6.40-7.el7 將被 安裝--> 解決依賴關係完成依賴關係解決========================================================================================== Package 架構 版本 源 大小==========================================================================================正在安裝: nmap-ncat x86_64 2:6.40-7.el7 base 201 k事務概要==========================================================================================安裝 1 軟件包總下載量:201 k安裝大小:414 k Is this ok [y/d/N]: y Downloading packages: nmap-ncat-6.40-7.el7.x86_64.rpm | 201 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安裝 : 2:nmap-ncat-6.40-7.el7.x86_64 1/1 驗證中 : 2:nmap-ncat-6.40-7.el7.x86_64 1/1 已安裝: nmap-ncat.x86_64 2:6.40-7.el7 完畢![root@aming-01 ~]#
看看nc 是那個包由來的,使用nc來查看memcached 的狀態
[root@aming-01 ~]# nc nc ncat [root@aming-01 ~]# rpm -qf `which nc` nmap-ncat-6.40-7.el7.x86_64 [root@aming-01 ~]# [root@aming-01 ~]# echo stats |nc 127.0.0.1 11211STAT pid 2681STAT uptime 1265STAT time 1514381278STAT version 1.4.15STAT libevent 2.0.21-stable STAT pointer_size 64STAT rusage_user 0.049564STAT rusage_system 0.046648STAT curr_connections 10STAT total_connections 12STAT connection_structures 11STAT reserved_fds 20STAT cmd_get 0STAT cmd_set 0STAT cmd_flush 0STAT cmd_touch 0STAT get_hits 0STAT get_misses 0STAT delete_misses 0STAT delete_hits 0STAT incr_misses 0STAT incr_hits 0STAT decr_misses 0STAT decr_hits 0STAT cas_misses 0STAT cas_hits 0STAT cas_badval 0STAT touch_hits 0STAT touch_misses 0STAT auth_cmds 0STAT auth_errors 0STAT bytes_read 13STAT bytes_written 1024STAT limit_maxbytes 67108864STAT accepting_conns 1STAT listen_disabled_num 0STAT threads 4STAT conn_yields 0STAT hash_power_level 16STAT hash_bytes 524288STAT hash_is_expanding 0STAT bytes 0STAT curr_items 0STAT total_items 0STAT expired_unfetched 0STAT evicted_unfetched 0STAT evictions 0STAT reclaimed 0END [root@aming-01 ~]#
如果安裝了 libmemcached ,也可以使用命令 memstat --servers=127.0.0.1:11211 查看memcached服務狀態
[root@aming-01 ~]# rpm -qa libmemcached [root@aming-01 ~]# yum install -y libmemcached 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.163.com正在解決依賴關係--> 正在檢查事務---> 軟件包 libmemcached.x86_64.0.1.0.16-5.el7 將被 安裝--> 解決依賴關係完成依賴關係解決========================================================================================== Package 架構 版本 源 大小==========================================================================================正在安裝: libmemcached x86_64 1.0.16-5.el7 base 237 k事務概要==========================================================================================安裝 1 軟件包總下載量:237 k安裝大小:677 k Downloading packages: libmemcached-1.0.16-5.el7.x86_64.rpm | 237 kB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安裝 : libmemcached-1.0.16-5.el7.x86_64 1/1 驗證中 : libmemcached-1.0.16-5.el7.x86_64 1/1 已安裝: libmemcached.x86_64 0:1.0.16-5.el7 完畢![root@aming-01 ~]#
查看狀態
[root@aming-01 ~]# memstat --servers=127.0.0.1 11211 Server: 127.0.0.1 (11211) pid: 2681 uptime: 1757 time: 1514381770 version: 1.4.15 libevent: 2.0.21-stable pointer_size: 64 rusage_user: 0.068242 rusage_system: 0.058493 curr_connections: 10 total_connections: 13 connection_structures: 11 reserved_fds: 20 cmd_get: 0 cmd_set: 0 cmd_flush: 0 cmd_touch: 0 get_hits: 0 get_misses: 0 delete_misses: 0 delete_hits: 0 incr_misses: 0 incr_hits: 0 decr_misses: 0 decr_hits: 0 cas_misses: 0 cas_hits: 0 cas_badval: 0 touch_hits: 0 touch_misses: 0 auth_cmds: 0 auth_errors: 0 bytes_read: 30 bytes_written: 2069 limit_maxbytes: 67108864 accepting_conns: 1 listen_disabled_num: 0 threads: 4 conn_yields: 0 hash_power_level: 16 hash_bytes: 524288 hash_is_expanding: 0 bytes: 0 curr_items: 0 total_items: 0 expired_unfetched: 0 evicted_unfetched: 0 evictions: 0 reclaimed: 0[root@aming-01 ~]#