memcached高缓存搭建

搭建服务端和客户端,让服务端去链接客户端

两台服务器:

web客户端:192.168.247.161

服务端:192.168.247.160

[root@localhost ~]# hostnamectl set-hostname client
[root@localhost ~]# su
[root@client ~]# 
[root@nginx ~]# hostnamectl set-hostname server
[root@nginx ~]# su
[root@server ~]# 

先搭建服务端

libevent 事件通知库

[root@server ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@server ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@server ~]# cd /abc
[root@server abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
[root@server abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[root@server abc]# cd /opt
[root@server opt]# ls
data  libevent-2.1.8-stable  memcached-1.5.6  nginx-1.12.2  rh
[root@server opt]# cd libevent-2.1.8-stable/
[root@server libevent-2.1.8-stable]# yum install gcc gcc-c++ make -y
[root@server libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@server libevent-2.1.8-stable]# make && make install

libevent安装完毕,接下来安装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
[root@server memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin/

-d 指定守护进程

-m 32m 指定缓存32兆

-p 11211 指定端口

-u root 指定用户管理

[root@server memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
[root@server memcached-1.5.6]# netstat -natp | grep memc
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      115996/memcached    
tcp6       0      0 :::11211                :::*                    LISTEN      115996/memcached    
[root@server memcached-1.5.6]# systemctl stop firewalld
[root@server memcached-1.5.6]# setenforce 0

可以先在本地连接这个缓存数据库,看看是否可以连接上

[root@server memcached-1.5.6]# yum install telnet -y
[root@server memcached-1.5.6]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

add 添加

键值名称 username

0 代表不设置序列号

0 无时间要求,不设置过期时间

7 字节长度,接下来如果输入的字节长度不符合要求,就会报错

add username 0 0 7
1234567
STORED
add users 0 0 7
123


STORED

ERROR

查看输入的键值

get username
VALUE username 0 7
1234567
END

value 后面的1 是更新因子,如果更新一次,就会自加一

gets username
VALUE username 0 7 1
1234567
END
set username 0 0 8
12345678
STORED
gets username
VALUE username 0 8 3
12345678
END

replace 是更新的意思,如果当前的键值名称不存在,就会更新失败

set 也是更新的意思,如果当前的键值名称不存在,会新建一个键值

replace school 0 0 2


NOT_STORED
get school
END
replace username 0 0 9
123456789
STORED
gets username
VALUE username 0 9 4
123456789
END

cas 更新,依据更新因子进行更新,相同则更新

cas username 0 0 7 4
1234567
STORED
gets username
VALUE username 0 7 5
1234567
END
cas username 0 0 8 2
12345678
EXISTS
gets username
VALUE username 0 7 5
1234567
END

键值后追加数据

append username 0 0 4
appe
STORED
get username
VALUE username 0 11
1234567appe
END

键值前追加数据

prepend username 0 0 5
prepe
STORED
get username
VALUE username 0 16
prepe1234567appe
END

删除键值

delete username
DELETED
get username
END

stats 显示状态信息

清除所有缓存数据 flush_all

flush_all
OK
get users
END

退出,quit

quit
Connection closed by foreign host.
[root@server memcached-1.5.6]# 

接下来安装客户端,安装客户端需要先安装LAMP环境,下面是LAMP的详细博客

https://blog.csdn.net/Lfwthotpt/article/details/103472655

[root@client ~]# mkdir /abc
[root@client ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@client ~]# cd /abc
[root@client abc]# cd LAMP-C7/
[root@client LAMP-C7]# ls
apr-1.6.2.tar.gz       Discuz_X2.5_SC_UTF8.zip  LAMP-php5.6.txt      php-5.6.11.tar.bz2
apr-util-1.6.0.tar.gz  httpd-2.4.29.tar.bz2     mysql-5.6.26.tar.gz
[root@client LAMP-C7]# tar xvzf apr-util-1.6.0.tar.gz -C /opt
[root@client LAMP-C7]# tar xvzf apr-1.6.2.tar.gz -C /opt
[root@client LAMP-C7]#  tar xvjf httpd-2.4.29.tar.bz2 -C /opt
[root@client LAMP-C7]# cd /opt
[root@client opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@client opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@client opt]# cd httpd-2.4.29/
[root@client httpd-2.4.29]# 
[root@client httpd-2.4.29]# yum install gcc gcc-c++ pcre-devel pcre expat-devel.x86_64 make -y
[root@client httpd-2.4.29]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[root@client httpd-2.4.29]# make && make install
[root@client httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@client httpd-2.4.29]# vim /etc/init.d/httpd
# chkconfig: 35 85 21   
# description: Apache is a World Wide Web server
[root@client httpd-2.4.29]# chkconfig --add httpd
[root@client httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@client httpd-2.4.29]# vim /etc/httpd.conf 
 51 Listen 192.168.247.161:80
 52 #Listen 80
202 ServerName www.gsy.com:80
[root@client httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@client httpd-2.4.29]# httpd -t
Syntax OK
[root@client httpd-2.4.29]# service httpd start 
[root@client httpd-2.4.29]# netstat -natp | grep 80
tcp        0      0 192.168.247.206:80      0.0.0.0:*               LISTEN      64446/httpd         
[root@client httpd-2.4.29]# systemctl stop firewalld.service 
[root@client httpd-2.4.29]# setenforce 0
[root@client httpd-2.4.29]# cd ..
[root@client opt]# cd /abc/LAMP-C7/
[root@client LAMP-C7]# ls
apr-1.6.2.tar.gz       Discuz_X2.5_SC_UTF8.zip  LAMP-php5.6.txt      php-5.6.11.tar.bz2
apr-util-1.6.0.tar.gz  httpd-2.4.29.tar.bz2     mysql-5.6.26.tar.gz
[root@client LAMP-C7]# tar zxvf mysql-5.6.26.tar.gz -C /opt
[root@client LAMP-C7]# yum install -y ncurses-devel autoconf cmake
[root@client LAMP-C7]# cd /opt/mysql-5.6.26/
[root@client LAMP-C7]# 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@client mysql-5.6.26]# make
[root@client mysql-5.6.26]# make install
[root@client mysql-5.6.26]# cp support-files/my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
[root@client mysql-5.6.26]# cp support-files/mysql.server /etc/init.d/mysqld
[root@client mysql-5.6.26]# chmod 755 /etc/init.d/mysqld 
[root@client mysql-5.6.26]# chkconfig --add /etc/init.d/mysqld
[root@client mysql-5.6.26]# chkconfig --level 35 mysqld on
[root@client mysql-5.6.26]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@client mysql-5.6.26]# source /etc/profile
[root@client mysql-5.6.26]# echo $PATH
/usr/java/jdk1.8.0_201-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@client mysql-5.6.26]# useradd -s /sbin/nologin mysql
[root@client mysql-5.6.26]# chown -R mysql.mysql /usr/local/mysql/
[root@client 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@client mysql-5.6.26]# ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
[root@client mysql-5.6.26]# vim /etc/init.d/mysqld 
 46 basedir=/usr/local/mysql
 47 datadir=/home/mysql
[root@client mysql-5.6.26]# service mysqld start
Starting MySQL. SUCCESS! 
[root@client mysql-5.6.26]# netstat -natp | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      89546/mysqld        
[root@client mysql-5.6.26]# mysqladmin -uroot -p password "abc123"
Enter password: 
Warning: Using a password on the command line interface can be insecure.
[root@client mysql-5.6.26]# yum -y install gd libpng libpng-devel pcre pcre-devel libxml2-devel libjpeg-devel
[root@client mysql-5.6.26]# cd /abc/LAMP-C7/
[root@client LAMP-C7]# ls
apr-1.6.2.tar.gz       Discuz_X2.5_SC_UTF8.zip  LAMP-php5.6.txt      php-5.6.11.tar.bz2
apr-util-1.6.0.tar.gz  httpd-2.4.29.tar.bz2     mysql-5.6.26.tar.gz
[root@client LAMP-C7]# tar jvxf php-5.6.11.tar.bz2 -C /opt
[root@client LAMP-C7]# cd /opt/php-5.6.11/
[root@client 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@client php-5.6.11]# make && make install
[root@client php-5.6.11]# cp php.ini-development /usr/local/php5/php.ini
[root@client php-5.6.11]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[root@client php-5.6.11]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
[root@client php-5.6.11]# vim /etc/httpd.conf 
393     AddType application/x-gzip .gz .tgz
394     AddType application/x-httpd-php .php
395     AddType application/x-httpd-php-source .phps
[root@client php-5.6.11]# vim /etc/httpd.conf 
256     DirectoryIndex index.html index.php
[root@client php-5.6.11]# cd /usr/local/httpd/
[root@client httpd]# ls
bin    cgi-bin  error   icons    lib   man     modules
build  conf     htdocs  include  logs  manual
[root@client httpd]# cd htdocs/
[root@client htdocs]# ls
index.html
[root@client htdocs]# vim index.html 
[root@client htdocs]# mv index.html index.php
[root@client htdocs]# cat index.php 
<?php
phpinfo();
?>
[root@client htdocs]# service httpd stop
[root@client htdocs]# service httpd start
[root@client htdocs]# systemctl stop firewalld
[root@client htdocs]# setenforce 0
[root@client htdocs]# service mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS! 

测试

[root@client htdocs]# vim index.php 
<?php
$link=mysql_connect('192.168.247.161','skyuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "fail!!";
mysql_close();
?>
[root@client htdocs]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> create database sky;
Query OK, 1 row affected (0.01 sec)

mysql> grant all ON sky.* TO 'skyuser'@'%' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@client htdocs]# 
[root@client htdocs]# mysql -u skyuser -p 
Enter password: 
ERROR 1045 (28000): Access denied for user 'skyuser'@'localhost' (using password: YES)
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+---------------------------+
| query                     |
+---------------------------+
| User: 'root'@'%';         |
| User: 'skyuser'@'%';      |
| User: 'root'@'127.0.0.1'; |
| User: 'root'@'::1';       |
| User: ''@'client';        |
| User: 'root'@'client';    |
| User: ''@'localhost';     |
| User: 'root'@'localhost'; |
+---------------------------+
8 rows in set (0.00 sec)
[root@client htdocs]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@client htdocs]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> grant all on sky.* to 'skyuser'@'client' identified by 'admin123';            Query OK, 0 rows affected (0.01 sec)

mysql>  grant all on sky.* to 'skyuser'@'localhost' identified by 'admin123';        Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@client htdocs]# mysql -u skyuser -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.

