CockroachDB systemd script

CockroachDB systemd script

Get Social!

This is a simple systemd script for CockroachDB. It works for single node installations or multiple nodes, however you’ll need to manually join each node to the cluster before using the systemd script. 

Create the systemd file and add the following content:

vi /etc/systemd/system/cockroach.service
[Unit]
Description=CockroachDB

[Install]
WantedBy=multi-user.target

[Service]
ExecStartPre=/bin/bash -c "test -f /etc/cockroachdb/hosts && (/bin/systemctl set-environment JOIN_SWITCH=--join=$(test -f /etc/cockroachdb/hosts && cat /dev/null /etc/cockroachdb/hosts)) || exit 0"
ExecStartPre=/bin/bash -c "test -f /etc/cockroachdb/host && (/bin/systemctl set-environment HOST_SWITCH=--host=$(test -f /etc/cockroachdb/host && cat /dev/null /etc/cockroachdb/host)) || exit 0"

ExecStart=/usr/local/bin/cockroach start --certs-dir=/etc/cockroachdb/certs \
                                         --store=/var/data/cockroachdb/ \
                                         --cache=.40 \
                                         --max-sql-memory=.30 \
                                         --external-io-dir=/tmp/cockroachdb/externalio \
                                         --temp-dir=/tmp/ \
                                         --port=26257 \
                                         --http-port=7005 \
                                         --logtostderr=ERROR \
                                         --log-dir=/var/logs/cockroachdb \
                                         $JOIN_SWITCH $HOST_SWITCH

ExecStop=/usr/local/bin/cockroach quit --certs-dir=/etc/cockroachdb/certs
SyslogIdentifier=cockroachdb
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
LimitNOFILE=35000

Note that there are several paths specified in the above file which may need to be tailored to your installation requirements.

Run the following to enable the service on system start, and to start the CockroachDB service.

systemctl enable cockroach # enable on startup
systemctl start cockroach  # start CockroachDB

If your host belongs to a cluster, create a hosts file containing the hosts of other nodes in your cluster that will be read when starting your local node. For multiple nodes, use a comma to separate each hostname and port combination.

vi /etc/cockroachdb/hosts
server1.jamescoyle.net:26257,server2.jamescoyle.net:26257

Visit our advertisers

Quick Poll

What type of VPN protocol do you use?

Visit our advertisers