Skip to content

Commit 6967874

Browse files
committed
Issue geerlingguy#6: Add replication configuration.
1 parent 181aef9 commit 6967874

File tree

6 files changed

+106
-16
lines changed

6 files changed

+106
-16
lines changed

defaults/main.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,29 @@ mysql_innodb_log_buffer_size: "8M"
4040
mysql_innodb_flush_log_at_trx_commit: "1"
4141
mysql_innodb_lock_wait_timeout: 50
4242

43-
# mysqldump settings
43+
# mysqldump settings.
4444
mysql_mysqldump_max_allowed_packet: "64M"
4545

46-
# mysqld_safe setting
46+
# Logging settings.
4747
mysql_log_error: /var/log/mysqld.log
4848
mysql_syslog_tag: mysqld
4949

50-
# databases and users settings
50+
# Databases.
5151
mysql_databases: []
52+
# Full example:
53+
# mysql_databases:
54+
# - { name: example, collation: utf8_general_ci, encoding: utf8, replicate: 1 }
55+
56+
# Users
5257
mysql_users: []
58+
# Full Example:
59+
# mysql_users:
60+
# - { name: example, host: 127.0.0.1, password: secret, priv: *.*:USAGE }
61+
62+
# Replication settings (replication is only enabled if master/user have values).
63+
mysql_server_id: "1"
64+
mysql_max_binlog_size: "100M"
65+
mysql_replication_role: master
66+
mysql_replication_master: ''
67+
# Same keys as `mysql_users` above.
68+
mysql_replication_user: []

tasks/databases.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
- name: Ensure MySQL databases are present.
3+
mysql_db:
4+
name: "{{ item.name }}"
5+
collation: "{{ item.collation | default('utf8_general_ci') }}"
6+
encoding: "{{ item.encoding | default('utf8') }}"
7+
state: present
8+
with_items: mysql_databases

tasks/main.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010

1111
- include: configure.yml
1212
- include: secure-installation.yml
13-
- include: databases-users.yml
13+
- include: databases.yml
14+
- include: users.yml
15+
- include: replication.yml

tasks/replication.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
- name: Ensure replication user exists on master.
3+
mysql_user:
4+
name: "{{ mysql_replication_user.name }}"
5+
host: "{{ mysql_replication_user.host | default('%') }}"
6+
password: "{{ mysql_replication_user.password }}"
7+
priv: "{{ mysql_replication_user.priv | default('*.*:REPLICATION SLAVE') }}"
8+
state: present
9+
when: >
10+
(mysql_replication_role == 'master')
11+
and mysql_replication_user
12+
and (mysql_replication_master != '')
13+
14+
- name: Check slave replication status.
15+
mysql_replication: mode=getslave
16+
ignore_errors: true
17+
register: slave
18+
when: >
19+
mysql_replication_role == 'slave'
20+
and (mysql_replication_master != '')
21+
22+
- name: Check master replication status.
23+
mysql_replication: mode=getmaster
24+
delegate_to: "{{ mysql_replication_master }}"
25+
register: master
26+
when: >
27+
slave|failed
28+
and (mysql_replication_role == 'slave')
29+
and (mysql_replication_master != '')
30+
31+
- name: Configure replication on the slave.
32+
mysql_replication:
33+
mode: changemaster
34+
master_host: "{{ mysql_replication_master }}"
35+
master_user: "{{ mysql_replication_user.name }}"
36+
master_password: "{{ mysql_replication_user.password }}"
37+
master_log_file: "{{ master.File }}"
38+
master_log_pos: "{{ master.Position }}"
39+
ignore_errors: True
40+
when: >
41+
slave|failed
42+
and (mysql_replication_role == 'slave')
43+
and (mysql_replication_master != '')
44+
and mysql_replication_user
45+
46+
- name: Start replication.
47+
mysql_replication: mode=startslave
48+
when: >
49+
slave|failed
50+
and (mysql_replication_role == 'slave')
51+
and (mysql_replication_master != '')

tasks/databases-users.yml renamed to tasks/users.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
---
2-
- name: Ensure MySQL databases are present.
3-
mysql_db:
4-
name: "{{ item.name }}"
5-
collation: "{{ item.collation | default('utf8_general_ci') }}"
6-
encoding: "{{ item.encoding | default('utf8') }}"
7-
state: present
8-
with_items: mysql_databases
9-
102
- name: Ensure MySQL users are present.
113
mysql_user:
124
name: "{{ item.name }}"

templates/my.cnf.j2

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,34 @@ port = {{ mysql_port }}
88
datadir = {{ mysql_datadir }}
99
socket = {{ mysql_socket }}
1010

11+
# Replication
12+
server-id = {{ mysql_server_id }}
13+
14+
{% if mysql_replication_role == 'master' %}
15+
log_bin = mysql-bin
16+
log-bin-index = mysql-bin.index
17+
expire_logs_days = 10
18+
max_binlog_size = {{ mysql_max_binlog_size }}
19+
20+
{% for db in mysql_databases %}
21+
{% if db.replicate|default(1) %}
22+
binlog_do_db = {{ db.name }}
23+
{% else %}
24+
binlog_ignore_db = {{ db.name }}
25+
{% endif %}
26+
{% endfor %}
27+
{% endif %}
28+
29+
{% if mysql_replication_role == 'slave' %}
30+
read_only
31+
relay-log = relay-bin
32+
relay-log-index = relay-bin.index
33+
{% endif %}
34+
1135
# Disabling symbolic-links is recommended to prevent assorted security risks
1236
symbolic-links = 0
1337

14-
# Settings user and group are ignored when systemd is used (fedora >= 15).
15-
# If you need to run mysqld under a different user or group,
16-
# customize your systemd unit file for mysqld according to the
17-
# instructions in http://fedoraproject.org/wiki/Systemd
38+
# User is ignored when systemd is used (fedora <= 15).
1839
user = mysql
1940

2041
# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html

0 commit comments

Comments
 (0)