在linux下安装memcached:
1.首先确认linux系统已联网 在root用户下使用yum安装
命令:yum -y install memcached
[root@localhost ~]# yum -y install memcached
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
* base: mirrors.nwsuaf.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.nwsuaf.edu.cn
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 2.4 MB 00:24
Setting up Install Process
Package memcached-1.4.4-5.el6.x86_64 already installed and latest version
Nothing to do
2.安装完成后,查看是否安装成功及开启相关端口及服务
命令:
memcached -h //查看memcached是否安装成功 看到版本号 表示已成功
[root@localhost ~]# iptables -A INPUT -ptcp --dport 11211 -j ACCEPT
[root@localhost ~]# memcached -uroot -d -m 1024 -p 11211
[root@localhost ~]# service iptables save
[root@localhost ~]# memcached -h
memcached 1.4.4
-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 <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)
-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 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)
[root@localhost ~]# iptables -A INPUT -ptcp --dport 11211 -j ACCEPT
[root@localhost ~]# memcached -uroot -d -m 1024 -p 11211
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
3.连接mecached进行增删改测试
连接命令:[root@localhost ~]# telnet 127.0.0.1 11211
运行后可能会出现如下问题
[root@localhost ~]# telnet 192.168.0.103 11211
-bash: telnet: command not found
[root@localhost ~]# teinet 127.0.0.1 11211
-bash: teinet: command not found
[root@localhost ~]# telnet 127.0.0.1 11211
-bash: telnet: command not found
解决办法:安装telnet服务
centos、ubuntu安装telnet命令的方法.:
yum list telnet* 列出telnet相关的安装包
yum install telnet-server 安装telnet服务
yum install telnet.* 安装telnet客户端
[root@localhost ~]# yum list telnet*
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.nwsuaf.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.nwsuaf.edu.cn
Available Packages
telnet.x86_64 1:0.17-48.el6 base
telnet-server.x86_64 1:0.17-48.el6
[root@localhost ~]# yum install telnet-server
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.nwsuaf.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.nwsuaf.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package telnet-server.x86_64 1:0.17-48.el6 will be installed
--> Processing Dependency: xinetd for package: 1:telnet-server-0.17-48.el6.x86_64
--> Running transaction check
---> Package xinetd.x86_64 2:2.3.14-40.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================================
Package Arch Version Repository Size
============================================================================================================================
Installing:
telnet-server x86_64 1:0.17-48.el6 base 37 k
Installing for dependencies:
xinetd x86_64 2:2.3.14-40.el6 base 122 k
Transaction Summary
============================================================================================================================
Install 2 Package(s)
Total download size: 159 k
Installed size: 313 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): telnet-server-0.17-48.el6.x86_64.rpm | 37 kB 00:00
(2/2): xinetd-2.3.14-40.el6.x86_64.rpm | 122 kB 00:01
----------------------------------------------------------------------------------------------------------------------------
Total 133 kB/s | 159 kB 00:01
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : 2:xinetd-2.3.14-40.el6.x86_64 1/2
Installing : 1:telnet-server-0.17-48.el6.x86_64 2/2
Verifying : 1:telnet-server-0.17-48.el6.x86_64 1/2
Verifying : 2:xinetd-2.3.14-40.el6.x86_64 2/2
Installed:
telnet-server.x86_64 1:0.17-48.el6
Dependency Installed:
xinetd.x86_64 2:2.3.14-40.el6
Complete!
[root@localhost ~]# yum install telnet.*
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.nwsuaf.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.nwsuaf.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package telnet.x86_64 1:0.17-48.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================================
Package Arch Version Repository Size
============================================================================================================================
Installing:
telnet x86_64 1:0.17-48.el6 base 58 k
Transaction Summary
============================================================================================================================
Install 1 Package(s)
Total download size: 58 k
Installed size: 109 k
Is this ok [y/N]: y
Downloading Packages:
telnet-0.17-48.el6.x86_64.rpm | 58 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : 1:telnet-0.17-48.el6.x86_64 1/1
Verifying : 1:telnet-0.17-48.el6.x86_64 1/1
Installed:
telnet.x86_64 1:0.17-48.el6
Complete!
这三步安装完成后重新使用telnet 127.0.0.1 11211连接memcached成功;也有可能安装完这三步后还是会提示command not found ,我在初次安装过程中就遇到过这问题,后来重新走了一遍这三步,再次连接 成功。
[root@localhost ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
接下来进行增加,命令如下
add key flags exptime bytes [noreply]
value
参数说明如下:
- key:键值 key-value 结构中的 key,用于查找缓存值。
- flags:可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息 。
- exptime:在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)
- bytes:在缓存中存储的字节数
- noreply(可选): 该参数告知服务器不需要返回数据
- value:存储的值(始终位于第二行)(可直接理解为key-value结构中的value)
示例:
[root@localhost ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
add s1 0 30 3
sss
STORED
接下来测试读取,代码如下
get s1
VALUE s1 0 3
sss
END
修改测试,参数和增加一样,示例代码如下
set s1 0 3000 4
dddd
STORED
get s1
VALUE s1 0 4
dddd
END
至此,memcached的安装及测试已完成