NoSQL介绍
memcached介绍
安装memcached
1.首先查看memcached包信息
[root@centos-02 ~]# yum list|grep memcache
collectd-memcachec.x86_64 5.8.0-1.el7 epel
dmlite-plugins-memcache.x86_64 0.8.8-1.el7 epel
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
opensips-memcached.x86_64 1.10.5-3.el7 epel
pcp-pmda-memcache.x86_64 3.11.8-7.el7 base
php-ZendFramework-Cache-Backend-Libmemcached.noarch
php-pecl-memcache.x86_64 3.0.8-4.el7 base
php-pecl-memcached.x86_64 2.2.0-1.el7 epel
python-memcached.noarch 1.48-4.el7 base
rubygem-memcache-client.noarch 1.8.5-13.el7 epel
uwsgi-router-memcached.x86_64 2.0.15-1.el7 epel
[root@centos-02 ~]#
2.直接yum安装memcached,libevent包作为依赖也被安装了
[root@centos-02 ~]# yum install -y memcached 已加载插件:fastestmirror base | 3.6 kB 00:00:00 epel/x86_64/metalink | 6.0 kB 00:00:00 epel | 4.7 kB 00:00:00 extras | 3.4 kB 00:00:00 mysql-connectors-community | 2.5 kB 00:00:00 mysql-tools-community | 2.5 kB 00:00:00 mysql56-community | 2.5 kB 00:00:00 updates | 3.4 kB 00:00:00 zabbix | 2.9 kB 00:00:00 zabbix-non-supported | 951 B 00:00:00 (1/3): epel/x86_64/updateinfo | 905 kB 00:00:07 (2/3): updates/7/x86_64/primary_db | 6.9 MB 00:00:15 (3/3): epel/x86_64/primary_db | 6.3 MB 00:00:50 Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn 正在解决依赖关系 --> 正在检查事务 ---> 软件包 memcached.x86_64.0.1.4.15-10.el7_3.1 将被 安装 --> 正在处理依赖关系 libevent-2.0.so.5()(64bit),它被软件包 memcached-1.4.15-10.el7_3.1.x86_64 需要 --> 正在检查事务 ---> 软件包 libevent.x86_64.0.2.0.21-4.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ========================================================================================= Package 架构 版本 源 大小 ========================================================================================= 正在安装: memcached x86_64 1.4.15-10.el7_3.1 base 85 k 为依赖而安装: libevent x86_64 2.0.21-4.el7 base 214 k 事务概要 ========================================================================================= 安装 1 软件包 (+1 依赖软件包) 总下载量:299 k 安装大小:901 k Downloading packages: (1/2): memcached-1.4.15-10.el7_3.1.x86_64.rpm | 85 kB 00:00:00 (2/2): libevent-2.0.21-4.el7.x86_64.rpm | 214 kB 00:00:01 ----------------------------------------------------------------------------------------- 总计 260 kB/s | 299 kB 00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : libevent-2.0.21-4.el7.x86_64 1/2 正在安装 : memcached-1.4.15-10.el7_3.1.x86_64 2/2 验证中 : memcached-1.4.15-10.el7_3.1.x86_64 1/2 验证中 : libevent-2.0.21-4.el7.x86_64 2/2 已安装: memcached.x86_64 0:1.4.15-10.el7_3.1 作为依赖被安装: libevent.x86_64 0:2.0.21-4.el7 完毕! [root@centos-02 ~]#
[root@centos-02 ~]# rpm -qa libevent libevent-2.0.21-4.el7.x86_64 [root@centos-02 ~]#
3.启动memcached,-u指定以memcached用户运行,-p监听端口,-m分配内存大小单位兆,-c最大并发数,这些参数可以在memcached启动的时候更改/usr/bin/memcached,也可以在配置文件中修改/etc/sysconfig/memcached
[root@centos-02 ~]# systemctl start memcached [root@centos-02 ~]# ps aux|grep memcache memcach+ 4271 0.0 0.1 325556 1156 ? Ssl 23:20 0:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024 root 4278 0.0 0.0 112664 928 pts/0 R+ 23:21 0:00 grep --color=auto memcache [root@centos-02 ~]# netstat -lntp Active 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 4271/memcached tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 960/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1586/master tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 1048/zabbix_server tcp6 0 0 :::3306 :::* LISTEN 1362/mysqld tcp6 0 0 :::11211 :::* LISTEN 4271/memcached tcp6 0 0 :::80 :::* LISTEN 809/httpd tcp6 0 0 :::22 :::* LISTEN 960/sshd tcp6 0 0 ::1:25 :::* LISTEN 1586/master tcp6 0 0 :::10051 :::* LISTEN 1048/zabbix_server [root@centos-02 ~]#
[root@centos-02 ~]# cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="" [root@centos-02 ~]#
4.查看memcached支持的选项
[root@centos-02 ~]# memcached -h memcached 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@centos-02 ~]#
查看memcached状态
1.查看memcached状态
[root@centos-02 ~]# 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 33 bytes_written 54 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 (目前memcached中有多少个项目) decr_hits 0 decr_misses 0 delete_hits 0 delete_misses 0 evicted_unfetched 0 evictions 0 expired_unfetched 0 get_hits 0 (命中了多少,命中率等于get_hits/curr_items,也就是保存的有多少被访问过了) 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 4271 pointer_size 64 reclaimed 0 reserved_fds 20 rusage_system 0.332541 rusage_user 0.468638 threads 4 time 1521646884 total_connections 12 total_items 0 touch_hits 0 touch_misses 0 uptime 1246 version 1.4.15 [root@centos-02 ~]#
2.安装nc包
[root@centos-02 ~]# yum install -y nc
3.查看nc包是由哪个包安装来的
[root@centos-02 ~]# rpm -qf `which nc` nmap-ncat-6.40-7.el7.x86_64 [root@centos-02 ~]#
4.用nc命令查看memcached状态
[root@centos-02 ~]# echo stats |nc 127.0.0.1 11211 STAT pid 4271 STAT uptime 1941 STAT time 1521647579 STAT version 1.4.15 STAT libevent 2.0.21-stable STAT pointer_size 64 STAT rusage_user 0.751807 STAT rusage_system 0.422651 STAT curr_connections 10 STAT total_connections 13 STAT connection_structures 11 STAT reserved_fds 20 STAT cmd_get 0 STAT cmd_set 0 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 0 STAT get_misses 0 STAT delete_misses 0 STAT delete_hits 0 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 39 STAT bytes_written 1081 STAT limit_maxbytes 67108864 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT bytes 0 STAT curr_items 0 STAT total_items 0 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT evictions 0 STAT reclaimed 0 END [root@centos-02 ~]#
5.还可以用memstat --servers=127.0.0.1:11211查看memcached状态
[root@centos-02 ~]# rpm -qa libmemcached [root@centos-02 ~]# yum install -y libmemcached
[root@centos-02 ~]# memstat --servers=127.0.0.1:11211 Server: 127.0.0.1 (11211) pid: 4271 uptime: 2206 time: 1521647844 version: 1.4.15 libevent: 2.0.21-stable pointer_size: 64 rusage_user: 0.840607 rusage_system: 0.477239 curr_connections: 10 total_connections: 14 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: 56 bytes_written: 2126 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@centos-02 ~]#