简介:
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
特点:
memcached作为高速运行的分布式缓存服务器,具有以下的特点。
· 协议简单
· 基于libevent的事件处理
· 内置内存存储方式
· memcached不互相通信的分布式
具体实验:
实验环境:两台CentOS7.3系统完成(一台Memcached服务器,一台基于LAMP架构进行的Memcached API客户端)
Memcached服务器 IP:192.168.120.128
Memcached API客户端 IP:192.168.120.135
**实验所需软件包:**
Memcached压缩包链接:https://pan.baidu.com/s/1pekiFGA0rv3XyM7KY58jUA
提取码:6uff
=========================memcached服务器端========================
[[email protected] ~]# mkdir /abc
[[email protected] ~]# mount.cifs //192.168.100.10/rhel7 /abc
Password for [email protected]//192.168.100.10/rhel7:
[[email protected] ~]# cd /abc/memcached/
[[email protected] memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/
[[email protected] memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/
[[email protected] memcached]# cd /opt/
[[email protected] opt]# yum install gcc gcc-c++ make -y
[[email protected] opt]# cd libevent-2.1.8-stable/
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[[email protected] libevent-2.1.8-stable]# make && make install
[[email protected] libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[[email protected] memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[[email protected] memcached-1.5.6]# make && make install
[[email protected] memcached-1.5.6]# ln -s /usr/local/memcached/bin/ /usr/local/bin/
[[email protected] memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
[[email protected] memcached-1.5.6]# netstat -ntap | grep memc
tcp 0 0 0.0.0.0:11211 0.0.0.0: LISTEN 58189/memcached
tcp6 0 0 :::11211 :::* LISTEN 58189/memcached
[[email protected] memcached-1.5.6]# systemctl stop firewalld.service
[[email protected] memcached-1.5.6]# setenforce 0
----------------------------数据操作--------------------------------------------------
[[email protected] memcached-1.5.6]# yum install telnet -y
[[email protected] memcached-1.5.6]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
add username 0 0 7
//不进行压缩和序列化标识 ,数据过期时间为永不过期 ,即将输入的字符串长度(0 0 7含义)
example //输入数据
get username //获取数据
VALUE username 0 7
example
gets username
VALUE username 0 7 1 //最后一位时更新因子会自增1
example
set username 0 0 10 //更新信息,若键名不存在,则自行添加
everything
replace username 0 0 8 //更新信息,若键名不存在,则报错
12345678
gets username
VALUE username 0 8 4
12345678
cas username 0 0 7 //检查更新,更新因子相等则更新否则返回EXISTS(不能更新)
logging
STORED
append username 0 0 7 //键值后追加数据
example
STORED
prepend username 0 0 2 //键值前追加数据
un
STORED
delecte username
flush_all //清除所有缓存数据
OK
stats //显示状态信息
quit //退出
=====================以下安装客户端--需要LAMP========================
第二台
[[email protected] ~]# mkdir /abc
[[email protected] ~]# mount.cifs //192.168.100.10/rhel7 /abc
[[email protected] ~]# cd /abc/memcached/LAMP-php5.6/
[[email protected] LAMP-php5.6]# tar zxvf apr-1.6.2.tar.gz -C /opt/
[[email protected] LAMP-php5.6]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/
[[email protected] LAMP-php5.6]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/
[[email protected] opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[[email protected] opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[[email protected] opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl
[[email protected] opt]# cd httpd-2.4.29
[[email protected] httpd-2.4.29]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[[email protected] httpd-2.4.29]# make && make install
[[email protected] httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[[email protected] httpd-2.4.29]# vim /etc/init.d/httpd
#chkconfig: 35 85 21 //第二行插入
[[email protected] httpd-2.4.29]# chkconfig --add httpd
[[email protected] httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf
ServerName www.yun.com:80 //第197行
Listen 192.168.120.135:80 //第51行(注释第52行)
[[email protected] httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[[email protected] httpd-2.4.29]# ln -s /usr/local/httpd/bin/ /usr/local/bin/
[[email protected] httpd-2.4.29]# apachectl -t
Syntax OK
[[email protected] httpd-2.4.29]# service httpd start
[[email protected] httpd-2.4.29]# netstat -ntap | grep 80
tcp 0 0 192.168.120.135:80 0.0.0.0: LISTEN 71464/httpd
------------------------------安装Mysql------------------------------------------
[[email protected] httpd-2.4.29]# yum install -y ncurses-devel autoconf cmake
[[email protected] httpd-2.4.29]# cd /abc/memcached/LAMP-php5.6/
[[email protected] LAMP-php5.6]# tar xzvf mysql-5.6.26.tar.gz -C /opt/
[[email protected] LAMP-php5.6]# 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
[[email protected] LAMP-php5.6]#make && make install
(等待很长时间)
[[email protected] mysql-5.6.26]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? yes
[[email protected] mysql-5.6.26]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.6.26]# chmod 755 /etc/init.d/mysqld
[[email protected] mysql-5.6.26]# chkconfig --add /etc/init.d/mysqld
[[email protected] mysql-5.6.26]# chkconfig mysqld --level 235 on
[[email protected] mysql-5.6.26]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[[email protected] mysql-5.6.26]# source /etc/profile
[[email protected] mysql-5.6.26]# useradd -s /sbin/nologin mysql
[[email protected] mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/
[[email protected] mysql-5.6.26]# /usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
[[email protected] mysql-5.6.26]# ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
[[email protected] mysql-5.6.26]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql //第46行
datadir=/home/mysql //第47行
[[email protected] mysql-5.6.26]# service mysqld start
Starting MySQL. SUCCESS!
[[email protected] mysql-5.6.26]# netstat -anpt | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 86823/mysqld
[[email protected] mysql-5.6.26]# mysqladmin -u root -p password "abc123"
Enter password:
[[email protected] mysql-5.6.26]# mysql -uroot -pabc123
------------------------------------------------以下安装PHP----------------------------
[[email protected] mysql-5.6.26]#yum -y install \
gd \
libpng \
libpng-devel \
pcre \
pcre-devel \
libxml2-devel \
libjpeg-devel
[[email protected] mysql-5.6.26]# cd /abc/memcached/LAMP-php5.6/
[[email protected] LAMP-php5.6]# tar xjvf php-5.6.11.tar.bz2 -C /opt/
[[email protected] LAMP-php5.6]# cd /opt/php-5.6.11/
[[email protected] 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
[[email protected] php-5.6.11]# make && make install
[[email protected] php-5.6.11]# cp php.ini-development /usr/local/php5/php.ini
[[email protected] php-5.6.11]# ln -s /usr/local/php5/bin/ /usr/local/bin/
[[email protected] php-5.6.11]# ln -s /usr/local/php5/sbin/ /usr/local/sbin/
[[email protected] php-5.6.11]# vim /etc/httpd.conf
AddType application/x-httpd-php .php //第394行
AddType application/x-httpd-php-source .phps //第395行
DirectoryIndex index.html index.php //第256行
[[email protected] php-5.6.11]#vim /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
[[email protected] htdocs]# mv index.html index.php
mv:是否覆盖"index.php"? yes
[[email protected] htdocs]# service httpd restart
[[email protected] htdocs]# systemctl stop firewalld.service
[[email protected] htdocs]# setenforce 0
在网页测试“http://192.168.120.135/index.php”
[[email protected] htdocs]# vim index.php
<?php
$link=mysql_connect(‘192.168.120.135‘,‘skyuser‘,‘admin123‘);
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>
[[email protected] htdocs]# mysql -uroot -p
Enter password:
mysql> CREATE DATABASE sky;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT all ON sky.* TO ‘skyuser‘@‘%‘ IDENTIFIED BY ‘admin123‘;
Query OK, 0 rows affected (0.12 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.09 sec)
[[email protected] htdocs]# service httpd restart
在网页测试“http://192.168.120.135/index.php”
#测试php能否解析连接mysql
[[email protected] htdocs]# cd /abc/memcached/
[[email protected] memcached]# tar zxvf memcache-2.2.7.tgz -C /opt/
[[email protected] memcached]# cd /opt/memcache-2.2.7/
[[email protected] memcache-2.2.7]# /usr/local/php5/bin/phpize //增加为PHP的模块后再对memcache进行配置
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[[email protected] memcache-2.2.7]# ./configure \
--enable-memcache \
--with-php-config=/usr/local/php5/bin/php-config
[[email protected] memcache-2.2.7]# make && make install
[[email protected] memcache-2.2.7]# vim /usr/local/php5/php.ini
extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/" //第737行
extension=memcache.so //第738行
[[email protected] memcache-2.2.7]# cd /usr/local/httpd/htdocs/
[[email protected] htdocs]# vim index.php
<?php
$memcache = new Memcache();
$memcache->connect(‘192.168.120.128‘,11211);
$memcache->set(‘key‘,‘Memcache test Successfull!‘,0,60);
$result = $memcache->get(‘key‘);
unset($memcache);
echo $result;
?>
[[email protected] htdocs]# service httpd restart
在网页测试“http://192.168.120.135/index.php”
基础Memcached服务就搭建完毕了,下一篇将介绍Memcached群集服务。
原文地址:http://blog.51cto.com/13756916/2178767