Added option to prepare mysql servers for replication
parent
09371b095f
commit
31f002f9d9
@ -0,0 +1,101 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# Manage MySQL accounts and databases.
|
||||
#
|
||||
# Note: in the following code:
|
||||
# - account means user@host
|
||||
# - user is the user part of account
|
||||
#
|
||||
|
||||
MYSQL_OPTS="--raw --skip-column-names --skip-line-numbers"
|
||||
|
||||
usage() {
|
||||
cat <<EOT >&2
|
||||
Usage: $0 <command> [<command arg>]
|
||||
|
||||
Available commands are:
|
||||
|
||||
list [<user>]
|
||||
List all accounts and their databases, separated by semi-colon. If user
|
||||
is specified, list databases for this user only.
|
||||
|
||||
passwd <user> <new password>
|
||||
Change password for specified user.
|
||||
|
||||
EOT
|
||||
}
|
||||
|
||||
error() {
|
||||
printf >&2 "Error: $@\n"
|
||||
}
|
||||
|
||||
get_host() {
|
||||
user="$1"
|
||||
host=$(mysql $MYSQL_OPTS --execute "SELECT host FROM mysql.user WHERE user='$user'")
|
||||
if [ $(echo "$host" |wc -l) -gt 1 ]; then
|
||||
# TODO: Not perfect!
|
||||
echo "$host" |grep '%'
|
||||
else
|
||||
echo $host
|
||||
fi
|
||||
}
|
||||
|
||||
get_dbs() {
|
||||
account="$1"
|
||||
echo "$(mysql $MYSQL_OPTS --execute "SHOW GRANTS FOR $account" |perl -ne 'print "$1 " if (/^GRANT (?!USAGE).* ON `(.*)`/)')"
|
||||
}
|
||||
|
||||
get_accounts() {
|
||||
echo "$(mysql $MYSQL_OPTS --execute "SELECT user,host FROM mysql.user;" |perl -ne 'print "$1\@$2\n" if (/^([^\s]+)\s+([^\s]+)$/)'|sed "s/^/'/; s/@/'@'/; s/$/'/;")"
|
||||
}
|
||||
|
||||
list() {
|
||||
if [ $# -gt 0 ]; then
|
||||
user="$1"
|
||||
host=$(get_host $user)
|
||||
account="'$user'@'$host'"
|
||||
echo $account:$(get_dbs "$account")
|
||||
else
|
||||
for account in $(get_accounts); do
|
||||
echo $account:$(get_dbs "$account")
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
passwd() {
|
||||
if [ $# -ne 2 ]; then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
user="$1"
|
||||
password="$2"
|
||||
host=$(get_host $user)
|
||||
|
||||
mysql -e "SET PASSWORD FOR '$user'@'$host' = PASSWORD('$password');"
|
||||
}
|
||||
|
||||
|
||||
#
|
||||
# Argument processing.
|
||||
#
|
||||
|
||||
if [ $# -lt 1 ]; then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
command="$1"
|
||||
shift
|
||||
|
||||
case "$command" in
|
||||
list)
|
||||
list $@
|
||||
;;
|
||||
passwd)
|
||||
passwd $@
|
||||
;;
|
||||
*)
|
||||
error "Unknown command: $command."
|
||||
;;
|
||||
esac
|
@ -0,0 +1,13 @@
|
||||
# Ansible managed
|
||||
service mysqlchk
|
||||
{
|
||||
socket_type = stream
|
||||
port = 8306
|
||||
protocol = tcp
|
||||
wait = no
|
||||
type = UNLISTED
|
||||
user = root
|
||||
server = /usr/share/scripts/mysqlchk.sh
|
||||
log_on_failure += USERID
|
||||
disable = no
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Ansible managed
|
||||
#
|
||||
# http://sysbible.org/x/2008/12/04/having-haproxy-check-mysql-status-through-a-xinetd-script/
|
||||
#
|
||||
# This script checks if a mysql server is healthy running on localhost. It will
|
||||
# return:
|
||||
#
|
||||
# "HTTP/1.x 200 OK\r" (if mysql is running smoothly)
|
||||
#
|
||||
# - OR -
|
||||
#
|
||||
# "HTTP/1.x 500 Internal Server Error\r" (else)
|
||||
#
|
||||
# The purpose of this script is make haproxy capable of monitoring mysql properly
|
||||
#
|
||||
# Author: Unai Rodriguez
|
||||
#
|
||||
# It is recommended that a low-privileged-mysql user is created to be used by
|
||||
# this script. Something like this:
|
||||
#
|
||||
# mysql> GRANT SELECT on mysql.* TO 'mysqlchkusr'@'localhost' \
|
||||
# -> IDENTIFIED BY '257retfg2uysg218' WITH GRANT OPTION;
|
||||
# mysql> flush privileges;
|
||||
|
||||
TMP_FILE="/tmp/mysqlchk.out"
|
||||
ERR_FILE="/tmp/mysqlchk.err"
|
||||
|
||||
#
|
||||
# We perform a simple query that should return a few results :-p
|
||||
#
|
||||
/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf -e "show databases;" > $TMP_FILE 2> $ERR_FILE
|
||||
|
||||
#
|
||||
# Check the output. If it is not empty then everything is fine and we return
|
||||
# something. Else, we just do not return anything.
|
||||
#
|
||||
|
||||
if [ "$(/bin/cat $TMP_FILE)" != "" ]; then
|
||||
# mysql is fine, return http 200
|
||||
/bin/echo -e "HTTP/1.1 200 OK\r\n"
|
||||
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
|
||||
/bin/echo -e "\r\n"
|
||||
/bin/echo -e "MySQL is running.\r\n"
|
||||
/bin/echo -e "\r\n"
|
||||
else
|
||||
# mysql is fine, return http 503
|
||||
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
|
||||
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
|
||||
/bin/echo -e "\r\n"
|
||||
/bin/echo -e "MySQL is *down*.\r\n"
|
||||
/bin/echo -e "\r\n"
|
||||
fi
|
@ -0,0 +1,53 @@
|
||||
---
|
||||
|
||||
- name: 'Copy MySQL configuration for replication'
|
||||
template:
|
||||
src: 'replication.cnf.j2'
|
||||
dest: "{{ mysql_config_directory }}/zzzz-replication.cnf"
|
||||
with_first_found:
|
||||
- "templates/mysql/replication.{{ inventory_hostname }}.cnf.j2"
|
||||
- "templates/mysql/replication.{{ host_group }}.cnf.j2"
|
||||
- 'templates/mysql/replication.cnf.j2'
|
||||
- 'replication.cnf.j2'
|
||||
notify: 'restart mysql'
|
||||
|
||||
- name: 'Create repl user'
|
||||
mysql_user:
|
||||
name: 'repl'
|
||||
host: '%'
|
||||
encrypted: true
|
||||
password: "{{ mysql_repl_password }}"
|
||||
priv: '*.*:REPLICATION SLAVE,REPLICATION CLIENT'
|
||||
update_password: 'on_create'
|
||||
state: 'present'
|
||||
register: create_repl_user
|
||||
when: mysql_repl_password | length > 0
|
||||
|
||||
- name: 'Add Nagios check for replication'
|
||||
template:
|
||||
src: 'replication_check.cfg.j2'
|
||||
dest: '/etc/nagios/nrpe.d/replication.cfg'
|
||||
notify: 'restart nagios-nrpe-server'
|
||||
|
||||
- name: 'Install xinetd'
|
||||
apt:
|
||||
name: 'xinetd'
|
||||
|
||||
- name: 'Add xinetd configuration for MySQL HAProxy check'
|
||||
copy:
|
||||
src: 'xinetd/mysqlchk'
|
||||
dest: '/etc/xinetd.d/'
|
||||
mode: '0644'
|
||||
notify: 'restart xinetd'
|
||||
|
||||
- name: 'Copy mysqlchk script'
|
||||
copy:
|
||||
src: 'xinetd/mysqlchk.sh'
|
||||
dest: '/usr/share/scripts/'
|
||||
mode: '0755'
|
||||
|
||||
- name: 'Copy dbadmin script'
|
||||
copy:
|
||||
src: 'dbadmin.sh'
|
||||
dest: '/usr/share/scripts/'
|
||||
mode: '0755'
|
@ -0,0 +1,7 @@
|
||||
# {{ansible_managed}}
|
||||
|
||||
[mysqld]
|
||||
{% if mysql_log_bin %}
|
||||
log_bin = {{ mysql_log_bin }}
|
||||
{% endif %}
|
||||
server_id = {{ mysql_server_id }}
|
@ -0,0 +1,3 @@
|
||||
# ansible managed
|
||||
|
||||
command[check_mysql_slave]=/usr/lib/nagios/plugins/check_mysql --check-slave -H localhost -f ~nagios/.my.cnf -w 1800 -c 3600
|
Loading…
Reference in New Issue