Memcached原理与部署

帅比萌擦擦* 提交于 2019-12-17 17:28:39

Memcached概述

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

Memcached常用架构

在这里插入图片描述

Memcached路由算法

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

环境部署

服务器角色 IP地址 需要安装的软件包
Memcached服务器 192.168.142.139 memcached-1.5.6.tar.gz、libevent-2.1.8-stable.tar.gz
Memcache客户端 192.168.142.130 memcache-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 memcached
tcp        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

#检查更新,更新因子相等则更新否则返回EXISTS
cas 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 ];then
echo "Start mount"
yum install expect -y
read -p "请输入提供资源主机IP:" gongip
/usr/bin/expect <<-EOF
spawn mount.cifs //$gongip/LAMP /mnt/
expect {
    "Password*"
    {send "\r"}
}
expect eof
EOF
fi

#安装apache服务
cd /mnt/

tar zxf apr-1.6.2.tar.gz -C /opt
tar 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/apr
mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util

yum -y install gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl

cd /opt/httpd-2.4.29/

./configure --prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi

make && make install

cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd

sed -i -e '2a# chkconfig: 35 85 21\n# description: Apache is a World Wide Web server' /etc/init.d/httpd

chkconfig --add httpd

sed -i -e '197d' -e '196aServerName www.kgc.com:80' /usr/local/httpd/conf/httpd.conf
ip=`ifconfig ens33 | awk 'NR==2{print $2}'`

sed -i -e '52s/^/#/' -e '51d' -e "50aListen $ip:80" /usr/local/httpd/conf/httpd.conf

ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

systemctl stop firewalld.service
setenforce 0
service httpd start

netstat -ntuap | grep httpd
if [ $? -eq 0 ];then 
    echo Apache服务启动
else 
    echo 启动失败
    exit 1
fi

#安装mysql服务
echo "================================="
echo "安装mysql数据库,请稍后。。。"
sleep 3

yum install -y ncurses-devel autoconf cmake
cd /mnt/
tar zxf mysql-5.6.26.tar.gz -C /opt/
cd /opt/mysql-5.6.26

cmake  \
-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.sock

echo "进行编译安装,要很久,请稍后。。。"
make && make install

cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld
chkconfig --add /etc/init.d/mysqld
chkconfig --level 235 mysqld on

echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile

echo $PATH | grep -o "mysql"
if [ $? -eq 0 ];then 
    echo "添加成功"
else 
    echo "失败"
    exit 1
fi

useradd -s /sbin/nologin mysql

chown -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/mysql

sed -i -e '47d' -e '46d' -e '45abasedir=/usr/local/mysql\ndatadir=/home/mysql' /etc/init.d/mysqld
service mysqld start

netstat -ntuap | grep mysqld
if [ $? -eq 0 ];then
        echo Mysql数据库启动
else
        echo 启动失败
    exit 1 
fi

#read -p "请输入数据库密码(自定义):" mima
/usr/bin/expect <<EOF
spawn mysqladmin -u root -p password 123123 
expect {
    "Enter*"
    {send "\r"}
}
expect eof
EOF

#安装PHP工具
yum install -y gd \
libpng \
libpng-devel \
pcre \
pcre-devel \
libxml2-devel \
libjpeg-devel

cd ~
tar jxf /mnt/php-5.6.11.tar.bz2 -C /opt/
cd /opt/php-5.6.11

./configure \
--prefix=/usr/local/php5 \
--with-gd \
--with-zlib \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php5 \
--enable-mbstring

make && make install

cp -p php.ini-development /usr/local/php5/php.ini
ln -s /usr/local/php5/bin/* /usr/local/bin/

sed -i '393a    AddType application/x-httpd-php .php\n    AddType application/x-httpd-php-source .phps' /usr/local/httpd/conf/httpd.conf

sed -i -e '256d' -e '255aDirectoryIndex index.html index.php' /usr/local/httpd/conf/httpd.conf

cat > /usr/local/httpd/htdocs/index.php <<-EOF
<?php
phpinfo();
?>
EOF

service httpd stop
service httpd start

rm -rf /opt/*
umount /mnt

source /etc/profile
echo $PATH | grep -o "mysql"
while [ $? -ne 0 ];do
  while [ "a" = "a" ]
    do
    echo "错误!!"
    sleep 2
    done
done
echo "LAMP架构已完成,欢迎下次光临!"

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

[root@client ~]# source lamp.sh

3.验证数据库运行

#进入数据库
[root@client php-5.6.11]# mysql -uroot -p123123

#创建数据库名为poke
mysql> 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.php

<?php
$link=mysql_connect('192.168.142.130','pokeuser','admin123');
if($link) echo "<h1>Success!!</h1>";
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.php

<?php
$memcache = new Memcache();
#指向memcached服务器地址
$memcache->connect('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测试是否成功
在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!