笔者在 CentOS Stream9 中,启动 RabbitMQ 服务失败,抛出如下错误:
2022-04-05 22:18:47.207955+08:00 [erro] <0.146.0> Error when reading /var/lib/rabbitmq/.erlang.cookie: eacces 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> supervisor: {local,net_sup} 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> errorContext: start_error 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> reason: {"Error when reading /var/lib/rabbitmq/.erlang.cookie: eacces", 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> [{auth,init_no_setcookie,0,[{file,"auth.erl"},{line,313}]}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {auth,init,1,[{file,"auth.erl"},{line,165}]}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {gen_server,init_it,2,[{file,"gen_server.erl"},{line,423}]}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {gen_server,init_it,6,[{file,"gen_server.erl"},{line,390}]}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {proc_lib,init_p_do_apply,3, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> [{file,"proc_lib.erl"},{line,226}]}]} 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> offender: [{pid,undefined}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {id,auth}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {mfargs,{auth,start_link,[]}}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {restart_type,permanent}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {significant,false}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {shutdown,2000}, 2022-04-05 22:18:47.220549+08:00 [erro] <0.144.0> {child_type,worker}] ... 022-04-05 22:18:47.243334+08:00 [erro] <0.130.0> BOOT FAILED 2022-04-05 22:18:47.243334+08:00 [erro] <0.130.0> =========== 2022-04-05 22:18:47.243334+08:00 [erro] <0.130.0> Exception during startup: 2022-04-05 22:18:47.243334+08:00 [erro] <0.130.0> 2022-04-05 22:18:47.243334+08:00 [erro] <0.130.0> error:{badmatch,{error,{{shutdown,{failed_to_start_child,auth,{"Error when reading /var/lib/rabbitmq/.erlang.cookie: eacces",[{auth,init_no_setcookie,0,[{file,"auth.erl"},{line,313}]},{auth,init,1,[{file,"auth.erl"},{line,165}]},{gen_server,init_it,2,[{file,"gen_server.erl"},{line,423}]},{gen_server,init_it,6,[{file,"gen_server.erl"},{line,390}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,226}]}]}}},{child,undefined,net_sup_dynamic,{erl_distribution,start_link,[#{clean_halt => false,name => rabbit_prelaunch_39184@localhost,name_domain => shortnames,net_tickintensity => 4,net_ticktime => 60,supervisor => net_sup_dynamic}]},permanent,false,1000,supervisor,[erl_distribution]}}}}
上面错误日志中,关键字“auth”和“Error when reading /var/lib/rabbitmq/.erlang.cookie: eacces”意为错误读取 .erlang.cookie 文件,是授权导致。
我们先看看 /var/lib/rabbitmq/.erlang.cookie 文件的权限:
[root@node3 rabbitmq]# ll -a total 872 drwxr-xr-x. 3 rabbitmq rabbitmq 90 Apr 5 22:11 . drwxr-xr-x. 38 root root 4096 Apr 4 18:20 .. -r--------. 1 root root 20 Apr 5 22:11 .erlang.cookie -r--------. 1 rabbitmq rabbitmq 20 Apr 4 00:00 .erlang.cookie.bak -rw-r-----. 1 rabbitmq rabbitmq 878435 Apr 5 22:18 erl_crash.dump drwxr-x---. 4 rabbitmq rabbitmq 95 Apr 5 15:19 mnesia
上面 .erlang.cookie 文件只能被 root 用户读取,rabbitmq 用户是不能访问的。为了 rabbitmq 用户能够正常访问,我们需要将该文件的所属组和用户设置为 rabbitmq。名利如下:
[root@node3 rabbitmq]# chown rabbitmq:rabbitmq .erlang.cookie [root@node3 rabbitmq]# ll -a total 872 drwxr-xr-x. 3 rabbitmq rabbitmq 90 Apr 5 22:11 . drwxr-xr-x. 38 root root 4096 Apr 4 18:20 .. -r--------. 1 rabbitmq rabbitmq 20 Apr 5 22:11 .erlang.cookie -r--------. 1 rabbitmq rabbitmq 20 Apr 4 00:00 .erlang.cookie.bak -rw-r-----. 1 rabbitmq rabbitmq 878435 Apr 5 22:18 erl_crash.dump drwxr-x---. 4 rabbitmq rabbitmq 95 Apr 5 15:19 mnesia
再次使用 rabbitmq-server -detached 命令重启服务:
[root@node3 rabbitmq]# rabbitmq-server -detached
RabbitMQ 服务正常启动。