安裝repcached memcached
分別在兩台服務器中安裝libevent與repcached memcached
一、安裝libevent
1.先用:ls -al /usr/lib | grep libevent 查看是否已安裝,如果已安裝且版本低於1.3,則先通過:rpm -e libevent —nodeps 進行卸載。
2.下載libevent安裝包:libevent-1.4.13-stable.tar.gz,然後解壓。
3.切換到解壓後的 libevent 主目錄:cd libevent-1.4.13-stable
4.依次執行:
Java代碼
./configure --prefix=/usr (或 ./configure --program-prefix=/usr)
make
make install
注意:
1)執行 make install 時可能需要 root 權限。
2)libevent會安裝到 /usr/lib 下。
5.測試libevent是否安裝成功:ls -al /usr/lib | grep libevent
出現類似如下結果則表示安裝成功:
Java代碼
lrwxrwxrwx. 1 root root 21 Jun 17 00:35 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x. 1 root root 373214 Jun 17 00:35 libevent-1.4.so.2.1.3
-rw-r--r--. 1 root root 603890 Jun 17 00:35 libevent.a
lrwxrwxrwx. 1 root root 26 Jun 17 00:35 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x. 1 root root 137329 Jun 17 00:35 libevent_core-1.4.so.2.1.3
-rw-r--r--. 1 root root 228968 Jun 17 00:35 libevent_core.a
-rwxr-xr-x. 1 root root 860 Jun 17 00:35 libevent_core.la
lrwxrwxrwx. 1 root root 26 Jun 17 00:35 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx. 1 root root 27 Jun 17 00:35 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x. 1 root root 295868 Jun 17 00:35 libevent_extra-1.4.so.2.1.3
-rw-r--r--. 1 root root 469074 Jun 17 00:35 libevent_extra.a
-rwxr-xr-x. 1 root root 867 Jun 17 00:35 libevent_extra.la
lrwxrwxrwx. 1 root root 27 Jun 17 00:35 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x. 1 root root 825 Jun 17 00:35 libevent.la
lrwxrwxrwx. 1 root root 21 Jun 17 00:35 libevent.so -> libevent-1.4.so.2.1.3
6.如果libevent的安裝目錄為/usr/local/lib下,則還需要建立 libevent-1.4.so.2 到 /usr/lib 的軟連接,這樣其他程序運行時才能找到libevent庫:ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2
二、安裝repcached memcached
1.下載repcached安裝包:memcached-1.2.8-repcached-2.2.1.tar.gz,然後解壓。
2.切換到解壓後的 repcached memcached 主目錄:cd memcached-1.2.8-repcached-2.2.1
3.依次執行:
Java代碼
./configure --enable-replication
make
make install
4.如果在64位系統中運行memcached可能會出現如下錯誤:
/usr/local/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
則使用libs參數調試:
LD_DEBUG=libs /usr/local/bin/memcached -v
可以看到以下輸出:
Java代碼
10641: find library=libevent-1.4.so.2 [0]; searching
10641: search cache=/etc/ld.so.cache
10641: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64 (system search path)
10641: trying file=/lib64/tls/x86_64/libevent-1.4.so.2
10641: trying file=/lib64/tls/libevent-1.4.so.2
10641: trying file=/lib64/x86_64/libevent-1.4.so.2
10641: trying file=/lib64/libevent-1.4.so.2
10641: trying file=/usr/lib64/tls/x86_64/libevent-1.4.so.2
10641: trying file=/usr/lib64/tls/libevent-1.4.so.2
10641: trying file=/usr/lib64/x86_64/libevent-1.4.so.2
10641: trying file=/usr/lib64/libevent-1.4.so.2
10641:
/usr/local/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
可知memcached並沒有在/usr/lib中加載libevent-1.4.so.2,而是在/usr/lib64中。
故在/usr/lib64中創建/usr/lib/libevent-1.4.so.2的軟鏈接:
ln -s /usr/lib/libevent-1.4.so.2 /usr/lib64/libevent-1.4.so.2
三、驗證
1.在第一個服務器執行:
Java代碼
[root@localhost ~]# /usr/local/bin/memcached -u nobody -l 192.168.73.201 -p 22122 -m 64 -x 192.168.73.202 -v
replication: connect (peer=192.168.73.202:11212)
replication: marugoto copying
replication: close
replication: listen
2.在第二個服務器執行:
Java代碼
[root@localhost ~]# /usr/local/bin/memcached -u nobody -l 192.168.73.202 -p 22122 -m 64 -x 192.168.73.201 -v
replication: connect (peer=192.168.73.201:11212)
replication: marugoto copying
replication: start
這時在第一個服務器中會輸出:
replication: accept
3.在第一個服務器中設置key value對:
Java代碼
[root@localhost ~]# telnet 192.168.73.201 22122
Trying 192.168.73.201...
Connected to 192.168.73.201.
Escape character is '^]'.
set hey 0 0 4
dude
STORED
get hey
VALUE hey 0 4
dude
END
quit
Connection closed by foreign host
4.在第二個服務器中取出key value對:
Java代碼
[root@localhost ~]# telnet 192.168.73.202 22122
Trying 192.168.73.202...
Connected to 192.168.73.202.
Escape character is '^]'.
get hey
VALUE hello 0 4
dude
END
quit
Connection closed by foreign host.
5.好了,repcached memcached已經安裝配置完成。
另外,安裝telnet客戶端的命令是:
yum -y install telnet
#後台運行:
/usr/local/bin/memcached -d -m 128 -u root -p 11211 -P /tmp/memcached.pid
# 結束memcache進程
kill `cat /tmp/memcached.pid`
參數:
-d 啟動守護進程(後台運行)
-m 分配給memcache使用的內存,單位是MB
-u 運行memcached的用戶
-l 監聽的服務器IP
-p 監聽的服務器端口,默認是11211
-c 最大運行的並發連接數,默認是1024
-P(大寫) 保存Memcache的pid文件,後面跟路徑