您好,登錄后才能下訂單哦!
一套開源的高性能分布式內存對象緩存系統
所有的數據都存儲在內存中
支持任意存儲類型的數據
提高網站的訪問速度
●求余數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) |
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
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 "<h2>Success!!</h2>";
else echo "Fail!!";
mysql_close();
?>
5.使用瀏覽器訪問http://192.168.142.130/index.php測試是否成功
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測試是否成功
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。