mysql> exit
Bye

在这里插入图片描述

LAMP安装完毕,接下来安装memcacahe客户端

[root@client abc]# yum install autoconf -y
[root@client htdocs]# cd /abc
[root@client abc]# tar zxvf memcache-2.2.7.tgz -C /opt
[root@client abc]# cd /opt
[root@client opt]# ls
httpd-2.4.29  memcache-2.2.7  mysql-5.6.26  package.xml  php-5.6.11  rh
[root@client opt]# cd memcache-2.2.7/
[root@client memcache-2.2.7]# ls
config9.m4  example.php                 memcache.php        memcache_standard_hash.c
config.m4   memcache.c                  memcache_queue.c    php_memcache.h
config.w32  memcache_consistent_hash.c  memcache_queue.h    README
CREDITS     memcache.dsp                memcache_session.c
[root@client memcache-2.2.7]# 

没有configure脚本,使用php5

[root@client memcache-2.2.7]# /usr/local/php5/bin/phpize
[root@client memcache-2.2.7]# ls
acinclude.m4    config.sub    Makefile.global             memcache_standard_hash.c
aclocal.m4      configure     memcache.c                  missing
autom4te.cache  configure.in  memcache_consistent_hash.c  mkinstalldirs
build           config.w32    memcache.dsp                php_memcache.h
config9.m4      CREDITS       memcache.php                README
config.guess    example.php   memcache_queue.c            run-tests.php
config.h.in     install-sh    memcache_queue.h
config.m4       ltmain.sh     memcache_session.c
[root@client memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config
[root@client memcache-2.2.7]# make && make install
[root@client memcache-2.2.7]# vim /usr/local/php5/php.ini 
 736 ; extension_dir = "ext"
 737 extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"		#增加
 738 extension = memcache.so	#增加

以下是在web客户端去检测服务端是否可以连接

[root@client memcache-2.2.7]# cd /usr/local/httpd/htdocs/
[root@client htdocs]# ls
index.php
[root@client htdocs]# vim gsy.php
<?php
$memcache = new Memcache();
$memcache->connect('192.168.247.160',11211);
$memcache->set('key','Memcache test Successfull',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
[root@client htdocs]# service httpd restart

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值