千家信息网

Memcached原理与部署

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,Memcached概述一套开源的高性能分布式内存对象缓存系统所有的数据都存储在内存中支持任意存储类型的数据提高网站的访问速度Memcached常用架构Memcached路由算法●求余数hash算法先用
千家信息网最后更新 2025年01月31日Memcached原理与部署

Memcached概述

一套开源的高性能分布式内存对象缓存系统所有的数据都存储在内存中支持任意存储类型的数据提高网站的访问速度

Memcached常用架构

Memcached路由算法

●求余数hash算法先用key做hash运算得到一个整数,再去做hash算法,根据余数进行路由,这种算法适合大多数据需求,但是不适合用在动态变化的环境中●一致性hash算法按照hash算法把对应的key通过一定的hash算法处理后映射形成一一个首尾相接闭合循环,然后通过使用与对象存储一样的hash算法将机器也映射到环中,顺时针方向计算将所有对象存储到里自己最近的机器中适合在动态变化的环境中使用

环境部署

服务器角色IP地址需要安装的软件包
Memcached服务器192.168.142.139memcached-1.5.6.tar.gz、libevent-2.1.8-stable.tar.gz
Memcache客户端192.168.142.130memcache-2.2.7.tgz 、LAMP(httpd、mysql、php)

第一步:配置Memcached服务器

1.安装编译环境

[root@server ~]# yum install gcc gcc-c++ make -y

2.远程挂载获取资源包

[root@server ~]# mount.cifs //192.168.142.1/memcached /mnt

3.解压资源包

[root@server memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt[root@server memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt[root@server memcached]# tar zxvf magent-0.5.tar.gz -C /opt

4.配置并编译安装libevent

[root@server memcached]# cd /opt/libevent-2.1.8-stable[root@server libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent[root@server libevent-2.1.8-stable]# make && make install

5.编译安装memcached

[root@server libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/[root@server memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/[root@server memcached-1.5.6]# make && make install

6.建立服务命令的软链接到系统,便于操作

[root@server memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin

7.启动服务,-d守护进程 ;-m缓存大小32M ;-p端口11211; -u用户

[root@server memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root

8.查看服务端口状态

[root@server memcached-1.5.6]# netstat -ntap | grep memcachedtcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      129587/memcached    tcp6       0      0 :::11211                :::*                    LISTEN      129587/memcached   

9.安装与使用telnet连接memcached

[root@server memcached-1.5.6]# yum install telnet -y#连接使用telnet [root@server memcached-1.5.6]# telnet 127.0.0.1 11211#添加键名,不设置序列号,不设置过期时间,即将输入的字节长度add username 0 0 0 #查看键值gets username #最后一个数字是跟新因子VALUE username 0 7 1   #更新,若键名不存在,则报错replace username 0 0 9#更新,对不存在的进行添加set username 0 0 10#检查更新,更新因子相等则更新否则返回EXISTScas username 0 0 7 4  #键后追加数据 最后数字是追加的字节数apppend username 0 0 4#键值前追加数据prepend username 0 0 2#删除键delete username  #清除所有缓存数据flush_all #显示状态信息stats #返回所有键值对的统计信息stats items#返回指定存储空间的键值对 stats cachedump 1 0#显示各个slab的信息stats slabs#输出所有item的大小和个数stats sizes#退出quit  

第二步:配置客户端的LAMP架构

1.使用脚本安装架构

[root@client ~]# vim lamp.sh#!/bin/bash#请使用source执行此脚本#数据库默认密码为123123#远程挂载df -hT | grep -o "cifs"if [ $? -ne 0 ];thenecho "Start mount"yum install expect -yread -p "请输入提供资源主机IP:" gongip/usr/bin/expect <<-EOFspawn mount.cifs //$gongip/LAMP /mnt/expect {    "Password*"    {send "\r"}}expect eofEOFfi#安装apache服务cd /mnt/tar zxf apr-1.6.2.tar.gz -C /opttar zxf apr-util-1.6.0.tar.gz -C /opt/tar jxf httpd-2.4.29.tar.bz2 -C /opt/cd /opt/mv apr-1.6.2/ httpd-2.4.29/srclib/aprmv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-utilyum -y install gcc \gcc-c++ \make \pcre-devel \expat-devel \perlcd /opt/httpd-2.4.29/./configure --prefix=/usr/local/httpd \--enable-so \--enable-rewrite \--enable-charset-lite \--enable-cgimake && make installcp /usr/local/httpd/bin/apachectl /etc/init.d/httpdsed -i -e '2a# chkconfig: 35 85 21\n# description: Apache is a World Wide Web server' /etc/init.d/httpdchkconfig --add httpdsed -i -e '197d' -e '196aServerName www.kgc.com:80' /usr/local/httpd/conf/httpd.confip=`ifconfig ens33 | awk 'NR==2{print $2}'`sed -i -e '52s/^/#/' -e '51d' -e "50aListen $ip:80" /usr/local/httpd/conf/httpd.confln -s /usr/local/httpd/conf/httpd.conf /etc/ln -s /usr/local/httpd/bin/* /usr/local/bin/systemctl stop firewalld.servicesetenforce 0service httpd startnetstat -ntuap | grep httpdif [ $? -eq 0 ];then     echo Apache服务启动else     echo 启动失败    exit 1fi#安装mysql服务echo "================================="echo "安装mysql数据库,请稍后。。。"sleep 3yum install -y ncurses-devel autoconf cmakecd /mnt/tar zxf mysql-5.6.26.tar.gz -C /opt/cd /opt/mysql-5.6.26cmake  \-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DEXTRA_CHARSETS=all \-DSYSCONFIDIR=/etc \-DMYSQL_DATADIR=/home/mysql/ \-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sockecho "进行编译安装,要很久,请稍后。。。"make && make installcp support-files/my-default.cnf /etc/my.cnfcp support-files/mysql.server /etc/init.d/mysqldchmod +x /etc/init.d/mysqldchkconfig --add /etc/init.d/mysqldchkconfig --level 235 mysqld onecho "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profilesource /etc/profileecho $PATH | grep -o "mysql"if [ $? -eq 0 ];then     echo "添加成功"else     echo "失败"    exit 1fiuseradd -s /sbin/nologin mysqlchown -R mysql:mysql /usr/local/mysql//usr/local/mysql/scripts/mysql_install_db \--user=mysql \--ldata=/var/lib/mysql \--basedir=/usr/local/mysql \--datadir=/home/mysqlsed -i -e '47d' -e '46d' -e '45abasedir=/usr/local/mysql\ndatadir=/home/mysql' /etc/init.d/mysqldservice mysqld startnetstat -ntuap | grep mysqldif [ $? -eq 0 ];then        echo Mysql数据库启动else        echo 启动失败    exit 1 fi#read -p "请输入数据库密码(自定义):" mima/usr/bin/expect < /usr/local/httpd/htdocs/index.php <<-EOFEOFservice httpd stopservice httpd startrm -rf /opt/*umount /mntsource /etc/profileecho $PATH | grep -o "mysql"while [ $? -ne 0 ];do  while [ "a" = "a" ]    do    echo "错误!!"    sleep 2    donedoneecho "LAMP架构已完成,欢迎下次光临!"

2.执行脚本,根据提示信息输入对应内容即可

[root@client ~]# source lamp.sh

3.验证数据库运行

#进入数据库[root@client php-5.6.11]# mysql -uroot -p123123#创建数据库名为pokemysql> create database poke;#提权poke用户mysql>  grant all on poke.* to 'pokeuser'@'client' identified by 'admin123';#刷新数据库mysql> flush privileges;#退出数据库mysql> quit

4.修改PHP首页内容

[root@client ~]# vim /usr/local/httpd/htdocs/index.phpSuccess!!";else echo "Fail!!";mysql_close();?>

5.使用浏览器访问http://192.168.142.130/index.php测试是否成功

第三步:客户端安装Memcache

1.安装环境包

[root@client ~]# yum install autoconf -y

2.远程挂载获取资源包

[root@client ~]# mount.cifs //192.168.142.1/memcached /mnt

3.解压资源包

[root@client memcached]# tar zvxf memcache-2.2.7.tgz -C /opt/

4.切入memcache-2.2.7目录

[root@client memcached]# cd /opt/memcache-2.2.7/

5.增加PHP的模块后再对memcache进行配置

[root@client memcache-2.2.7]# /usr/local/php5/bin/phpize

6.配置memcache

[root@client memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config

7.编译安装

[root@client memcache-2.2.7]# make && make install

8.修改PHP主配置文件

[root@client memcache-2.2.7]# vim /usr/local/php5/php.ini#替换第736行引号中内容extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"#追加下述条目 extension = memcache.so

9.编写测试网页,测试memcache

[root@client memcache-2.2.7]# vim /usr/local/httpd/htdocs/index.phpconnect('192.168.142.139',11211);$memcache->set('key','Memcache test Successfull!',0,60);$result = $memcache->get('key');unset($memcache);echo $result;?>

10.重启httpd服务

[root@client memcache-2.2.7]# service httpd restart

11.使用浏览器访问http://192.168.142.130/index.php测试是否成功

0