安装yum -y install memcached
[root@localhost init.d]# cd /etc/rc.d/init.d
[root@localhost init.d]# ./memcached start
Starting memcached: [ OK ]
[root@localhost init.d]# ps aux
[root@localhost init.d]# telnet localhost 11211
[root@localhost init.d]# telnet localhost 11211
bash: telnet: command not found
[root@localhost init.d]# yum -y install telnet
Trying ::1...
Connected to localhost.
Escape character is '^]'.可用了
set zhang 0 10 3 #set <key> <是否丫说0:不压缩1:压缩> <expire 最多30天> <保存值得字节数>
bar
STORED
get zhang
VALUE zhang 0 3
bar
END
set counter 0 0 1
incr counter 1
set test 0 0 4
test#不然会报CLIENT_ERROR bad data chunk
STORED
append test 0 0 3
add
STORED
get test
VALUE test 0 7
testadd
END
flush_all 清楚所有的key value
OK
quit
Connection closed by foreign host.
[root@localhost init.d]#yum install ruby
[root@localhost init.d]# yum install rubygems
root@localhost init.d]# gem install memcache-client
root@localhost init.d]#ruby
在里面
$KCODE='u'
require "rubygems"
require "memcache"
server=['localhost:11211']
option={}
cache=MemCache.new(server,option)
cache['key1']=123
cache['key2']="ABCD"
cache['key3']=%w(hoge fuga)
cache['key4']={:foo=>1,:bar=>"a"}
p cache['key1']
p cache['key2']
p cache['key3']
p cache['key4']
按ctr-d
123
"ABCD"
["hoge", "fuga"]
{:foo=>1, :bar=>"a"}
[root@localhost init.d]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get key1
VALUE key1 0 5
i{
END
get key2
VALUE key2 0 8
" ABCD
END
[root@localhost init.d]# ruby
require "rubygems"
require "memcache"
cache=MemCache.new(['localhost:11211'])
cache.set('key','value',10)
p cache['key']
sleep 11
"value"
[root@localhost init.d]# memcached -d -p 11212 -u nobody -c 1024 -m 64
[root@localhost init.d]# memcached -d -p 11213 -u nobody -c 1024 -m 64
//分布式集群利用一致hash函数算法
[root@localhost init.d]# ruby
$KCODE='u'
require "rubygems"
require "memcache"
require "logger"
server=['localhost:11211','localhost:11212','localhost:11213']
option={
:logger=>Logger.new(STDOUT)
}
cache=MemCache.new(server,option)
cache['key1']=123
cache['key2']="ABCD"
cache['key3']=%w(hoge fuga)
cache['key4']={:foo=>1,:bar=>"a"}
p cache['key1']
p cache['key2']
p cache['key3']
p cache['key4']
I, [2015-12-27T03:41:14.096026 #26504] INFO -- : memcache-client 1.8.5 ["localhost:11211", "localhost:11212", "localhost:11213"]
D, [2015-12-27T03:41:14.096202 #26504] DEBUG -- : Servers now: [<MemCache::Server: localhost:11211 [1] (NOT CONNECTED)>, <MemCache::Server: localhost:11212 [1] (NOT CONNECTED)>, <MemCache::Server: localhost:11213 [1] (NOT CONNECTED)>]
D, [2015-12-27T03:41:14.113930 #26504] DEBUG -- : set key1 to <MemCache::Server: localhost:11212 [1] (CONNECTED)>: 5
D, [2015-12-27T03:41:14.116295 #26504] DEBUG -- : set key2 to <MemCache::Server: localhost:11211 [1] (CONNECTED)>: 8
D, [2015-12-27T03:41:14.116596 #26504] DEBUG -- : set key3 to <MemCache::Server: localhost:11211 [1] (CONNECTED)>: 16
D, [2015-12-27T03:41:14.119752 #26504] DEBUG -- : set key4 to <MemCache::Server: localhost:11213 [1] (CONNECTED)>: 19
D, [2015-12-27T03:41:14.121405 #26504] DEBUG -- : get key1 from <MemCache::Server: localhost:11212 [1] (CONNECTED)>
123
D, [2015-12-27T03:41:14.121677 #26504] DEBUG -- : get key2 from <MemCache::Server: localhost:11211 [1] (CONNECTED)>
"ABCD"
D, [2015-12-27T03:41:14.121934 #26504] DEBUG -- : get key3 from <MemCache::Server: localhost:11211 [1] (CONNECTED)>
["hoge", "fuga"]
D, [2015-12-27T03:41:14.122167 #26504] DEBUG -- : get key4 from <MemCache::Server: localhost:11213 [1] (CONNECTED)>
模拟失去一个节点
[root@localhost init.d]# ruby
$KCODE='u'
require "rubygems"
require "memcache"
require "logger"
server=['localhost:11212','localhost:11213']
option={
:logger=>Logger.new(STDOUT)
}
cache=MemCache.new(server,option)
cache['key1']=123
cache['key2']="ABCD"
cache['key3']=%w(hoge fuga)
cache['key4']={:foo=>1,:bar=>"a"}
p cache['key1']
p cache['key2']
p cache['key3']
p cache['key4']
I, [2015-12-27T03:55:44.585193 #26589] INFO -- : memcache-client 1.8.5 ["localhost:11212", "localhost:11213"]
D, [2015-12-27T03:55:44.585444 #26589] DEBUG -- : Servers now: [<MemCache::Server: localhost:11212 [1] (NOT CONNECTED)>, <MemCache::Server: localhost:11213 [1] (NOT CONNECTED)>]
D, [2015-12-27T03:55:44.607323 #26589] DEBUG -- : set key1 to <MemCache::Server: localhost:11212 [1] (CONNECTED)>: 5
D, [2015-12-27T03:55:44.607668 #26589] DEBUG -- : set key2 to <MemCache::Server: localhost:11212 [1] (CONNECTED)>: 8
D, [2015-12-27T03:55:44.607869 #26589] DEBUG -- : set key3 to <MemCache::Server: localhost:11212 [1] (CONNECTED)>: 16
D, [2015-12-27T03:55:44.609018 #26589] DEBUG -- : set key4 to <MemCache::Server: localhost:11213 [1] (CONNECTED)>: 19
D, [2015-12-27T03:55:44.609463 #26589] DEBUG -- : get key1 from <MemCache::Server: localhost:11212 [1] (CONNECTED)>
123
D, [2015-12-27T03:55:44.609726 #26589] DEBUG -- : get key2 from <MemCache::Server: localhost:11212 [1] (CONNECTED)>
"ABCD"
D, [2015-12-27T03:55:44.609963 #26589] DEBUG -- : get key3 from <MemCache::Server: localhost:11212 [1] (CONNECTED)>
["hoge", "fuga"]
D, [2015-12-27T03:55:44.610171 #26589] DEBUG -- : get key4 from <MemCache::Server: localhost:11213 [1] (CONNECTED)>
{:foo=>1, :bar=>"a"}
[root@localhost ~]# wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
[root@localhost ~]# yum install libevent libevent-devel -y
[root@localhost ~]# tar zxvf memcached-1.2.8-repcached-2.2.tar.gz
[root@localhost ~]# cd memcached-1.2.8-repcached-2.2
[root@localhost memcached-1.2.8-repcached-2.2]#./configure --enable-replication --program-transform-name=s/memcached/repcached/
[root@localhost memcached-1.2.8-repcached-2.2]# make
[root@localhost memcached-1.2.8-repcached-2.2]# make install
/usr/bin/install -c 'memcached' '/usr/local/bin/memcached'
/usr/bin/install -c 'memcached-debug' '/usr/local/bin/memcached-debug'
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'
make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'
[root@localhost memcached-1.2.8-repcached-2.2]# killall memcached
[root@localhost memcached-1.2.8-repcached-2.2]# killall memcached
[root@localhost memcached-1.2.8-repcached-2.2]# su paohaijiao非root
[paohaijiao@localhost memcached-1.2.8-repcached-2.2]$ /usr/local/bin/repcached -p 11211 -v -d
paohaijiao@localhost memcached-1.2.8-repcached-2.2]$ replication: listen
[paohaijiao@localhost root]$ pstree -G
init─┬─NetworkManager─┬─dhclient
│ └─{NetworkManager}
├─abrtd
├─acpid
├─atd
├─auditd───{auditd}
├─bluetoothd
├─bonobo-activati───{bonobo-activat}
├─clock-applet
├─console-kit-dae───63*[{console-kit-da}]
├─crond
├─cupsd
├─2*[dbus-daemon───{dbus-daemon}]
├─2*[dbus-launch]
├─devkit-power-da
├─gconfd-2
├─gdm-binary─┬─gdm-simple-slav─┬─Xorg
│ │ ├─gdm-session-wor─+
│ │ │ +
│ │ └─{gdm-simple-sla}
│ └─{gdm-binary}
├─gdm-user-switch
├─gnome-keyring-d───2*[{gnome-keyring-}]
├─gnome-screensav
├─gnome-settings-───{gnome-settings}
├─gnome-terminal─┬─bash───su───bash───p+
│ ├─gnome-pty-helpe
│ └─{gnome-terminal}
├─gnote
├─gvfs-afc-volume───{gvfs-afc-volum}
├─gvfs-fuse-daemo───3*[{gvfs-fuse-daem}]
├─gvfs-gdu-volume
├─gvfs-gphoto2-vo
├─gvfsd
├─gvfsd-burn
├─gvfsd-trash
├─hald─┬─hald-runner─┬─hald-addon-acpi
│ │ ├─hald-addon-inpu
│ │ └─hald-addon-rfki
│ └─{hald}
├─master─┬─pickup
│ └─qmgr
├─5*[mingetty]
├─modem-manager
├─notification-ar
├─packagekitd
├─polkitd
├─pulseaudio─┬─gconf-helper
│ └─2*[{pulseaudio}]
├─repcached(启动了这一个)
├─restorecond
├─rsyslogd───3*[{rsyslogd}]
├─rtkit-daemon───2*[{rtkit-daemon}]
├─seahorse-daemon
├─sshd
├─tpvmlp
├─trashapplet
├─udevd───2*[udevd]
├─udisks-daemon───udisks-daemon
├─2*[vmtoolsd]
├─vmware-vmblock-───2*[{vmware-vmblock}]
├─wnck-applet
└─wpa_supplicant
[paohaijiao@localhost root]$ /usr/local/bin/repcached -p 11213 -x 127.0.0.1 -v -d
[paohaijiao@localhost root]$ replication: connect (peer=127.0.0.1:11212)
replication: marugoto copying
replication: start
[root@localhost ~]# pstree
init─┬─NetworkManager─┬─dhclient
│ └─{NetworkManager}
├─abrtd
├─acpid
├─atd
├─auditd───{auditd}
├─bluetoothd
├─bonobo-activati───{bonobo-activat}
├─clock-applet
├─console-kit-dae───63*[{console-kit-da}]
├─crond
├─cupsd
├─2*[dbus-daemon───{dbus-daemon}]
├─2*[dbus-launch]
├─devkit-power-da
├─gconfd-2
├─gdm-binary─┬─gdm-simple-slav─┬─Xorg
│ │ ├─gdm-session-wor─┬─gnome-session─┬─bluetoo+
│ │ │ │ ├─gdu-not+
│ │ │ │ ├─gnome-p+
│ │ │ │ ├─gnome-p+
│ │ │ │ ├─gnome-v+
│ │ │ │ ├─gpk-upd+
│ │ │ │ ├─metacit+
│ │ │ │ ├─nautilus
│ │ │ │ ├─nm-appl+
│ │ │ │ ├─polkit-+
│ │ │ │ ├─python
│ │ │ │ └─{gnome-+
│ │ │ └─{gdm-session-wo}
│ │ └─{gdm-simple-sla}
│ └─{gdm-binary}
├─gdm-user-switch
├─gnome-keyring-d───2*[{gnome-keyring-}]
├─gnome-screensav
├─gnome-settings-───{gnome-settings}
├─gnome-terminal─┬─bash───su───bash───su───bash───su───bash───su───bash───+
│ ├─bash───su───bash
│ ├─bash───pstree
│ ├─gnome-pty-helpe
│ └─{gnome-terminal}
├─gnote
├─gvfs-afc-volume───{gvfs-afc-volum}
├─gvfs-fuse-daemo───3*[{gvfs-fuse-daem}]
├─gvfs-gdu-volume
├─gvfs-gphoto2-vo
├─gvfsd
├─gvfsd-burn
├─gvfsd-trash
├─hald─┬─hald-runner─┬─hald-addon-acpi
│ │ ├─hald-addon-inpu
│ │ └─hald-addon-rfki
│ └─{hald}
├─master─┬─pickup
│ └─qmgr
├─5*[mingetty]
├─modem-manager
├─notification-ar
├─polkitd
├─pulseaudio─┬─gconf-helper
│ └─2*[{pulseaudio}]
├─2*[repcached]11213+11211
├─restorecond
├─rsyslogd───3*[{rsyslogd}]
├─rtkit-daemon───2*[{rtkit-daemon}]
├─seahorse-daemon
├─sshd
├─tpvmlp
├─trashapplet
├─udevd───2*[udevd]
├─udisks-daemon───udisks-daemon
├─2*[vmtoolsd]
├─vmware-vmblock-───2*[{vmware-vmblock}]
├─wnck-applet
└─wpa_supplicant
[root@localhost ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set ab 0 0 3
foo
STORED
get ab
VALUE ab 0 3
foo
[root@localhost ~]# telnet localhost 11213
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get ab
VALUE ab 0 3
foo
set x 0 0 3
xyz
STORED
get x
VALUE x 0 3
xyz
END
quit
Connection closed by foreign host.
[root@localhost ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get x
VALUE x 0 3
xyz
END
这说明他是双向的