Memcached Overview
一套开源的高性能分布式内存对象缓存系统
所有的数据都存储在内存中
支持任意存储类型的数据
提高网站的访问速度
Memcached common architecture
Memcached routing algorithm
●求余数hash算法
先用key做hash运算得到一个整数,再去做hash算法,根据余数进行路
由,这种算法适合大多数据需求,但是不适合用在动态变化的环境中
●一致性hash算法
按照hash算法把对应的key通过一定的hash算法处理后映射形成一一个首
尾相接闭合循环,然后通过使用与对象存储一样的hash算法将机器也映
射到环中,顺时针方向计算将所有对象存储到里自己最近的机器中
适合在动态变化的环境中使用
Environmental deployment
Server Role | IP addresses | You need to install software packages |
---|---|---|
Memcached server | 192.168.142.139 | memcached-1.5.6.tar.gz, libevent-2.1.8-stable.tar.gz |
Memcache client | 192.168.142.130 | memcache-2.2.7.tgz 、LAMP(httpd、mysql、php) |
Step 1: Configure Memcached server
1. Install the build environment
[root@server ~]# yum install gcc gcc-c++ make -y
2. Mount remote access to resources package
[root@server ~]# mount.cifs //192.168.142.1/memcached /mnt
3. Extract Resource Kit
[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. Installation configured and compiled 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. compile and install 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. Establish a soft link to the service command system, easy to operate
[root@server memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin
7. Start services, -d daemon; -m cache size is 32M ;-p port 11211; -u user
[root@server memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
8. Check the service port status
[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. Installation and use telnet connection 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
Step Two: Configure Client LAMP architecture
1. uses scripts to install architecture
[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. Execute the script, based on the corresponding content information input to prompt
[root@client ~]# source lamp.sh
3. Verify that the database is running
#进入数据库
[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. Modify the PHP content Home
[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. Use a browser to access http://192.168.142.130/index.php test was successful
The third step: the client installation Memcache
1. Installation environment package
[root@client ~]# yum install autoconf -y
2. Mount remote access to resources package
[root@client ~]# mount.cifs //192.168.142.1/memcached /mnt
3. Extract Resource Kit
[root@client memcached]# tar zvxf memcache-2.2.7.tgz -C /opt/
4. cut memcache-2.2.7 directory
[root@client memcached]# cd /opt/memcache-2.2.7/
The increase after PHP module configured to memcache
[root@client memcache-2.2.7]# /usr/local/php5/bin/phpize
6. Configure memcache
[root@client memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config
7. compile and install
[root@client memcache-2.2.7]# make && make install
8. Modify the main configuration file 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. The written test page, the test 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. Restart httpd service
[root@client memcache-2.2.7]# service httpd restart
11. Use a browser to access http://192.168.142.130/index.php test was successful