溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么搭建Memcache服務

發(fā)布時間:2020-06-01 15:29:32 來源:億速云 閱讀:332 作者:鴿子 欄目:系統(tǒng)運維

安裝Memcache

Memcache的搭建需要借助于LAMP或LNMP,本篇博文采用LNMP結構

安裝環(huán)境如下:
192.168.148.129   Memcache
192.168.148.130   php
192.168.148.131   nginx
192.168.148.136   mysql
  • 安裝nginx

下載nginx軟件包

[root@bogon ~]#  useradd -M -s /sbin/nologin nginx
[root@bogon ~]#  yum -y install openssl-devel
[root@bogon ~]# tar zxf pcre-8.39.tar.gz -C /usr/src
[root@bogon ~]#  tar zxf zlib-1.2.8.tar.gz -C /usr/src
[root@bogon ~]#  tar zxf nginx-1.14.0.tar.gz -C /usr/src
[root@bogon ~]#  cd /usr/src/nginx-1.14.0/
[root@bogon  nginx-1.14.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx --group=nginx --with-http_dav_module \
--with-http_stub_status_module --with-http_addition_module \
--with-http_sub_module --with-http_flv_module --with-http_mp4_module \
--with-pcre=/usr/src/pcre-8.39 --with-zlib=/usr/src/zlib-1.2.8 \
--with-http_ssl_module --with-http_gzip_static_module && make && make install
[root@bogon ~]#  ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@bogon ~]#  nginx
[root@bogon ~]#  netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8460/nginx: master  

下載php軟件包

[root@localhost ~]# yum -y install libxml2-devel lzip2-devel libcurl-devel  libmcrypt-devel openssl-devel bzip2-devel 
[root@localhost ~]# tar zxf libmcrypt-2.5.7.tar.gz 
[root@localhost ~]# cd libmcrypt-2.5.7/
[root@localhost libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
[root@localhost ~]# tar zxf php-5.6.27.tar.gz 
[root@localhost ~]# cd php-5.6.27/
[root@localhost php-5.6.27]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets \
--enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
--with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt  \
--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d \
--with-bz2 --enable-maintainer-zts && make && make install
[root@localhost php-5.6.27]# cp php.ini-production /etc/php.ini
[root@localhost php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.27]# chmod +x /etc/init.d/php-fpm 
[root@localhost php-5.6.27]# chkconfig --add php-fpm
[root@localhost php-5.6.27]# chkconfig php-fpm on
[root@localhost php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
[root@localhost php-5.6.27]# vim /usr/local/php5.6/etc/php-fpm.conf
修改內(nèi)容如下: 
pid = run/php-fpm.pid 
listen = 192.168.31.141:9000    \\本地ip地址(千萬不能用127.0.0.1)
pm.max_children = 50 
pm.start_servers = 5 
pm.min_spare_servers = 5 
pm.max_spare_servers = 35 
[root@localhost php-5.6.27]# service php-fpm start
Starting php-fpm  done
[root@localhost php-5.6.27]# netstat -anpt | grep php-fpm
tcp        0      0 192.168.148.130:9000    0.0.0.0:*               LISTEN      130988/php-fpm: mas 
  • 安裝MySQL數(shù)據(jù)庫

下載mysql腳本一鍵安裝

[root@bogon ~]# mysql -u root -p123
mysql> create database testdb1;
mysql> use testdb1;
mysql> grant all on *.* to xws@'192.168.148.%' identified by '123456';
mysql> create table test1(id int not null auto_increment,name varchar(20) default null,primary  key (id)) engine=innodb auto_increment=1 default charset=utf8;
mysql> insert into test1(name) values ('tom1'),('tom2'),('tom3'),('tom4'),('tom5');
mysql> select * from test1;
+----+------+
| id | name |
+----+------+
|  1 | tom1 |
|  2 | tom2 |
|  3 | tom3 |
|  4 | tom4 |
|  5 | tom5 |
+----+------+
5 rows in set (0.00 sec)
nginx操作如下:
[root@bogon nginx-1.14.0]# vim /usr/local/nginx/conf/nginx.conf  //修改如下(大約在43行)

        location / {
            root   html;
            index  index.php index.html index.htm;
        }
          location ~ \.php$ {
            root           /var/www/html;
            fastcgi_pass   192.168.148.130:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
[root@bogon nginx-1.14.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@bogon nginx-1.14.0]# nginx -s reload

php操作如下:

[root@bogon ~]# mkdir -p /var/www/html
[root@bogon ~]# vim /var/www/html/test.php
<?php
phpinfo();
?>
[root@bogon ~]# vim /var/www/html/test1.php
<?php
$link=mysqli_connect('192.168.148.136','xws','123456');
if($link) echo "恭喜你,數(shù)據(jù)庫連接成功?。。?quot;; else echo "connect shibai";
mysqli_close($link);
?>

客戶端訪問如下:

怎么搭建Memcache服務

怎么搭建Memcache服務

  • 安裝Memecache

下載memcache

[root@bogon ~]# tar zxf libevent-2.0.22-stable.tar.gz -C /usr/src/
[root@bogon ~]# cd /usr/src/libevent-2.0.22-stable/
[root@bogon libevent-2.0.22-stable]# ./configure && make && make install 
[root@bogon ~]# tar zxf memcached-1.4.33.tar.gz -C /usr/src/
[root@bogon ~]# cd /usr/src/memcached-1.4.33/
[root@bogon memcached-1.4.33]# ./configure --prefix=/usr/local/memcached \
> --with-libevent=/usr/local/ && make && make install
[root@bogon ~]# memcached -d -m 2048 -l 192.168.1.7 -p 11211 -c 10240 -P /usr/local/memcached/memcached.pid -u root
[root@bogon ~]# netstat -anpt | grep 11211
  • php操作如下(安裝Memecache客戶端)
[root@bogon ~]# tar zxf memcache-3.0.8.tgz -C /usr/src/
[root@bogon ~]# cd /usr/src/memcache-3.0.8/
[root@bogon memcache-3.0.8]# /usr/local/php5.6/bin/phpize
[root@PHP memcache-3.0.8]# ./configure --enable-memcache \
--with-php-config=/usr/local/php/bin/php-config && make && make install
//執(zhí)行后會顯示memcache.so存放的路徑
[root@PHP ~]# echo "extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/memcache.so" >> /etc/php.ini
//在PHP主配置文件中填寫memcache.so模塊存放的路徑
[root@PHP ~]# systemctl restart php-fpm
[root@PHP ~]# vim /var/www/html/test2.php
<?php
$memcache = new Memcache;
$memcache->connect('192.168.1.129', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 600) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 600 seconds)<br/>";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>";
var_dump($get_result);
?>
//此測試腳本是顯示memcached的版本
//并且向里面插入了一個緩存時間為600秒的鍵值對“test=123”,其ID為“key”

客戶端訪問如下:

怎么搭建Memcache服務

在PHP服務器上安裝telnet工具測試

[root@PHP ~]# yum -y install telnet
[root@PHP ~]# telnet 192.168.148.129 11211           //登陸到memcached的11211端口
Trying 192.168.148.129...
Connected to 192.168.148.129.
Escape character is '^]'.
get key              //查詢ID為“key”的鍵值對,可以看到我們測試腳本寫入的“test=123”
VALUE key 1 66
O:8:"stdClass":2:{s:8:"str_attr";s:4:"test";s:8:"int_attr";i:123;}
END
//在進行上面的get驗證時,需要將test2.php文件中插入的鍵值對的保存時間值改大一些
//或者重新訪問一下,以免緩存失效,查詢不到
quit                     //退出當前環(huán)境
Connection closed by foreign host.
[root@PHP ~]# 
測試Memcache緩存數(shù)據(jù)庫
[root@PHP ~]# vim /var/www/html/test4.php 
<?php
$memcachehost = '192.168.1.7';                      //指定Memcache服務器地址
$memcacheport = 11211;                                 //指定其開放的端口號
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select * from test1 limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
$conn=mysql_connect("192.168.1.8","lzj","123456");                        //指定數(shù)據(jù)庫服務器的IP地址、用戶及密碼
mysql_select_db(testdb1);
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result))
{
$arr[]=$row;
}
$f = 'mysql';
$memcache->add($key,serialize($arr),0,30);
$data = $arr ;
}
else{
$f = 'memcache';
$data_mem=$memcache->get($key);
$data = unserialize($data_mem);
}
echo $f;
echo "<br>";
echo "$key";
echo "<br>";
//print_r($data); 
foreach($data as $a)
{
echo "number is <b><font color=#FF0000>$a[id]</font></b>";
echo "<br>";
echo "name is <b><font color=#FF0000>$a[name]</font></b>";                    //突出顯示信息的字體顏色
echo "<br>";
}
?>
//經(jīng)常需要修改的地方已經(jīng)標注了!而且這個測試腳本在Memcache軟件中也有
第一次進行訪問

怎么搭建Memcache服務

第二次進行訪問(刷新之后)

怎么搭建Memcache服務


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI