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服务的用户
- 先看下
[[email protected] ~]# 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 [[email protected] ~]# 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 完毕![[email protected] ~]# rpm -qa libevent libevent-2.0.21-4.el7.x86_64 [[email protected] ~]#
- 启动memcached
[[email protected] ~]# systemctl start memcached [[email protected] ~]# 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 [[email protected] ~]#
- 看戏端口
- 11211 端口就是memcached的端口
[[email protected] ~]# 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 [[email protected] ~]#
- 想要更改启动的配置选项有俩种办法
- 1,可以自己自定义区启动它,用命令行的形式去启动,启动的过程中更改参数
- /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
- 2 , 还有一种 进入配置文件 /etc/sysconfig/memcached 里面修改参数
[[email protected] ~]# 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 就差不多了
[[email protected] ~]# 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. [[email protected] ~]#
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 查看命中率有多高
[[email protected] ~]# 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[[email protected] ~]#
- curr_items memcached里面的项目 平常关注的是 get_hist/curr_items 得出的结果,计算出命中率数值
- 命中率的目的,是要确认,memcached缓存了数据,而且还有请求,如果光缓存却没有请求,那么做这个缓存也就没有意义。
- 还可以使用nc命令 没有安装一下
[[email protected] ~]# nc -bash: nc: 未找到命令 [[email protected]ing-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 完毕![[email protected] ~]#
- 看看nc 是那个包由来的,使用nc来查看memcached 的状态
[[email protected] ~]# nc nc ncat [[email protected] ~]# rpm -qf `which nc` nmap-ncat-6.40-7.el7.x86_64 [[email protected] ~]# [[email protected] ~]# 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 [[email protected] ~]#
- 如果安装了 libmemcached ,也可以使用命令 memstat --servers=127.0.0.1:11211 查看memcached服务状态
[[email protected] ~]# rpm -qa libmemcached [[email protected] ~]# 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 完毕![[email protected] ~]#
- 查看状态
[[email protected] ~]# 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[[email protected] ~]#
原文地址:http://blog.51cto.com/ch71smas/2055300
时间: 2024-10-07 01:30:03