Home Backend Development PHP Tutorial 解析linux下安装memcacheq(mcq)全过程笔记_php技巧

解析linux下安装memcacheq(mcq)全过程笔记_php技巧

May 17, 2016 am 08:58 AM
linux Install

memcacheQ是一个单纯的分布式消息队列服务。
一,MEMCACHEQ的应用背景
Web应用中为什
么会需要消息队列?主要原因是由于在高并发环境下,由于来不及同步处理,请求往往会发生堵塞,比如说,大量的insert,update之类的请求同时到达mysql,直接导致无数的行锁表锁,甚至最后请求会堆积过多,从而触发too manyconnections错误。通过使用消息队列,我们可以异步处理请求,从而缓解系统的压力。在Web2.0的时代,高并发的情况越来越常见,从而使消息队列有成为居家必备的趋势,相应的也涌现出了很多实现方案,像Twitter以前就使用RabbitMQ实现消息队列服务,现在又转而使用Kestrel来实现消息队列服务,此外还有很多其他的选择,比如说:ActiveMQ,ZeroMQ等。

上述消息队列的软件中,大多为了实现AMQP,STOMP,XMPP之类的协议,变得极其重量级,但在很多Web应用中的实际情况是:我们只是想找到一个缓解高并发请求的解决方案,不需要杂七杂八的功能,一个轻量级的消息队列实现方式才是我们真正需要的。

二,MEMCACHEQ的特性
1 简单易用
2 处理速度快
3 多条队列
4 并发性能好
5 与memcache的协议兼容。这就意味着只要装了memcache的extension就可以了,不需要额外的插件。

三,安装
MEMCACHEQ依赖于libevent和BerkleyDB。
BerkleyDB用于持久化存储队列的数据。 这样在MEMCACHEQ崩溃或者服务器挂掉的时候,
不至于造成数据的丢失。这一点很重要,很重要。
它的安装依赖于BerkeleyDB 和 libevent,所以要先安装这BerkeleyDB和libevent:
其中libevent如果你安装过memcached就已经安装了,如果不确定,就检查一下吧

1. 先检查libevent, libevent-devel是否已经安装:  rpm -qa|grep libevent 输出中必须包含libevent, libevent-deve, 如果缺失,使用以下命令安装:
yum install libevent yum
install libevent-devel
注意事项:libevent, libevent-devel优先使用yum安装源,光盘镜像中的rpm包安装,这样稳定性和兼容性可得到保证,网上流传的使用源码安装libevent的方法会有问题,因为很可能系统已经安装libevent, 再使用源码安装, 必然导致冲突,造成意外问题,所以一定要使用上述命令检查系统是否已经安装相应的库

2. 安装BerkleyDB
1.tar zxvf bdb-5.3.3.tar.gz
2.cd db-5.3.3/
#需要进入特定操作系统编译环境,更常规软件的编译有些区别
3.cd build_unix/
4. ../dist/configure --prefix=/usr/local/berkeleydb
#如果没有指定特殊安装路径,编译完成,需要将Berkeley Db运行库的路径添加到系统配置里面
echo "/usr/local/BerkeleyDB.5.3/lib/" >> /etc/ld.so.conf
#重载系统Ld运行库
ldconfig
5. make & make install
记得改/etc/ld.so.conf文件,添加/usr/local/BerkeleyDB.5.3/lib啊,不然后面的mcq会安装错误。
而BerkeleyDB就要去下载了
点击下载Berkeley DB 5.3.21.rar
下面安装memcacheq,
先下载一个memcacheq-0.2.0.rar
解压,进目录
./configure –with-bdb=/usr/local/BerkeleyDB.5.1 –with-libevent=/usr/local/lib –enable-threads
make
make install
关键是红色字体那一步,一定输入正确,不然make不通过,无法安装

