search
HomeOperation and MaintenanceLinux Operation and MaintenanceHow to install and configure memcache under Linux

How to install and configure memcache under Linux

May 18, 2023 am 11:28 AM
linuxmemcache

基本原理:

① 客户端第一次访问应用程序时,会到数据库(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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
What is Maintenance Mode in Linux? ExplainedWhat is Maintenance Mode in Linux? ExplainedApr 22, 2025 am 12:06 AM

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

Linux: A Deep Dive into Its Fundamental PartsLinux: A Deep Dive into Its Fundamental PartsApr 21, 2025 am 12:03 AM

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

Linux Architecture: Unveiling the 5 Basic ComponentsLinux Architecture: Unveiling the 5 Basic ComponentsApr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux Operations: Utilizing the Maintenance ModeLinux Operations: Utilizing the Maintenance ModeApr 19, 2025 am 12:08 AM

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

Linux: How to Enter Recovery Mode (and Maintenance)Linux: How to Enter Recovery Mode (and Maintenance)Apr 18, 2025 am 12:05 AM

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software