


How to install and configure memcache under Linux
基本原理:
① 客户端第一次访问应用程序时,会到数据库(rdbms)中取出数据,返回给客户端;同时也将取出的数据保存到memcached中。
② 第二次访问时,因为数据已经缓存,就不用去数据库查询了,直接从memcached中取。
rdbms是文件型的数据库,最终还是以文件的形式保存在磁盘上;而memcached则不一样,它是key:value关系型的数据库,是保存在内存中的。内存的读写速度要比磁盘的读写速度快得多,前者是后者的10的6次方倍。
memcached是基于libevent的事件处理。libevent是个程序库,它将linux的epoll、bsd类操作系统的kqueue等事件处理功能封装成统一的接口。即使对服务器的连接数增加,也能发挥o(1)的性能。 memcached使用这个libevent库,因此能在linux、bsd、solaris等操作系统上发挥其高性能。关于事件处理这里就不再详细介绍,可以参考dan kegel的the c10k problem。
编译安装memcached
1、由于memcached是基于libevent的,因此需要安装libevent,libevent-devel
# yum install libevent libevent-devel -y
2、下载并解压memcached-1.4.6.tar.gz
memcached官方网站是:
# tar -xvzf memcached-1.4.6.tar.gz
3、编译安装memcached-1.4.6
# cd memcached-1.4.6
# ./configure --prefix=/etc/memcached
# make
# make install
4、配置环境变量(这一步可忽略...)
进入用户宿主目录,编辑.bash_profile,为系统环境变量ld_library_path增加新的目录,需要增加的内容如下:
# vi .bash_profile
memcached_home=/etc/memcached
export ld_library_path=$ld_library_path:$memcached_home/lib
刷新用户环境变量:# source .bash_profile
5、编写memcached服务启停脚本
# cd /etc/init.d
vi memcached,脚本内容如下:
#!/bin/sh # # startup script for the server of memcached # # processname: memcached # pidfile: /etc/memcached/memcached.pid # logfile: /etc/memcached/memcached_log.txt # memcached_home: /etc/memcached # chkconfig: 35 21 79 # description: start and stop memcached service # source function library . /etc/rc.d/init.d/functions retval=0 prog="memcached" basedir=/etc/memcached cmd=${basedir}/bin/memcached pidfile="$basedir/${prog}.pid" #logfile="$basedir/memcached_log.txt" ipaddr="192.168.1.200" # 绑定侦听的ip地址 port="11211" # 服务端口 username="root" # 运行程序的用户身份 max_memory=64 # default: 64m | 最大使用内存 max_simul_conn=1024 # default: 1024 | 最大同时连接数 #maxcon=51200 #growth_factor=1.3 # default: 1.25 | 块大小增长因子 #thread_num=6 # default: 4 #verbose="-vv" # 查看详细启动信息 #bind_protocol=binary # ascii, binary, or auto (default) start() { echo -n $"starting service: $prog" $cmd -d -m $max_memory -u $username -l $ipaddr -p $port -c $max_simul_conn -p $pidfile retval=$? echo [ $retval -eq 0 ] && touch /var/lock/subsys/$prog } stop() { echo -n $"stopping service: $prog " run_user=`whoami` pidlist=`ps -ef | grep $run_user | grep memcached | grep -v grep | awk '{print($2)}'` for pid in $pidlist do # echo "pid=$pid" kill -9 $pid if [ $? -ne 0 ]; then return 1 fi done retval=$? echo [ $retval -eq 0 ] && rm -f /var/lock/subsys/$prog } # see how we were called. case "$1" in start) start ;; stop) stop ;; #reload) # reload # ;; restart) stop start ;; #condrestart) # if [ -f /var/lock/subsys/$prog ]; then # stop # start # fi # ;; status) status memcached ;; *) echo "usage: $0 {start|stop|restart|status}" exit 1 esac exit $retval
6、赋予执行权限
#chmod +x memcached
7、设置memcached随系统启动
# chkconfig --add memcached
# chkconfig --level 35 memcached on
启动memcached
# service memcached start
//启动的时候实际上是调用了下面的这个命令,以守护进程的方式来启动memcached
/etc/memcached/bin/memcached -d -m 64 -u root -l 192.168.1.201 \
-p 11211 -c 1024 -p /etc/memcached/memcached.pid
查看memcached是否启动
# ps -ef | grep memcached
安装memcache的php扩展
1.在 选择相应想要下载的memcache版本。
2.安装php的memcache扩展
tar vxzf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make
make install
3.上述安装完后会有类似这样的提示:
installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
4.把php.ini中的extension_dir = “./”修改为
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”
5.添加一行来载入memcache扩展:extension=memcache.so
接下来重启php就可以了,可以通过phpinfo测试页面查看
The above is the detailed content of How to install and configure memcache under Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