下面是启动
memcacheq -d -r -u root -p21201 -H /data/memcacheq -N -v -L 1024 -B 1024 > /data/mq_error.log 2>&1
这里不推荐使用root用户,有些帖子里说不可以,我这里测试是可以的,不过可能会不安全。
1 下面是启动时候的参数
使用memcacheq -h 的命令来查看命令行选项
2 这个是正确的启动memcacheq:memcacheq -d -uroot -r -p11212 -H /home/wwwroot/mcq -N -R -v -L 1024 -B 1024 > /home/wwwlogs/mq_error.log 2 > &1
3 这个不知道为什么就不行/usr/local/memcacheq/bin/memcacheq -d -l 127.0.0.1 -A 8192 -H /data/memcacheq -B 65535 -N -R -u root
-p TCP监听端口(default: 22201)
 -U UDP监听端口(default: 0, off)
 -s unix socket路径(不支持网络)
 -a unix socket访问掩码(default 0700)
 -l 监听网卡
 -d 守护进程
 -r 最大化核心文件限制
 -u 以用户身份运行(only when run as root)
 -c 最大并发连接数(default is 1024)
 -v 详细输出 (print errors/warnings while in event loop)
 -vv 更详细的输出 (also print client commands/reponses)
 -i 打印许可证信息
 -P PID文件
 -t 线程数(default 4)
 --------------------BerkeleyDB Options-------------------------------
 -m BerkeleyDB内存缓存大小, default is 64MB
 -A 底层页面大小, default is 4096, (512B ~ 64KB, power-of-two)
 -H

数据库家目录, default is '/data1/memcacheq'
 -L 日志缓冲区大小, default is 32KB
 -C 多少秒checkpoint一次, 0 for disable, default is 5 minutes
 -T 多少秒memp_trickle一次, 0 for disable, default is 30 seconds
 -S 多少秒queue stats dump一次, 0 for disable, default is 30 seconds
 -e 达到缓存百分之多少需要刷新, default is 60%
 -E 一个单一的DB文件有多少页, default is 16*1024, 0 for disable
 -B 指定消息体的长度,单位字节, default is 1024
 -D 多少毫秒做一次死锁检测(deadlock detecting), 0 for disable, default is 100ms
 -N 开启DB_TXN_NOSYNC获得巨大的性能改善, default is off
 -R 自动删除不再需要的日志文件, default is off
测试

三、测试
1.telnet 10.218.31.121 22201
2.stats
2.stats queue
3.set q4  0 0 5
4 hello
5 get q4
6 stats queue
7 delete q4
如果set的时候补成功not_STORED的话,检查一下你的启动命令吧,参数没设置好,如果你是新手,干翠多看几个帖子,多尝试启动命令,换换参数,就行了

四,使用
使用以上命令启动mq后,(注意上面的-B参数表示messag的body长度不能超过1024 bytes),使用mq时只需要用到两个命令:set和get:
set 0 \r\n
\r\n
STORED\r\n
get \r\n
VALUE \r\n
\r\n
END\r\n
可以看到,和memcache协议基本一致,只是把key name换成queue name,而且在set的命令中,忽略了expire_time的参数。毕竟mq的数据存储是存在berkeleyDB中,做了持久化存储,没有内存的过期时间。
当使用set命令时,就向指定的消息队列中写入了一条新消息,也就是向BerkeleyDB中新insert了一条数据,当使用get命令时,就从 指定队列中取出一条新消息,也就是向BerkeleyDB中delete了一条数据。当使用stats查看一个指定队列时,可以看到这个队列一共接收了多 少消息,其中被取出了多少条。
示例:

复制代码 代码如下:

fengbo@onlinegame-10-121:~$ telnet 127.0.0.1 22202
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]'.
set q4 0 0 5
hello
STORED
set q4 0 0 5
world
STORED
stats queue
STAT q4 2/0
END
get q4
VALUE q4 0 5
hello
END
stats queue
STAT q4 2/1
END

上面执行了两次set的命令,使用stats queue查看时,可以看到q4的队列中共有消息2条,已取出0条;当使用get取出第一条后,再此使用stats queue查看,q4中消息有2条,其中已取出1条。
PHP测试:
复制代码 代码如下:

session_start();
$memcache_obj = new Memcache;
$memcache_obj->connect(‘127.0.0.1′, 11212) or die (“error”);
memcache_set($memcache_obj, ‘k',10, 0, 0);
echo “queue”.memcache_get($memcache_obj, ‘k');
memcache_close($memcache_obj);

注释:
这个时候会出现这样的问题
memcacheq: error while loading shared libraries: libdb-5.0.so: cannot open shared object file: No such file or directory
解决办法:在/usr/lib 下建个 libdb-5.0.so 软链就OK啦
ln -s /usr/local/BerkeleyDB.5.0/lib/libdb-5.0.so /usr/lib/

五,关闭memcacheQ
使用ps命令查查memcacheQ的进程:ps -ef|grep wuf,然后直接将进程kill掉.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Unable to log in to mysql as root Unable to log in to mysql as root Apr 08, 2025 pm 04:54 PM

The main reasons why you cannot log in to MySQL as root are permission problems, configuration file errors, password inconsistent, socket file problems, or firewall interception. The solution includes: check whether the bind-address parameter in the configuration file is configured correctly. Check whether the root user permissions have been modified or deleted and reset. Verify that the password is accurate, including case and special characters. Check socket file permission settings and paths. Check that the firewall blocks connections to the MySQL server.

C language conditional compilation: a detailed guide for beginners to practical applications C language conditional compilation: a detailed guide for beginners to practical applications Apr 04, 2025 am 10:48 AM

C language conditional compilation is a mechanism for selectively compiling code blocks based on compile-time conditions. The introductory methods include: using #if and #else directives to select code blocks based on conditions. Commonly used conditional expressions include STDC, _WIN32 and linux. Practical case: Print different messages according to the operating system. Use different data types according to the number of digits of the system. Different header files are supported according to the compiler. Conditional compilation enhances the portability and flexibility of the code, making it adaptable to compiler, operating system, and CPU architecture changes.

What are the 5 basic components of Linux? What are the 5 basic components of Linux? Apr 06, 2025 am 12:05 AM

The five basic components of Linux are: 1. The kernel, managing hardware resources; 2. The system library, providing functions and services; 3. Shell, the interface for users to interact with the system; 4. The file system, storing and organizing data; 5. Applications, using system resources to implement functions.

How to solve mysql cannot be started How to solve mysql cannot be started Apr 08, 2025 pm 02:21 PM

There are many reasons why MySQL startup fails, and it can be diagnosed by checking the error log. Common causes include port conflicts (check port occupancy and modify configuration), permission issues (check service running user permissions), configuration file errors (check parameter settings), data directory corruption (restore data or rebuild table space), InnoDB table space issues (check ibdata1 files), plug-in loading failure (check error log). When solving problems, you should analyze them based on the error log, find the root cause of the problem, and develop the habit of backing up data regularly to prevent and solve problems.

Can mysql run on android Can mysql run on android Apr 08, 2025 pm 05:03 PM

MySQL cannot run directly on Android, but it can be implemented indirectly by using the following methods: using the lightweight database SQLite, which is built on the Android system, does not require a separate server, and has a small resource usage, which is very suitable for mobile device applications. Remotely connect to the MySQL server and connect to the MySQL database on the remote server through the network for data reading and writing, but there are disadvantages such as strong network dependencies, security issues and server costs.

Solutions to the errors reported by MySQL on a specific system version Solutions to the errors reported by MySQL on a specific system version Apr 08, 2025 am 11:54 AM

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

MySQL can't be installed after downloading MySQL can't be installed after downloading Apr 08, 2025 am 11:24 AM

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

How to solve the problem of missing dependencies when installing MySQL How to solve the problem of missing dependencies when installing MySQL Apr 08, 2025 pm 12:00 PM

MySQL installation failure is usually caused by the lack of dependencies. Solution: 1. Use system package manager (such as Linux apt, yum or dnf, Windows VisualC Redistributable) to install the missing dependency libraries, such as sudoaptinstalllibmysqlclient-dev; 2. Carefully check the error information and solve complex dependencies one by one; 3. Ensure that the package manager source is configured correctly and can access the network; 4. For Windows, download and install the necessary runtime libraries. Developing the habit of reading official documents and making good use of search engines can effectively solve problems.

See all articles