簡介:
Memcached 是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它經過在內存中緩存數據和對象來減小讀取數據庫的次數,從而提升動態、數據庫驅動網站的速度。Memcached基於一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,可是客戶端能夠用任何語言來編寫,並經過memcached協議與守護進程通訊。
特色:
memcached做爲高速運行的分佈式緩存服務器,具備如下的特色。
· 協議簡單
· 基於libevent的事件處理
· 內置內存存儲方式
· memcached不互相通訊的分佈式
具體實驗:
實驗環境:兩臺CentOS7.3系統完成(一臺Memcached服務器,一臺基於LAMP架構進行的Memcached API客戶端)php
Memcached服務器 IP:192.168.120.128 Memcached API客戶端 IP:192.168.120.135 **實驗所需軟件包:** Memcached壓縮包連接:https://pan.baidu.com/s/1pekiFGA0rv3XyM7KY58jUA
提取碼:6uff
=========================memcached服務器端========================
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.10/rhel7 /abc
Password for root@//192.168.100.10/rhel7:
[root@localhost ~]# cd /abc/memcached/
[root@localhost memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/
[root@localhost memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/
[root@localhost memcached]# cd /opt/
[root@localhost opt]# yum install gcc gcc-c++ make -y
[root@localhost opt]# cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@localhost libevent-2.1.8-stable]# make && make install
[root@localhost libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@localhost memcached-1.5.6]# make && make install
[root@localhost memcached-1.5.6]# ln -s /usr/local/memcached/bin/ /usr/local/bin/
[root@localhost memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
[root@localhost 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
[root@localhost memcached-1.5.6]# systemctl stop firewalld.service
[root@localhost memcached-1.5.6]# setenforce 0html
----------------------------數據操做--------------------------------------------------
[root@localhost memcached-1.5.6]# yum install telnet -y
[root@localhost memcached-1.5.6]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.mysql
add username 0 0 7
//不進行壓縮和序列化標識 ,數據過時時間爲永不過時 ,即將輸入的字符串長度(0 0 7含義)c++
example //輸入數據sql
get username //獲取數據
VALUE username 0 7
example數據庫
gets username
VALUE username 0 7 1 //最後一位時更新因子會自增1
exampleapache
set username 0 0 10 //更新信息,若鍵名不存在,則自行添加
everythingvim
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========================
第二臺
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.10/rhel7 /abc
[root@localhost ~]# cd /abc/memcached/LAMP-php5.6/
[root@localhost LAMP-php5.6]# tar zxvf apr-1.6.2.tar.gz -C /opt/
[root@localhost LAMP-php5.6]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/
[root@localhost LAMP-php5.6]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@localhost opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl
[root@localhost opt]# cd httpd-2.4.29
[root@localhost httpd-2.4.29]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[root@localhost httpd-2.4.29]# make && make install
[root@localhost httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@localhost httpd-2.4.29]# vim /etc/init.d/httpd
#chkconfig: 35 85 21 //第二行插入
[root@localhost httpd-2.4.29]# chkconfig --add httpd
[root@localhost 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行)
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/ /usr/local/bin/
[root@localhost httpd-2.4.29]# apachectl -t
Syntax OK
[root@localhost httpd-2.4.29]# service httpd start
[root@localhost httpd-2.4.29]# netstat -ntap | grep 80
tcp 0 0 192.168.120.135:80 0.0.0.0: LISTEN 71464/httpd
------------------------------安裝Mysql------------------------------------------
[root@localhost httpd-2.4.29]# yum install -y ncurses-devel autoconf cmake
[root@localhost httpd-2.4.29]# cd /abc/memcached/LAMP-php5.6/
[root@localhost LAMP-php5.6]# tar xzvf mysql-5.6.26.tar.gz -C /opt/
[root@localhost 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
[root@localhost LAMP-php5.6]#make && make install
(等待很長時間)
[root@localhost mysql-5.6.26]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆蓋"/etc/my.cnf"? yes
[root@localhost mysql-5.6.26]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql-5.6.26]# chmod 755 /etc/init.d/mysqld
[root@localhost mysql-5.6.26]# chkconfig --add /etc/init.d/mysqld
[root@localhost mysql-5.6.26]# chkconfig mysqld --level 235 on
[root@localhost mysql-5.6.26]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@localhost mysql-5.6.26]# source /etc/profile
[root@localhost mysql-5.6.26]# useradd -s /sbin/nologin mysql
[root@localhost mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost mysql-5.6.26]# /usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
[root@localhost mysql-5.6.26]# ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
[root@localhost mysql-5.6.26]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql //第46行
datadir=/home/mysql //第47行
[root@localhost mysql-5.6.26]# service mysqld start
Starting MySQL. SUCCESS!
[root@localhost mysql-5.6.26]# netstat -anpt | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 86823/mysqld
[root@localhost mysql-5.6.26]# mysqladmin -u root -p password "abc123"
Enter password:
[root@localhost mysql-5.6.26]# mysql -uroot -pabc123
------------------------------------------------如下安裝PHP----------------------------
[root@localhost mysql-5.6.26]#yum -y install \
gd \
libpng \
libpng-devel \
pcre \
pcre-devel \
libxml2-devel \
libjpeg-devel
[root@localhost mysql-5.6.26]# cd /abc/memcached/LAMP-php5.6/
[root@localhost LAMP-php5.6]# tar xjvf php-5.6.11.tar.bz2 -C /opt/
[root@localhost LAMP-php5.6]# cd /opt/php-5.6.11/
[root@localhost 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
[root@localhost php-5.6.11]# make && make install
[root@localhost php-5.6.11]# cp php.ini-development /usr/local/php5/php.ini
[root@localhost php-5.6.11]# ln -s /usr/local/php5/bin/ /usr/local/bin/
[root@localhost php-5.6.11]# ln -s /usr/local/php5/sbin/ /usr/local/sbin/
[root@localhost 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行
[root@localhost php-5.6.11]#vim /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
[root@localhost htdocs]# mv index.html index.php
mv:是否覆蓋"index.php"? yes
[root@localhost htdocs]# service httpd restart
[root@localhost htdocs]# systemctl stop firewalld.service
[root@localhost htdocs]# setenforce 0
在網頁測試「http://192.168.120.135/index.php」
[root@localhost 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();
?>
[root@localhost 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)
[root@localhost htdocs]# service httpd restart
在網頁測試「http://192.168.120.135/index.php」
#測試php可否解析鏈接mysql
[root@localhost htdocs]# cd /abc/memcached/
[root@localhost memcached]# tar zxvf memcache-2.2.7.tgz -C /opt/
[root@localhost memcached]# cd /opt/memcache-2.2.7/
[root@localhost 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
[root@localhost memcache-2.2.7]# ./configure \
--enable-memcache \
--with-php-config=/usr/local/php5/bin/php-config
[root@localhost memcache-2.2.7]# make && make install
[root@localhost 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行
[root@localhost memcache-2.2.7]# cd /usr/local/httpd/htdocs/
[root@localhost 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;
?>
[root@localhost htdocs]# service httpd restart
在網頁測試「http://192.168.120.135/index.php」
基礎Memcached服務就搭建完畢了,下一篇將介紹Memcached羣集服務。