LAMP+memcache +(mysql)搭建

LAMP+memcache +(mysql)搭建:

一、基础环境配置
1、环境规划:
pc1 192.168.224.141 : mysql
pc2 192.168.224.142 : web
pc3 192.168.224.143 : memcache
2、时间同步:

yum -y install ntp ntpdate		//安装ntpdate工具
ntpdate cn.pool.ntp.org		//设置系统时间与网络时间同步
hwclock --systohc		//将系统时间写入硬件时间

3、关闭防火墙:

systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 
setenforce 0

4、卸载Mariadb:

rpm -e mariadb-libs postfix

二、web服务器配置
1、安装软件,启动服务:

[root@pc2 ~]# wget https://downloads.mysql.com/archives/get/file/mysql-community-libs-compat- 8.0.16-2.el7.x86_64.rpm
[root@pc2 ~]# yum localinstall mysql-community-client-8.0.16-2.el7.x86_64.rpm mysql-community-server-8.0.16-2.el7.x86_64.rpm mysql-community-libs-8.0.16-2.el7.x86_64.rpm mysql-community-common-8.0.16-2.el7.x86_64.rpm mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm
[root@pc2 ~]# yum install http php php-gb php-mysql php-memcache
[root@pc2 ~]# systemctl restart httpd
[root@pc2 ~]# systemctl enable httpd
[root@pc2 ~]# systemctl restart mysql
[root@pc2 ~]# systemctl enable mysql

2、在服务端创建用户:

mysql> create user 'memcache'@'%' identified by 'ABC123.com';
mysql> ALTER USER 'memcache'@'%' IDENTIFIED WITH mysql_native_password BY 'ABC123.com';
mysql> flush privileges;

3、测试http功能:

[root@pc2 ~]# vim /var/www/html/index.html
[root@pc2 ~]# cat /var/www/html/index.html
this is a thml...

LAMP+memcache +(mysql)搭建_第1张图片
4、测试PHP链接功能:

vim /var/www/html/index.php 
cat /var/www/html/index.php 

LAMP+memcache +(mysql)搭建_第2张图片
5、测试mysql:

vim /var/www/html/mysql.php 
cat /var/www/html/mysql.php 
Success!!";
else echo "Fail!!";
mysql_close();
?>

LAMP+memcache +(mysql)搭建_第3张图片
三、memcache服务配置
1、安装:
(1)libevent 稳定版
wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz

tar zxvf libevent-1.4.14b-stable.tar.gz yum install make gcc gcc-c++
cd libevent-1 4 14b-stable
./configure --prefix=/usr/local/libevent/ 
make
make install

(2)memcached 稳定版
wget https://memcached.org/files/memcached-1.5.16.tar.gz

tar -zxvf memcached-1.4.5.tar.gz
cd memcached-1.4.5
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/ 
make
make install

2、启动:

[root@pc3 init.d]# /usr/local/memcached/bin/memcached -d -l 127.0.0.1 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid
[root@pc3 init.d]# ps -ef |grep memcached
root      11018      1  0 17:53 ?        00:00:00 /usr/local/memcached/bin/memcached -d -l 127.0.0.1 -p 11211 -u root -m 64 -c 1024 -p /var/run/memcached.
pid
root      11033  10997  0 17:55 pts/1    00:00:00 grep --color=auto memcached

3、链接测试:

[root@pc3 init.d]# telnet 127.0.0.1 11211 Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
...

STAT pid 17143 进程id
STAT uptime 6177628 总的运行时间,秒数

4、测试web和memcache的连通性:

[root@pc3 ~]# /usr/local/memcached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid
[root@pc3 ~]# ps -ef |grep memcached
root 16629 1 0 02:38 ? 00:00:00
/usr/local/memcached/bin/memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024
-P /var/run/memcached.pid
root 16640 16561 0 02:38 pts/1 00:00:00 grep --color=auto memcached

5、代码测试:

connect('192.168.224.143', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."
"; $tmp_object = new stdClass; $tmp_object->str_attr = 'test'; $tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server"); echo "Store data in the cache (data will expire in 10 seconds)
"; $get_result = $memcache->get('key'); echo "Data from the cache:
"; var_dump($get_result); ?>

6、配置session:

vim /etc/php.ini
session.save_handler = memcache
session.save_path = "tcp://192.168.224.142:11211?
persistent=1&weight=1&timeout=1&retry_interval=15"

session.save_handler:设置session的储存方式为memcache。默认以文件方式存取session数据,如果想要使用自定义的处理来存取session数据,比如memcache方式则修为session.save_handler =
memcache
session.save_path:设置session储存的位置,多台memcache用逗号隔开
memcache实现session共享也可以在某个一个应用中设置:
ini_set(“session.save_handler”, “memcache”);
ini_set(“session.save_path”, “tcp://192.168.0.9:11211”);
测试memcache的可用性:

";
echo "now_time:".time()."
"; echo "session_id:".session_id()."
"; ?>

7、创建测试数据库:

create database testab1;
use testab1;
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;
insert into test1(name) values ('tom1'),('tom2'),('tom3'),('tom4'),('tom5');
select * from test1;
grant select on testab1.* to user@'%' identified by '123456';

8、测试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.224.142","user","123456");
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 ;
} e
lse{
$f = 'memcache';
$data_mem=$memcache->get($key);
$data = unserialize($data_mem);
} e
cho $f;
echo "
"; echo "$key"; echo "
"; //print_r($data); foreach($data as $a) { e cho "number is $a[id]"; echo "
"; echo "name is $a[name]"; echo "
"; } ?>

如果出现mysql表示memcached中没有内容,需要memcached从数据库中取得。
再刷新页面,如果有memcache标志表示这次的数据是从memcached中取得的。
memcached有个缓存时间默认是1分钟,过了一分钟后,memcached需要重新从数据库中取得数据。

你可能感兴趣的:(LAMP+memcache +(mysql)搭建)