Created
December 29, 2017 15:27
-
-
Save alexbowers/577356a5dcbc7c61aeb47a08cbbbcfb3 to your computer and use it in GitHub Desktop.
Install MySQL 5.7 via Ansible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: restart apparmor | |
become: yes | |
service: | |
name: apparmor | |
state: restarted | |
- name: restart mysql | |
become: yes | |
service: | |
name: mysql | |
state: restarted |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Install Percona Server apt signing key | |
become: yes | |
apt_key: | |
keyserver: keys.gnupg.net | |
id: 8507EFA5 | |
state: present | |
- name: Install Percona Server apt repository | |
become: yes | |
apt_repository: | |
repo: 'deb http://repo.percona.com/apt xenial main' | |
state: present | |
- name: Install Percona Server apt source repository | |
become: yes | |
apt_repository: | |
repo: 'deb-src http://repo.percona.com/apt xenial main' | |
state: present | |
- name: Update apt cache | |
become: yes | |
apt: | |
update_cache: yes | |
- name: Install Percona 5.7 Server | |
become: yes | |
apt: | |
pkg: percona-server-common-5.7 | |
state: latest | |
update_cache: yes | |
environment: | |
DEBIAN_FRONTEND: noninteractive | |
- name: Install Percona 5.7 Server | |
become: yes | |
apt: | |
pkg: percona-server-client-5.7 | |
state: latest | |
update_cache: yes | |
environment: | |
DEBIAN_FRONTEND: noninteractive | |
- name: Install Percona 5.7 Server | |
become: yes | |
apt: | |
pkg: percona-server-server-5.7 | |
state: latest | |
update_cache: yes | |
environment: | |
DEBIAN_FRONTEND: noninteractive | |
- name: Install python packages | |
become: yes | |
apt: pkg={{ item }} state=present | |
with_items: | |
- vim | |
- python-pycurl | |
- python-mysqldb | |
- name: Ensure mysql is started | |
become: yes | |
service: | |
name: mysql | |
state: started | |
- name: Remove anonymous MySQL user accounts | |
become: yes | |
mysql_user: | |
name: '' | |
host: localhost | |
state: absent | |
- name: Remove anonymous MySQL user accounts | |
become: yes | |
mysql_user: | |
name: '' | |
host_all: yes | |
state: absent | |
- name: Update mysql root password for all root accounts | |
become: yes | |
mysql_user: | |
name: root | |
host: "{{ item }}" | |
password: "{{ dbs[environment].password }}" | |
priv: "*.*:ALL,GRANT" | |
with_items: | |
- "{{ ansible_hostname }}" | |
- "%" | |
- 127.0.0.1 | |
- ::1 | |
- localhost | |
- name: Update ~/.my.cnf file | |
become: yes | |
template: | |
src: ../templates/user_my.cnf.j2 | |
dest: ~/.my.cnf | |
- name: Check MySQL Data Directory | |
command: > | |
mysql --user=root --password={{ dbs[environment].password }} | |
--batch --skip-column-names --execute="SELECT @@datadir;" | |
register: mysql_datadir | |
check_mode: False | |
changed_when: False | |
- name: Stop MySQL | |
become: yes | |
service: | |
name: mysql | |
state: stopped | |
when: mysql_datadir.stdout == "/var/lib/mysql/" | |
- name: Change MySQL Data Directory | |
become: yes | |
command: > | |
rsync -av /var/lib/mysql "/mnt/database-storage-{{environment}}" | |
when: mysql_datadir.stdout == "/var/lib/mysql/" | |
- name: Move old MySQL Data Directory | |
become: yes | |
command: mv /var/lib/mysql /var/lib/mysql.bak | |
when: mysql_datadir.stdout == "/var/lib/mysql/" | |
- name: Restart mysql | |
become: yes | |
service: | |
name: mysql | |
state: restarted | |
- name: Modify [mysqld] config options | |
become: yes | |
ini_file: | |
path: /etc/mysql/percona-server.conf.d/mysqld.cnf | |
section: mysqld | |
option: "{{ item.option }}" | |
value: "{{ item.value }}" | |
state: "{{ item.state }}" | |
with_items: | |
- { option: "bind-address", value: 0.0.0.0, state: absent } | |
- { option: "default-storage-engine", value: InnoDB, state: present } | |
- { option: sql_mode, value: "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION", state: present } | |
- { option: "symbolic-links", value: 0, state: present } | |
- { option: datadir, value: "/mnt/database-storage-{{environment}}/mysql/", state: present } | |
- { option: "log-bin", value: "/mnt/database-storage-{{environment}}/mysql/mysql-bin", state: present } | |
- { option: "expire-logs-days", value: 14, state: present } | |
- { option: "sync-binlog", value: 1, state: present } | |
- { option: "log-error", value: "/var/log/mysql/mysql-error.log", state: present } | |
- { option: "log-queries-not-using-indexes", value: 1, state: present } | |
- { option: "slow-query-log", value: 1, state: present } | |
- { option: "slow-query-log-file", value: "/var/log/mysql/mysql-slow.log", state: present } | |
- { option: "tmp-table-size", value: 32M, state: present } | |
- { option: "max-heap-table-size", value: 32M, state: present } | |
- { option: "query-cache-type", value: 0, state: present } | |
- { option: "query-cache-size", value: 0, state: present } | |
- { option: "max-connections", value: 500, state: present } | |
- { option: "thread-cache-size", value: 50, state: present } | |
- { option: "open-files-limit", value: 65535, state: present } | |
- { option: "table-definition-cache", value: 1024, state: present } | |
- { option: "table-open-cache", value: 2048, state: present } | |
- { option: "key-buffer-size", value: 32M, state: present } | |
- { option: "lc-messages-dir", value: "/usr/share/mysql", state: present } | |
- { option: "myisam-recover", value: "FORCE,BACKUP", state: absent } | |
- { option: "max-allowed-packet", value: 16M, state: present } | |
- { option: "max-connect-errors", value: 1000000, state: present } | |
notify: | |
- restart mysql | |
- name: Configure MySQL my.cnf file | |
become: yes | |
copy: | |
src: "../files/{{environment}}/my.cnf" | |
dest: /etc/mysql/percona-server.conf.d/my.cnf | |
notify: | |
- restart mysql | |
- name: Change Apparmor Access Control Rules | |
become: yes | |
lineinfile: | |
dest: /etc/apparmor.d/tunables/alias | |
regexp: "^alias /var/lib/mysql" | |
line: "alias /var/lib/mysql/ -> /mnt/database-storage-{{environment}}/mysql/," | |
state: present | |
notify: | |
- restart apparmor | |
- name: Start MySQL | |
become: yes | |
service: | |
name: mysql | |
state: started |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[mysqld] | |
# Values obtained from using the Percona server conf generator. These are the values that change between server configs based on RAM, CPUs etc. | |
explicit_defaults_for_timestamp | |
# INNODB # | |
innodb-flush-method = O_DIRECT | |
innodb-log-files-in-group = 2 | |
innodb-log-file-size = 256M | |
innodb-flush-log-at-trx-commit = 1 | |
innodb-file-per-table = 1 | |
innodb-buffer-pool-size = 12G | |
server-id = 72364823 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[client] | |
password={{ dbs[environment].password }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment