【原创】NOSQL系列-memcached安装管理与repcached高可用性

Memcached :基于内存工作键值存储型数据库,可以作为应用->memcached 缓存层->数据库。Danga Interactive公司开发,最初为了加速LiveJournal访问速度而开发的,后来成为广泛应用的开源项目。

一、实验环境

1.系统类型

[leo@h4 etc]$ cat issue

CentOS Linux release 6.0 (Final)

Kernel \r on an \m

2.系统位数

[leo@h4 桌面]$ getconf LONG_

32

Linux ISOCentOS-6.0-i386-bin-DVD.iso 32

JDK version"1.6.0_25-ea"

虚拟机:Product VMware® Workstation Version 7.0.0 build-203739

3.CentOS需要能连入网络,这个比较简单,我们只需要设置菜单->系统->首选项->网络连接

123149oysqd3qqdvjyyo73.jpg

打开网络连接

1231490znegrsi8v0xnvpq.jpg

点击“编辑”,这是设置system eth0 网卡

123150hhrrwxiiqiiqixqi.jpg

选择IPv4设置->方法:自动(DHCP)[让系统动态分配ip即可]->应用,因为我在公司走的是路由器,所以选择自动分配IP比较方便

123150brii6t9mjjdiq38h.jpg

如果不是使用的root用户编辑的,会让你输入root用户密码,输入后点击“授权”即可

[root@h4 ~]# service network restart      重起网络服务

[root@h4 ~]# ifconfig                   这回我们看到ip地址已经被自动分配了

eth0      Link encap:Ethernet  HWaddr 00:0C:29:8B:7D:18  

          inet addr:192.168.2.107  Bcast:192.168.2.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe8b:7d18/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:8847 errors:0 dropped:0 overruns:0 frame.:0

          TX packets:2355 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:6067491 (5.7 MiB)  TX bytes:231934 (226.4 KiB)

          Interrupt:19 Base address:0x2024

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:10 errors:0 dropped:0 overruns:0 frame.:0

          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:704 (704.0 b)  TX bytes:704 (704.0 b)

4.最最最重要的是设置VMware networkBridged”桥接方式,才能连上物理网络哦
VMware菜单-> VM -> Settings –> 选择network Adapter

123151l9epervug05glllz.jpg

5.我们测试效果

看火狐浏览器中已经显示“楚汉传奇”的搜索结果了,我们伟大的上网了

大家这个时候一定要坚持住啊,不要直接看电视剧去啦

顺便推荐一下360搜索引擎,大家是否用过360搜索,效果比baidu 好些哦:)

下面我们转入正题

123152g29kt1dvn9dgtd9c.jpg

二、memcached安装管理

1.安装memcached

我在网上看到很多人默认情况下yum不能安装

yum install memcached

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

RHN support will be disabled.

Setting up Install Process

No package memcached available.

Nothing to do

原因是:操作系统在安装的时候yum包没有安装上,所以就没有。你只能去下载一个yum包,自己手动安装上

解决方案:可以参考吴飚的“yum报错完美解决帖”http://f.dataguru.cn/thread-44212-1-2.html

Linux ISOCentOS-6.0-i386-bin-DVD.iso 32  如果你是安装这个版本的话,并且在选择组件的时候选择了yum组件那么现在可以直接yum install memcached,幸亏当初安装的时候抱着宁可错杀100也不放过1个心态,统统安装上了,现在可以直接使用啦!天灵灵 地灵灵 yumyum快显灵

[root@h4 ~]# yum install memcached

Loaded plugins: fastestmirror, refresh-packagekit

Loading mirror speeds from cached hostfile

* base: mirrors.btte.net

* extras: mirrors.btte.net

* updates: mirrors.btte.net

Setting up Install Process

Resolving Dependencies

--&gt Running transaction check

---&gt Package memcached.i686 0:1.4.4-3.el6 set to be updated

--&gt Finished Dependency Resolution

Dependencies Resolved    自动选择的版本1.4.4-3.el6,现在最高版本1.4.15

================================================================================

Package             Arch           Version                Repository      Size

================================================================================

Installing:

memcached           i686           1.4.4-3.el6            base            67 k

Transaction Summary

================================================================================

Install       1 Package(s)           只安装一个包

Upgrade       0 Package(s)         没有更新包

Total download size: 67 k             下载需要67k

Installed size: 145 k                  安装需要145k

Is this ok [y/N]: y                    是否下载安装=> yes

