lamp+memcache缓存服务搭建


请注意!这里使用的是mysql8.0.16
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190728182849337.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjcxNDE1,size_16,color_FFFFFF,t_70)`192.168.42.186 : memcache
192.168.42.187 : web
192.168.42.181 :mysql`

同步时间:

yum -y install ntp ntpdate
ntpdate cn.pool.ntp.org
hwclock --systohc

关闭防火墙,卸载原有的低版本mysql

```
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
rpm -e mariadb-libs postfix
```
2.3.2 web服务器配置

在192.168.42.145
安装软件,启动服务

wget https://downloads.mysql.com/archives/get/file/mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm

```
yum localinstall mysql-community-client-8.0.16-2.el7.x86_64.rpm mysql-communityserver-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
```

```
yum install http php php-gb php-mysql php-memcache

systemctl restart httpd
systemctl enable httpd
systemctl restart mysql
systemctl enable mysql
```
在服务端创建用户:

```
mysql> create user 'memcache'@'%' identified by 'Cloudbu@123';
mysql> ALTER USER 'memcache'@'%' IDENTIFIED WITH mysql_native_password BY
'Cloudbu@123';
mysql> flush privileges;
```
测试http功能。
。。。。。。。

测试PHP链接功能

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

```
在windows浏览器访问php文件


测试mysql
```
Success!!";
else echo "Fail!!";
mysql_close();
?>
```
出现Success!!

2. memcache 服务配置

## 在 192.168.42.186 : memcache

软件版本
libevent 稳定版
wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
memcached 稳定版
wget https://memcached.org/files/memcached-1.5.16.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
```

Memcached安装

```
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
```

启动:

```
[root@localhost 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@localhost init.d]# ps -ef |grep memcached
root 16227 1 0 00:56 ? 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 16238 1201 0 00:57 pts/0 00:00:00 grep --color=auto memcached
```

链接测试:

```
[root@localhost init.d]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
...
```
测试web和memcache的连通性;
修改客户端监听所有地址

```
[root@localhost ~]# /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@localhost ~]# 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
```
代码测试

## 在web端:


```
vim  memcache

connect('192.168.42.186', 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); ?> ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190728184630375.png) 配置session: ``` vim /etc/php.ini session.save_handler = memcache session.save_path = "tcp://192.168.42.145: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()."
"; ?> ``` 创建测试数据库 在mysql中创建表 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'; 测试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.42.156","user","123456"); mysql_select_db(testab1); $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 "
"; echo "$key"; echo "
"; //print_r($data); foreach($data as $a) { echo "number is $a[id]"; echo "
"; echo "name is $a[name]"; echo "
"; } ?> ``` 以上步骤做完之后重启服务,即可在windows浏览器通过web访问mysql, 网页显示的第一行就是memcache和mysql交替,最好是访问一下停一下再访问, ~~~~由于实验是几天前做完的,现在才想起写博客,所以有的地方会缺少结果图片,但大部分都完整~~

你可能感兴趣的:(lamp+memcache缓存服务搭建)