操作系统:CentOS 7
安装依赖
[root@localhost ~]# yum install -y autoconf epel-release gcc gcc-c++ glibc-devel kernel-devel librabbitmq librabbitmq-devel m4 make ncurses-devel openssl-devel perl python-simplejson socat unixODBC unixODBC-devel xmlto
RabbitMQ是用Erlang编写的,所以需要安装Erlang运行环境
[root@localhost src]# tar -xf otp_src_25.0.4.tar.gz
[root@localhost src]# cd otp_src_25.0.4
[root@localhost otp_src_25.0.4]# ./configure --prefix=/program/erlang --enable-smp-support --enable-threads --enable-kernel-poll --enable-hipe --with-ssl --without-javac
[root@localhost otp_src_25.0.4]# make
[root@localhost otp_src_25.0.4]# make install
[root@localhost otp_src_25.0.4]# /program/erlang/bin/erl -version # 检查Erlang安装是否成功
Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 13.0.4
[root@localhost otp_src_25.0.4]# vim /etc/profile # 把Erlang的bin目录加入系统环境变量(可选操作)
export PATH=/program/erlang/bin:$PATH
[root@localhost otp_src_25.0.4]# shutdown -r now
[root@localhost ~]# erl -version
Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 13.0.4
[root@localhost ~]#
安装RabbitMQ服务器(rabbitmq-server-generic-unix-版本号.tar.xz是免安装版,直接解压即可用)
[root@localhost src]# xz -d rabbitmq-server-generic-unix-3.10.7.tar.xz
[root@localhost src]# tar -xf rabbitmq-server-generic-unix-3.10.7.tar
[root@localhost src]# cp -rf rabbitmq_server-3.10.7/ /program/
[root@localhost src]# mv /program/rabbitmq_server-3.10.7/ /program/rabbitmq/
[root@localhost src]# vim /etc/profile # 把RabbitMQ的sbin目录加入系统环境变量(可选操作)
export PATH=/program/erlang/bin:/program/rabbitmq/sbin:$PATH
[root@localhost src]# shutdown -r now
启动和停止RabbitMQ服务
[root@localhost ~]# rabbitmq-server # 该方式不能关闭终端,否则RabbitMQ服务也会停止
[root@localhost ~]# rabbitmq-server -detached # 在后台启动RabbitMQ服务,该方式可关闭终端【推荐使用】
[root@localhost ~]# rabbitmq-server stop # 停止RabbitMQ服务
实现开机自启动RabbitMQ服务(注:以下操作仅实现开机自启动RabbitMQ服务,并不启动消费端,如何实现开机自动启动RabbitMQ消费端可『
点击这里』。)
创建service文件
[root@localhost ~]# vim /usr/lib/systemd/system/rabbitmq.service
[Unit]
Description=RabbitMQ
After=syslog.target network.target
[Service]
User=root
Group=root
WorkingDirectory=/program/rabbitmq
ExecStart=/program/rabbitmq/sbin/rabbitmq-server
ExecStop=/program/rabbitmq/sbin/rabbitmqctl stop
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod +x /usr/lib/systemd/system/rabbitmq.service
[root@localhost ~]#
解决“/program/rabbitmq/sbin/rabbitmq-server: 第 73 行:exec: erl: 未找到”和“/program/rabbitmq/sbin/rabbitmq-env: 第 180 行:exec: erl: 未找到”的问题
[root@localhost ~]# vim /program/rabbitmq/sbin/rabbitmq-server
#!/bin/sh
# vim:sw=4:et:
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
##
set -e
# 临时设置PATH变量的值,否则无法实现开机自启动(会报“exec: erl: 未找到”错误)
export PATH=$PATH:/program/erlang/bin
# Get default settings with user overrides for (RABBITMQ_)<var_name>
# Non-empty defaults should be set in rabbitmq-env
SCRIPTS_DIR=$(dirname "$0")
. "$SCRIPTS_DIR/rabbitmq-env"
[ "$NOTIFY_SOCKET" ] && RUNNING_UNDER_SYSTEMD=true
[root@localhost ~]# systemctl enable rabbitmq.service # 设置开机自启动
[root@localhost ~]#
常用管理命令
[root@localhost ~]# systemctl start rabbitmq.service # 启动服务
[root@localhost ~]# systemctl stop rabbitmq.service # 停止服务
[root@localhost ~]# systemctl restart rabbitmq.service # 重启服务
[root@localhost ~]# systemctl status rabbitmq.service # 查看运行状态
[root@localhost ~]# systemctl enable rabbitmq.service # 设置开机自启动
[root@localhost ~]# systemctl disable rabbitmq.service # 取消开机自启动
创建RabbitMQ管理员
[root@localhost ~]# rabbitmqctl add_user admin 88888888 # 账号/密码:admin/88888888
Adding user "admin" ...
Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more.
[root@localhost ~]# rabbitmqctl set_permissions -p / admin ".*" ".*" ".*" # 设置管理员权限
Setting permissions for user "admin" in vhost "/" ...
[root@localhost ~]# rabbitmqctl set_user_tags admin administrator # 设置管理员标签(即管理员角色)
Setting tags for user "admin" to [administrator] ...
[root@localhost ~]#
用户管理命令汇总
[root@localhost ~]# rabbitmqctl add_user 用户名 密码 # 创建用户
[root@localhost ~]# rabbitmqctl list_users # 查看用户
[root@localhost ~]# rabbitmqctl set_user_tags 用户名 角色名 # 设置管理员标签(即管理员角色)
[root@localhost ~]# rabbitmqctl set_permissions -p / 用户名 ".*" ".*" ".*" # 设置管理员权限
[root@localhost ~]# rabbitmqctl change_password 用户名 密码 # 修改密码
[root@localhost ~]#
rabbitmqctl delete_user 用户名 # 删除用户
启用RabbitMQ网页管理插件(提醒:必须先启动RabbitMQ服务,否则无法启用插件,生产环境下不建议开启网页管理插件)
[root@localhost ~]# rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
started 3 plugins.
[root@localhost ~]#
此时在浏览器打开“http://192.168.***.***:15672/”并输入RabbitMQ管理员账号密码即可对RabbitMQ进行管理。
RabbitMQ命令行管理工具没有提供清除所有队列的指令,只能先查出所有队列,再逐个删除
[root@localhost ~]# rabbitmqctl list_queues # 列出所有队列
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
name messages
amq.gen-ML7yvT6284ss3zkHFGEqmB 0
amq.gen-r9jAUQGK-aUde7yaOftXOH 0
[root@localhost ~]# rabbitmqctl delete_queue amq.gen-ML7yvT6284ss3zkHFGEqmB # 删除指定队列
可以使用grep和awk实现一条命令删除所有队列(删除所有名称以amq开头的队列)
[root@localhost ~]# rabbitmqctl list_queues | grep ^amq | awk '{print $1}' | xargs -n1 rabbitmqctl delete_queue
通过重置RabbitMQ实现清除所有队列(不推荐)
[root@localhost ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@localhost ...
[root@localhost ~]# rabbitmqctl reset
Resetting node rabbit@localhost ...
[root@localhost ~]# rabbitmqctl start_app
Starting node rabbit@localhost ...
[root@localhost ~]#
备注:该方式不仅会清除队列,还会清除所有配置,执行命令后需要重新创建RabbitMQ管理员,故不推荐使用该方式。
PHP如何操作RabbitMQ可看『
使用amqp扩展操作RabbitMQ』。