Downloading Packages:

memcached-1.4.4-3.el6.i686.rpm                           |  67 kB     00:00     

warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY base/gpgkey                                              | 3.3 kB     00:00 ...

Importing GPG key 0xC105B9DE "CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>" from /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6               会验证密钥这步

Is this ok [y/N]: y

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Warning: RPMDB altered outside of yum.

  Installing     : memcached-1.4.4-3.el6.i686                                                                                                                       1/1

Installed:

  memcached.i686 0:1.4.4-3.el6                                                                                                                                          

Complete!                          memcached安装完成

2.启动memcached服务

[root@h4 ~]# cd /etc/rc.d/init.d/        进入开机启动服务目录,找到memcached启动脚本

[root@h4 init.d]# ll | grep memcached

-rwxr-xr-x. 1 root root 1635  5 26 201t root memcached    我们找到啦

[root@h4 init.d]# ./memcached start     我们启动memcached服务,系统是中文版的比较好看

正在启动 memcached              [确定

3.查看memcached进程

进程树查看

[root@h4 init.d]# pstree –G            -G 如果你使用远程连接,需要加-G,否则乱码

[root@h4 init.d]# pstree               本地连接就不用加

……

├─memcached───5*[{memcached}] 已经启动1memcached进程(其中包括5个线程)

……

进程过滤法查看

[root@h4 init.d]# ps aux | grep memcached

495       9979  0.0  0.1  58132   868 ?        Ssl  16:03   0:00 memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid

选项解释:

-d    后台运行memcached

-p    默认端口号11211

-u    启动用户,安装完memcached后会自动建立一个叫“memcached”用户,非登录模式

[root@h4 init.d]# cat /etc/passwd

memcached:x:495:489:Memcached daemon:/var/run/memcached:/sbin/nologin

-m   memcached参数,指用多少内存啊什么的

-c

4.连接memcached并测试

[root@h4 init.d]# telnet localhost 11211

-bash: telnet: command not found           找不到这个命令,怎么回事,原来没有安装

telnet-server-1.2-134.22.i586.rpm包,默认是没有telnet server端包的需要手动安装

[root@h4 init.d]# rpm -qa | grep telnet 检查一下如果没有安装请先安装telnet server= telnet-server-1.2-134.22.i586.rpm (只安装server端就可以了,不用安装client

我们可以从网上下载也可以,在ISO光盘里面找到

[root@h4 ~]# rpm -ivh telnet-server-1.2-134.22.i586.rpm

warning: telnet-server-1.2-134.22.i586.rpm: Header V3 RSA/SHA256 Signature, key ID 307e3d54: NOKEY

Preparing...                ########################################### [100%]

   1:telnet-server          ########################################### [100%]

[root@h4 ~]# rpm -qa | grep telnet             再次检查已经有了

telnet-server-1.2-134.22.i586

[root@h4 ~]# chkconfig telnet on               设置开机启动telnet服务

[root@h4 sbin]# /etc/init.d/xinetd restart        好么连xinetd都没有安装,重新安装一下

-bash: /etc/init.d/xinetd:没有那个文件和目录

[root@h4 ~]# rpm -ivh xinetd-2.3.14-29.el6.i686.rpm

Preparing...                ########################################### [100%]

   1:xinetd                 ########################################### [100%]

[root@h4 init.d]# rpm -qa | grep xinetd          检查xinetd包已经安装完成

xinetd-2.3.14-29.el6.i686

[root@h4 init.d]# chkconfig --add xinetd              添加xinetd服务到开机启动

[root@h4 init.d]# chkconfig xinetd on                设置开机启动

[root@h4 init.d]# chkconfig -add telnet               添加telnet服务到开机启动

[root@h4 init.d]# chkconfig telnet on                 设置开机启动

[root@h4 init.d]# chkconfig --list | grep xinetd         

xinetd   0:关闭  1:关闭  2:启动  3:启动  4:启动  5:启动  6:关闭

[root@h4 init.d]# chkconfig --list | grep telnet

telnet:           启动

[root@h4 init.d]# /etc/init.d/xinetd restart            启动xinetd服务

由于telnet服务也是由xinetd守护的,所以安装完telnet-server,要启动telnet服务就必须重新启动xinetd

我有一个重大失误,在此进行检讨,我们安装telnet客户端导致

[root@h4 ~]# telnet 127.0.0.1

-bash: telnet: command not found      导致找不到命令

如果安装

[root@h4 ~]# which telnet

/usr/bin/telnet                      会显示命令路径

注:服务端 只负责接收  客户端负责发起,所以我们2个都要安装

[root@h4 ~]# rpm -ivh telnet-0.17-46.el6.i686.rpm

Preparing...                ########################################### [100%]

   1:telnet                 ########################################### [100%]

[root@h4 ~]# telnet localhost         测试一下现在telnet正常了,没有问题了

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

CentOS Linux release 6.0 (Final)

Kernel 2.6.32-71.el6.i686 on an i686

login: leo

Password

yum安装方式,比较自动,比较方便

[root@h4 xinetd.d]# yum provides */telnet        这个是查看哪些RPM包提供了telnet这个文件

Loaded plugins: fastestmirror, refresh-packagekit

Loading mirror speeds from cached hostfile

* base: mirrors.btte.net

* extras: mirrors.btte.net

* updates: mirrors.btte.net

开始测试memcached

[root@h4 init.d]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

输入key-values

set leonarding 0 0 12              

liusheng

STORED

输出key-values

get leonarding

VALUE leonarding 0 12

liusheng

END

语法解释: set   key  flag  expires  byte

Flag :是否压缩数据  0  不压缩  1压缩

Expires:过期时间,单位秒(超过这个时间数据就失效),最大保留30天,设置为永久的也会在这个时间过期,0代表永久

Byte:指定value的字节数,注意字节匹配精确 hello 字节数是5  leo 字节数是3

Quit:退出memcached命令

Connection closed by foreign host.                 这是退出

[root@h4 init.d]# telnet localhost 11211            这种方式也可以

[root@h4 init.d]# telnet 192.168.2.107 11211        这种方式也可以

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

增加values

set counter 0 0 1          输入key-values

1

STORED

incr counter 1            增加values 1+1=2

2

incr counter 2            2+2=4

4

get counter

VALUE counter 0 1

4                       最后value=4

END

删除values

delete counter            删除values值,就是删除key-values

DELETED

get counter              看现在没有了values

END

追加values

set leo 0 0 3   

leo                     原来values=leo

STORED

append leo 0 0 7

narding                 追加的values=narding

STORED

get leo

VALUE leo 0 10

Leonarding              完整values=leoanrding

END

删除所有key-values   

flush_all

OK

get leo                  看所有键值对都被删除了

END

get counter

END

#########################################################################

repcached高可用性

repcached:全称 replication cached是由日本人发明的memcached的高可用性技术,简称复制缓冲区技术。场景用在当有一台服务器当掉时候,会有另一台服务器来接管它,并且这两台服务器的内容是一模一样的,我们就不需要在去数据库拿数据了。

repcached架构:一个主memcached 配一个备memcached,一一对应作冗余

三、repcached安装部署

1.安装libevent-devel    这个包是编译repcached程序所依赖的包

你也可以从网上下载后->tar zxvf libevent-2.0.17-stable.tar.gz 网上有很多,注意依赖关系和编译

我们使用yum方式来安装

[root@h4 init.d]# yum install libevent-devel

Loaded plugins: fastestmirror, refresh-packagekit

Loading mirror speeds from cached hostfile

* base: mirrors.ta139.com

* extras: mirrors.ta139.com

* updates: mirrors.ta139.com

Setting up Install Process

Resolving Dependencies

--&gt Running transaction check

---&gt Package libevent-devel.i686 0:1.4.13-4.el6 set to be updated

--&gt Processing Dependency: libevent-headers = 1.4.13-4.el6 for package: libevent-devel-1.4.13-4.el6.i686

--&gt Processing Dependency: libevent-doc = 1.4.13-4.el6 for package: libevent-devel-1.4.13-4.el6.i686

--&gt Processing Dependency: libevent = 1.4.13-4.el6 for package: libevent-devel-1.4.13-4.el6.i686

--&gt Running transaction check

---&gt Package libevent.i686 0:1.4.13-4.el6 set to be updated

---&gt Package libevent-doc.noarch 0:1.4.13-4.el6 set to be updated

---&gt Package libevent-headers.noarch 0:1.4.13-4.el6 set to be updated

--&gt Finished Dependency Resolution

Dependencies Resolved            有三个依赖包需要先安装

=======================================================================================================

Package                                       Arch                                Version                                     Repository                         Size

=======================================================================================================

Installing:

libevent-devel                                i686                                1.4.13-4.el6                                base                               71 k

Installing for dependencies:

libevent-doc                                  noarch                              1.4.13-4.el6                                base                              194 k

libevent-headers                              noarch                              1.4.13-4.el6                                base                               30 k

Updating for dependencies:

libevent                                      i686                                1.4.13-4.el6                                base                               67 k


 

Transaction Summary

=======================================================================================================

Install       3 Package(s)

Upgrade       1 Package(s)

Total download size: 362 k

Is this ok [y/N]:y

Downloading Packages:

(1/4): libevent-1.4.13-4.el6.i686.rpm                                                                                                           |  467 B     00:00     

http://mirrors.ta139.com/centos/6.3/os/i386/Packages/libevent-1.4.13-4.el6.i686.rpm: [Errno -1] Package does not match intended download. Suggestion: run yum clean metadata

Trying other mirror.

(1/4): libevent-1.4.13-4.el6.i686.rpm                                                                                                           |  67 kB     00:00     

http://mirrors.btte.net/centos/6.3/os/i386/Packages/libevent-1.4.13-4.el6.i686.rpm: [Errno -1] Package does not match intended download. Suggestion: run yum clean metadata

Trying other mirror.

(1/4): libevent-1.4.13-4.el6.i686.rpm                                                                                                           |  67 kB     00:00     

(2/4): libevent-devel-1.4.13-4.el6.i686.rpm                                                                                                     |  71 kB     00:00     

(3/4): libevent-doc-1.4.13-4.el6.noarch.rpm                                                                                                     | 194 kB     00:00     

(4/4): libevent-headers-1.4.13-4.el6.noarch.rpm                                                                                                 |  30 kB     00:00     

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Total                                                                                                                                  409 kB/s | 362 kB     00:00     

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Warning: RPMDB altered outside of yum.

  Updating       : libevent-1.4.13-4.el6.i686                                                                                                                      1/5

  Installing     : libevent-headers-1.4.13-4.el6.noarch                                                                                                            2/5

  Installing     : libevent-devel-1.4.13-4.el6.i686                                                                                                                3/5

  Installing     : libevent-doc-1.4.13-4.el6.noarch                                                                                                                4/5

  Cleanup        : libevent-1.4.13-1.el6.i686                                                                                                                      5/5

Installed:

  libevent-devel.i686 0:1.4.13-4.el6                                                                                                                                   

Dependency Installed:

  libevent-doc.noarch 0:1.4.13-4.el6                                               libevent-headers.noarch 0:1.4.13-4.el6                                             

Dependency Updated:

  libevent.i686 0:1.4.13-4.el6                                                                                                                                         

Complete!

使用yum安装不用在做编译工作

2.查看我们是否安装成功libevent

[root@h4 init.d]# cd /usr/lib

[root@h4 lib]# ll | grep libevent

lrwxrwxrwx.  1 root root       21  1月  4 20:09 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3

-rwxr-xr-x.  1 root root   108948  6月 22 2012 libevent-1.4.so.2.1.3

-rw-r--r--.  1 root root   133014  6月 22 2012 libevent.a

lrwxrwxrwx.  1 root root       26  1月  4 20:09 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3

-rwxr-xr-x.  1 root root    37080  6月 22 2012 libevent_core-1.4.so.2.1.3

-rw-r--r--.  1 root root    47248  6月 22 2012 libevent_core.a

lrwxrwxrwx.  1 root root       26  1月  4 20:09 libevent_core.so -> libevent_core-1.4.so.2.1.3

lrwxrwxrwx.  1 root root       27  1月  4 20:09 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3

-rwxr-xr-x.  1 root root    84228  6月 22 2012 libevent_extra-1.4.so.2.1.3

-rw-r--r--.  1 root root       103130  6月  22 2012  libevent_extra.a

lrwxrwxrwx.  1 root root       27  1月  4 20:09  libevent_extra.so -> libevent_extra-1.4.so.2.1.3

lrwxrwxrwx.  1 root root       21  1月  4 20:09  libevent.so -> libevent-1.4.so.2.1.3

3.先下载memcached-1.2.8-repcached-2.2.tar.gz包(这个包本身自带memcached模块)

[root@h4 ~]# mkdir memcached        建立一个目录把包下载到目录中

[root@h4 memcached]#

wget http://downloads.sourceforge.net ... epcached-2.2.tar.gz

这个sourceforge网站下载时好时坏,可能下载不了,我也是好不容易得来的,我把它放在

http://f.dataguru.cn/thread-46524-1-1.html 有需要的筒子们赶紧下载哦

4.解tar包

[root@h4 memcached]# tar -zxvf memcached-1.2.8-repcached-2.2.tar.gz

[root@h4 memcached]# cd memcached-1.2.8-repcached-2.2          进入目录进行编译

5.编译三部曲

第一步 编译好后产生makefile文件

[root@h4 memcached-1.2.8-repcached-2.2]# ./configure --enable-replication --program-transform-name=s/memcached/repcached/ --with-libevent=/usr/local/libevent

checking build system type... i686-pc-linux-gnu

checking host system type... i686-pc-linux-gnu

checking target system type... i686-pc-linux-gnu

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking for gcc... gcc

checking for C compiler default output file name... a.out

checking whether the C compiler works... yes

checking whether we are cross compiling... no

checking for suffix of executables...

checking for suffix of object files... o

checking whether we are using the GNU C compiler... yes

checking whether gcc accepts -g... yes

checking for gcc option to accept ISO C89... none needed

checking for style. of include used by make... GNU

checking dependency style. of gcc... gcc3

checking whether gcc and cc understand -c and -o together... yes

checking for a BSD-compatible install... /usr/bin/install -c

checking for libevent directory... (system)

checking for library containing socket... none required

checking for library containing gethostbyname... none required

checking for library containing mallinfo... none required

checking for daemon... yes

checking how to run the C preprocessor... gcc -E

checking for grep that handles long lines and -e... /bin/grep

checking for egrep... /bin/grep -E

checking for ANSI C header files... yes

checking for sys/types.h... yes

checking for sys/stat.h... yes

checking for stdlib.h... yes

checking for string.h... yes

checking for memory.h... yes

checking for strings.h... yes

checking for inttypes.h... yes

checking for stdint.h... yes

checking for unistd.h... yes

checking for stdbool.h that conforms to C99... yes

checking for _Bool... yes

checking for an ANSI C-conforming const... yes

checking malloc.h usability... yes

checking malloc.h presence... yes

checking for malloc.h... yes

checking for struct mallinfo.arena... yes

checking for socklen_t... yes

checking for endianness... little

checking for mlockall... yes

checking for getpagesizes... no

checking for memcntl... no

configure: creating ./config.status

config.status: creating Makefile

config.status: creating doc/Makefile

config.status: creating config.h

config.status: executing depfiles commands

[root@h4 memcached-1.2.8-repcached-2.2]# ll | grep Makefile

-rw-r--r--. 1 root root  34856  1  4  20:53 Makefile       这个文件已经产生

第二步

[root@h4 memcached-1.2.8-repcached-2.2]# make

make  all-recursive

make[1]: Entering directory `/root/memcached/memcached-1.2.8-repcached-2.2'

Making all in doc

。。。。。。。。。。。。。。。省略

第三步

[root@h4 memcached-1.2.8-repcached-2.2]# make install

Making install in doc

make[1]: Entering directory `/root/memcached/memcached-1.2.8-repcached-2.2/doc'

make[2]: Entering directory `/root/memcached/memcached-1.2.8-repcached-2.2/doc'

make[2]: Nothing to be done for `install-exec-am'.

test -z "/usr/local/share/man/man1" || /bin/mkdir -p "/usr/local/share/man/man1"

/usr/bin/install -c -m 644 './memcached.1' '/usr/local/share/man/man1/repcached.1'

make[2]: Leaving directory `/root/memcached/memcached-1.2.8-repcached-2.2/doc'

make[1]: Leaving directory `/root/memcached/memcached-1.2.8-repcached-2.2/doc'

make[1]: Entering directory `/root/memcached/memcached-1.2.8-repcached-2.2'

make[2]: Entering directory `/root/memcached/memcached-1.2.8-repcached-2.2'

test -z "/usr/local/bin" || /bin/mkdir -p "/usr/local/bin"

  /usr/bin/install -c 'memcached' '/usr/local/bin/repcached'

  /usr/bin/install -c 'memcached-debug' '/usr/local/bin/repcached-debug'

make[2]: Nothing to be done for `install-data-am'.

make[2]: Leaving directory `/root/memcached/memcached-1.2.8-repcached-2.2'

make[1]: Leaving directory `/root/memcached/memcached-1.2.8-repcached-2.2'

到此完成编译任务

 

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26686207/viewspace-752187/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26686207/viewspace-752187/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值