Skip to content

Instantly share code, notes, and snippets.

@michaeljoy
Last active November 18, 2019 14:24
Show Gist options
  • Save michaeljoy/ac94cb9543237e8b9d7d5720fbd88600 to your computer and use it in GitHub Desktop.
Save michaeljoy/ac94cb9543237e8b9d7d5720fbd88600 to your computer and use it in GitHub Desktop.
pt-heartbeat systemd init script example - percona tookit systemd examples
[mysql]
host=localhost
socket=/var/run/mysqld/mysqld.sock
[client]
host=localhost
socket=/var/run/mysqld/mysqld.sock
utc
replace
daemonize
pid=/var/run/pt-heartbeat-databasenamehere.pid
socket=/var/run/mysqld/mysqld.sock
database=databasenamehere
table=heartbeat
interval=0.01
user=insert_low_privilege_mysql_user_here
password=insert_low_privilege_mysql_user_password_here
#
# pt-heartbeat systemd service file for databasenamehere
#
[Unit]
Description="pt-heartbeat"
After=network-online.target syslog.target
Wants=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
PIDFile=/var/run/pt-heartbeat-databasenamehere.pid
ExecStart=/usr/bin/pt-heartbeat "--config=/etc/percona-toolkit/pt-heartbeat-databasenamehere.conf" "--defaults-file=/etc/percona-toolkit/my.cnf" "--update"
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=pt-heartbeat
Restart=on-abort
-- Grants for 'pt_heartbeat'@'10.%'
CREATE USER IF NOT EXISTS 'pt_heartbeat'@'10.%';
ALTER USER 'pt_heartbeat'@'10.%' IDENTIFIED WITH 'mysql_native_password' AS 'INSERTREALLYSECUREPASSWORDHERE' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT DELETE, INSERT, SELECT, UPDATE ON `databasenamehere`.`heartbeat` TO 'pt_heartbeat'@'10.%';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'pt_heartbeat'@'10.%';
-- Grants for 'pt_heartbeat'@'127.0.0.1'
CREATE USER IF NOT EXISTS 'pt_heartbeat'@'127.0.0.1';
ALTER USER 'pt_heartbeat'@'127.0.0.1' IDENTIFIED WITH 'mysql_native_password' AS 'INSERTREALLYSECUREPASSWORDHERE' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT DELETE, INSERT, SELECT, UPDATE ON `databasenamehere`.`heartbeat` TO 'pt_heartbeat'@'127.0.0.1';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'pt_heartbeat'@'127.0.0.1';
-- Grants for 'pt_heartbeat'@'localhost'
CREATE USER IF NOT EXISTS 'pt_heartbeat'@'localhost';
ALTER USER 'pt_heartbeat'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS 'INSERTREALLYSECUREPASSWORDHERE' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT DELETE, INSERT, SELECT, UPDATE ON `databasenamehere`.`heartbeat` TO 'pt_heartbeat'@'localhost';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'pt_heartbeat'@'localhost';
@hafkensite
Copy link

If you specify daemonize in the configuration of pt-heartbeat the systemd service will not always run correctly on boot. Systemd does not expect the process to spawn a separate daemon process.

@jonathan-russo
Copy link

@hafkensite I was able to get systemd to start the service reliably by specifying a different Type and After section. My .service file is as follows

#
# pt-heartbeat systemd service file
#
[Unit]
Description="pt-heartbeat"
After=network-online.target syslog.target
Wants=network-online.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
PIDFile=/var/run/pt-heartbeat.pid
ExecStart=/usr/bin/pt-heartbeat "--config=/etc/percona-toolkit/pt-heartbeat.conf" "--defaults-file=/etc/percona-toolkit/my.cnf" "--update"
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=pt-heartbeat
Restart=on-abort

@michaeljoy
Copy link
Author

I updated the gist with your suggested systemd configuration... just curious, which version of Ubuntu are you utilizing this on @jonathan-russo ? Originally this was implemented on Ubuntu 14.04 so it's quite a bit older than current at this point. Also, for posterity and anyone reading this, when running parallel GTID replication threads, replication lag is not consistent across all hosted databases. So you need to track this 'per database' in such cases that you're using parallel replication threads in some of the more enhanced MySQL replication configuration subsets.

@jonathan-russo
Copy link

@michaeljoy I actually am running that systemd config on Amazon Linux 2(4.14.146-120.181.amzn2.x86_64).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment