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
-
21.1 nosql介紹 21.2 memrcached介紹 21.3 安裝memcached 21 -
21.1 nosql介紹 21.2 memrcached介紹 21.3 安裝memcached 21 - 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 ~]#
[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 ~]#