How to keep RabbitMQ data regardless of the node name

Node name is by default part of the location where the RabbitMQ data is stored, so you need to explicitly define static location to ensure that RabbitMQ data is always available in these rare cases when there is a possibility of node name change.

Edit /etc/rabbitmq/rabbitmq-env.conf to alter default settings for RabbitMQ AMQP server. I will modify RABBITMQ_MNESIA_BASE, RABBITMQ_MNESIA_DIR and RABBITMQ_NODENAME variables, but notice that I will skip RABBITMQ_ part as it is not required in configuration file and clearly obvious.

Default configuration

Default environment configuration file.

$ cat /etc/rabbitmq/rabbitmq-env.conf 
# Defaults to rabbit. This can be useful if you want to run more than one node
# per machine - RABBITMQ_NODENAME should be unique per erlang-node-and-machine
# combination. See the clustering on a single machine guide for details:
# http://www.rabbitmq.com/clustering.html#single-machine
#NODENAME=rabbit

# By default RabbitMQ will bind to all interfaces, on IPv4 and IPv6 if
# available. Set this if you only want to bind to one network interface or#
# address family.
#NODE_IP_ADDRESS=127.0.0.1

# Defaults to 5672.
#NODE_PORT=5672

Default Mnesia directory.

$ sudo rabbitmqctl environment --formatter=json | jq --raw-output '.mnesia.dir | implode'
/var/lib/rabbitmq/mnesia/[email protected]

Use static directory

Set RABBITMQ_MNESIA_DIR variable to define static data directory.

$ cat /etc/rabbitmq/rabbitmq-env.conf 
# Defaults to rabbit. This can be useful if you want to run more than one node
# per machine - RABBITMQ_NODENAME should be unique per erlang-node-and-machine
# combination. See the clustering on a single machine guide for details:
# http://www.rabbitmq.com/clustering.html#single-machine
#NODENAME=rabbit

# By default RabbitMQ will bind to all interfaces, on IPv4 and IPv6 if
# available. Set this if you only want to bind to one network interface or#
# address family.
#NODE_IP_ADDRESS=127.0.0.1

# Defaults to 5672.
#NODE_PORT=5672

MNESIA_DIR=$MNESIA_BASE/rabbitmq

Restart rabbitmq-server.

$ sudo systemctl restart rabbitmq-server

Static Mnesia directory.

$ sudo rabbitmqctl environment --formatter=json | jq --raw-output '.mnesia.dir | implode'
/var/lib/rabbitmq/mnesia/rabbitmq

Use custom prefix

Set RABBITMQ_NODENAME to define prefix, but skip hostname part, RABBITMQ_MNESIA_DIR variable to define static data directory, RABBITMQ_MNESIA_BASE is used just for reference.

$ cat /etc/rabbitmq/rabbitmq-env.conf 
# Defaults to rabbit. This can be useful if you want to run more than one node
# per machine - RABBITMQ_NODENAME should be unique per erlang-node-and-machine
# combination. See the clustering on a single machine guide for details:
# http://www.rabbitmq.com/clustering.html#single-machine
NODENAME=fox

# By default RabbitMQ will bind to all interfaces, on IPv4 and IPv6 if
# available. Set this if you only want to bind to one network interface or#
# address family.
#NODE_IP_ADDRESS=127.0.0.1

# Defaults to 5672.
#NODE_PORT=5672

MNESIA_BASE=/var/lib/rabbitmq/mnesia
MNESIA_DIR=$MNESIA_BASE/$NODENAME

Restart rabbitmq-server.

$ sudo systemctl restart rabbitmq-server

Dynamic Mnesia directory that depends on the prefix, but ignores hostname part.

$ sudo rabbitmqctl environment --formatter=json | jq --raw-output '.mnesia.dir | implode'
/var/lib/rabbitmq/mnesia/fox

Node name uses prefix and short hostname.

$ sudo rabbitmqctl eval 'node().'
[email protected]