Skip to content

Instantly share code, notes, and snippets.

@lstellway
Created February 23, 2022 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lstellway/29825ee8c68eeb8c2f9247d1e6e1fbf8 to your computer and use it in GitHub Desktop.
Save lstellway/29825ee8c68eeb8c2f9247d1e6e1fbf8 to your computer and use it in GitHub Desktop.
Helper script to update MySQL / MariaDB TLS after renewing LetsEncrypt certificate
#!/bin/bash
DOMAIN="$1"
SOURCE="/etc/letsencrypt/live/${DOMAIN}"
DESTINATION="/var/lib/mysql/pki"
USER="mysql."
# Validate provided domain
if [ -z "${DOMAIN}" ] || [ ! -d "${SOURCE}" ]; then
printf "Please enter a valid domain (provided '%s')\n" "${DOMAIN}"
exit 1
fi
# Create files
mkdir -p "${DESTINATION}"
cp "${SOURCE}/cert.pem" "${DESTINATION}"
openssl x509 -in "${SOURCE}/chain.pem" > "${DESTINATION}/chain.pem"
openssl rsa -in "${SOURCE}/privkey.pem" -out "${DESTINATION}/privkey.pem"
# Set permissions
chown -R "${USER}" "${DESTINATION}"
chmod 600 $DESTINATION/*.pem
# Reload TLS
mysql --user=root --execute="FLUSH SSL"
@lstellway
Copy link
Author

Example usage:

update-mysql-letsencrypt example.com

(where example.com represents the directory in /etc/letsencrypt/live/*)

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