├── .ansible-lint ├── .github ├── FUNDING.yml └── workflows │ ├── ci.yml │ ├── release.yml │ └── stale.yml ├── .gitignore ├── .yamllint ├── LICENSE ├── README.md ├── defaults └── main.yml ├── handlers └── main.yml ├── meta └── main.yml ├── molecule └── default │ ├── converge.yml │ └── molecule.yml ├── tasks ├── configure.yml ├── databases.yml ├── main.yml ├── replication.yml ├── secure-installation.yml ├── setup-Archlinux.yml ├── setup-Debian.yml ├── setup-RedHat.yml ├── users.yml └── variables.yml ├── templates ├── my.cnf.j2 ├── root-my.cnf.j2 └── user-my.cnf.j2 └── vars ├── Archlinux.yml ├── Debian-10.yml ├── Debian-11.yml ├── Debian-12.yml ├── Debian.yml ├── RedHat-7.yml ├── RedHat-8.yml ├── RedHat-9.yml └── Ubuntu.yml /.ansible-lint: -------------------------------------------------------------------------------- 1 | skip_list: 2 | - 'yaml' 3 | - 'role-name' 4 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | --- 3 | github: geerlingguy 4 | patreon: geerlingguy 5 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: CI 3 | 'on': 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | schedule: 9 | - cron: "0 1 * * 3" 10 | 11 | defaults: 12 | run: 13 | working-directory: 'geerlingguy.mysql' 14 | 15 | jobs: 16 | 17 | lint: 18 | name: Lint 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Check out the codebase. 22 | uses: actions/checkout@v4 23 | with: 24 | path: 'geerlingguy.mysql' 25 | 26 | - name: Set up Python 3. 27 | uses: actions/setup-python@v5 28 | with: 29 | python-version: '3.x' 30 | 31 | - name: Install test dependencies. 32 | run: pip3 install yamllint 33 | 34 | - name: Lint code. 35 | run: | 36 | yamllint . 37 | 38 | molecule: 39 | name: Molecule 40 | runs-on: ubuntu-latest 41 | strategy: 42 | matrix: 43 | distro: 44 | - rockylinux9 45 | - ubuntu2204 46 | - ubuntu2004 47 | - debian12 48 | - debian11 49 | 50 | steps: 51 | - name: Check out the codebase. 52 | uses: actions/checkout@v4 53 | with: 54 | path: 'geerlingguy.mysql' 55 | 56 | - name: Set up Python 3. 57 | uses: actions/setup-python@v5 58 | with: 59 | python-version: '3.x' 60 | 61 | - name: Install test dependencies. 62 | run: pip3 install ansible molecule molecule-plugins[docker] docker 63 | 64 | # See: https://github.com/geerlingguy/ansible-role-mysql/issues/422 65 | - name: Disable AppArmor on Debian. 66 | run: | 67 | set -x 68 | sudo apt-get install apparmor-profiles 69 | sudo ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/ 70 | sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld 71 | if: ${{ startsWith(matrix.distro, 'debian') }} 72 | 73 | - name: Run Molecule tests. 74 | run: molecule test 75 | env: 76 | PY_COLORS: '1' 77 | ANSIBLE_FORCE_COLOR: '1' 78 | MOLECULE_DISTRO: ${{ matrix.distro }} 79 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This workflow requires a GALAXY_API_KEY secret present in the GitHub 3 | # repository or organization. 4 | # 5 | # See: https://github.com/marketplace/actions/publish-ansible-role-to-galaxy 6 | # See: https://github.com/ansible/galaxy/issues/46 7 | 8 | name: Release 9 | 'on': 10 | push: 11 | tags: 12 | - '*' 13 | 14 | defaults: 15 | run: 16 | working-directory: 'geerlingguy.mysql' 17 | 18 | jobs: 19 | 20 | release: 21 | name: Release 22 | runs-on: ubuntu-latest 23 | steps: 24 | - name: Check out the codebase. 25 | uses: actions/checkout@v4 26 | with: 27 | path: 'geerlingguy.mysql' 28 | 29 | - name: Set up Python 3. 30 | uses: actions/setup-python@v5 31 | with: 32 | python-version: '3.x' 33 | 34 | - name: Install Ansible. 35 | run: pip3 install ansible-core 36 | 37 | - name: Trigger a new import on Galaxy. 38 | run: >- 39 | ansible-galaxy role import --api-key ${{ secrets.GALAXY_API_KEY }} 40 | $(echo ${{ github.repository }} | cut -d/ -f1) $(echo ${{ github.repository }} | cut -d/ -f2) 41 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Close inactive issues 3 | 'on': 4 | schedule: 5 | - cron: "55 20 * * 6" # semi-random time 6 | 7 | jobs: 8 | close-issues: 9 | runs-on: ubuntu-latest 10 | permissions: 11 | issues: write 12 | pull-requests: write 13 | steps: 14 | - uses: actions/stale@v8 15 | with: 16 | days-before-stale: 120 17 | days-before-close: 60 18 | exempt-issue-labels: bug,pinned,security,planned 19 | exempt-pr-labels: bug,pinned,security,planned 20 | stale-issue-label: "stale" 21 | stale-pr-label: "stale" 22 | stale-issue-message: | 23 | This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! 24 | 25 | Please read [this blog post](https://www.jeffgeerling.com/blog/2020/enabling-stale-issue-bot-on-my-github-repositories) to see the reasons why I mark issues as stale. 26 | close-issue-message: | 27 | This issue has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. 28 | stale-pr-message: | 29 | This pr has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! 30 | 31 | Please read [this blog post](https://www.jeffgeerling.com/blog/2020/enabling-stale-issue-bot-on-my-github-repositories) to see the reasons why I mark issues as stale. 32 | close-pr-message: | 33 | This pr has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. 34 | repo-token: ${{ secrets.GITHUB_TOKEN }} 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.retry 2 | */__pycache__ 3 | *.pyc 4 | .cache 5 | -------------------------------------------------------------------------------- /.yamllint: -------------------------------------------------------------------------------- 1 | --- 2 | extends: default 3 | 4 | rules: 5 | line-length: 6 | max: 160 7 | level: warning 8 | 9 | ignore: | 10 | .github/workflows/stale.yml 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Jeff Geerling 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role: MySQL 2 | 3 | [![CI](https://github.com/geerlingguy/ansible-role-mysql/actions/workflows/ci.yml/badge.svg)](https://github.com/geerlingguy/ansible-role-mysql/actions/workflows/ci.yml) 4 | 5 | Installs and configures MySQL or MariaDB server on RHEL/CentOS or Debian/Ubuntu servers. 6 | 7 | ## Requirements 8 | 9 | No special requirements; note that this role requires root access, so either run it in a playbook with a global `become: true`, or invoke the role in your playbook like: 10 | 11 | ```yaml 12 | - hosts: database 13 | roles: 14 | - role: geerlingguy.mysql 15 | become: true 16 | ``` 17 | 18 | ## Role Variables 19 | 20 | Available variables are listed below, along with default values (see `defaults/main.yml`): 21 | 22 | ```yaml 23 | mysql_user_home: /root 24 | mysql_user_name: root 25 | mysql_user_password: root 26 | ``` 27 | 28 | The home directory inside which Python MySQL settings will be stored, which Ansible will use when connecting to MySQL. This should be the home directory of the user which runs this Ansible role. The `mysql_user_name` and `mysql_user_password` can be set if you are running this role under a non-root user account and want to set a non-root user. 29 | 30 | ```yaml 31 | mysql_root_home: /root 32 | mysql_root_username: root 33 | mysql_root_password: root 34 | ``` 35 | 36 | The MySQL root user account details. 37 | 38 | ```yaml 39 | mysql_root_password_update: false 40 | ``` 41 | 42 | Whether to force update the MySQL root user's password. By default, this role will only change the root user's password when MySQL is first configured. You can force an update by setting this to `true`. 43 | 44 | > Note: If you get an error like `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)` after a failed or interrupted playbook run, this usually means the root password wasn't originally updated to begin with. Try either removing the `.my.cnf` file inside the configured `mysql_user_home` or updating it and setting `password=''` (the insecure default password). Run the playbook again, with `mysql_root_password_update` set to `yes`, and the setup should complete. 45 | 46 | > Note: If you get an error like `ERROR 1698 (28000): Access denied for user 'root'@'localhost' (using password: YES)` when trying to log in from the CLI you might need to run as root or sudoer. 47 | 48 | ```yaml 49 | mysql_enabled_on_startup: true 50 | ``` 51 | 52 | Whether MySQL should be enabled on startup. 53 | 54 | ```yaml 55 | mysql_config_file: *default value depends on OS* 56 | mysql_config_include_dir: *default value depends on OS* 57 | ``` 58 | 59 | The main my.cnf configuration file and include directory. 60 | 61 | ```yaml 62 | overwrite_global_mycnf: true 63 | ``` 64 | 65 | Whether the global my.cnf should be overwritten each time this role is run. Setting this to `false` tells Ansible to only create the `my.cnf` file if it doesn't exist. This should be left at its default value (`true`) if you'd like to use this role's variables to configure MySQL. 66 | 67 | ```yaml 68 | mysql_config_include_files: [] 69 | ``` 70 | 71 | A list of files that should override the default global my.cnf. Each item in the array requires a "src" parameter which is a path to a file. An optional "force" parameter can force the file to be updated each time ansible runs. 72 | 73 | ```yaml 74 | mysql_databases: [] 75 | ``` 76 | 77 | The MySQL databases to create. A database has the values `name`, `encoding` (defaults to `utf8`), `collation` (defaults to `utf8_general_ci`) and `replicate` (defaults to `1`, only used if replication is configured). The formats of these are the same as in the `mysql_db` module. 78 | 79 | You can also delete a database (or ensure it's not on the server) by setting `state` to `absent` (defaults to `present`). 80 | 81 | ```yaml 82 | mysql_users: [] 83 | ``` 84 | 85 | The MySQL users and their privileges. A user has the values: 86 | 87 | - `name` 88 | - `host` (defaults to `localhost`) 89 | - `password` (can be plaintext or encrypted—if encrypted, set `encrypted: true`) 90 | - `encrypted` (defaults to `false`) 91 | - `priv` (defaults to `*.*:USAGE`) 92 | - `append_privs` (defaults to `false`) 93 | - `state` (defaults to `present`) 94 | - `case_sensitive` (defaults to `false`) 95 | - `update_password` (defaults to `always`) 96 | 97 | The formats of these are the same as in the `mysql_user` module. 98 | 99 | ```yaml 100 | mysql_packages: 101 | - mysql 102 | - mysql-server 103 | ``` 104 | 105 | (OS-specific, RedHat/CentOS defaults listed here) Packages to be installed. In some situations, you may need to add additional packages, like `mysql-devel`. 106 | 107 | ```yaml 108 | mysql_enablerepo: "" 109 | ``` 110 | 111 | (RedHat/CentOS only) If you have enabled any additional repositories (might I suggest geerlingguy.repo-epel or geerlingguy.repo-remi), those repositories can be listed under this variable (e.g. `remi,epel`). This can be handy, as an example, if you want to install later versions of MySQL. 112 | 113 | ```yaml 114 | mysql_python_package_debian: python3-mysqldb 115 | ``` 116 | 117 | (Ubuntu/Debian only) If you need to explicitly override the MySQL Python package, you can set it here. Set this to `python-mysqldb` if using older distributions running Python 2. 118 | 119 | ```yaml 120 | mysql_port: "3306" 121 | mysql_bind_address: '0.0.0.0' 122 | mysql_datadir: /var/lib/mysql 123 | mysql_socket: *default value depends on OS* 124 | mysql_pid_file: *default value depends on OS* 125 | ``` 126 | 127 | Default MySQL connection configuration. 128 | 129 | ```yaml 130 | mysql_log_file_group: mysql *adm on Debian* 131 | mysql_log: "" 132 | mysql_log_error: *default value depends on OS* 133 | mysql_syslog_tag: *default value depends on OS* 134 | ```yaml 135 | 136 | MySQL logging configuration. Setting `mysql_log` (the general query log) or `mysql_log_error` to `syslog` will make MySQL log to syslog using the `mysql_syslog_tag`. 137 | 138 | ```yaml 139 | mysql_slow_query_log_enabled: false 140 | mysql_slow_query_log_file: *default value depends on OS* 141 | mysql_slow_query_time: 2 142 | ``` 143 | 144 | Slow query log settings. Note that the log file will be created by this role, but if you're running on a server with SELinux or AppArmor, you may need to add this path to the allowed paths for MySQL, or disable the mysql profile. For example, on Debian/Ubuntu, you can run `sudo ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/usr.sbin.mysqld && sudo service apparmor restart`. 145 | 146 | ```yaml 147 | mysql_key_buffer_size: "256M" 148 | mysql_max_allowed_packet: "64M" 149 | mysql_table_open_cache: "256" 150 | ... 151 | ``` 152 | 153 | The rest of the settings in `defaults/main.yml` control MySQL's memory usage and some other common settings. The default values are tuned for a server where MySQL can consume 512 MB RAM, so you should consider adjusting them to suit your particular server better. 154 | 155 | ```yaml 156 | mysql_disable_log_bin: false 157 | ``` 158 | 159 | This variable should be set to `true` if you don't need replication, or otherwise don't need a log of all MySQL's activity. If you leave it at the default value, disk space may be consumed at an alarming rate on highly-utlilized database servers! 160 | 161 | ```yaml 162 | mysql_server_id: "1" 163 | mysql_max_binlog_size: "100M" 164 | mysql_binlog_format: "ROW" 165 | mysql_expire_logs_days: "10" 166 | mysql_replication_role: '' 167 | mysql_replication_master: '' 168 | mysql_replication_user: {} 169 | ``` 170 | 171 | Replication settings. Set `mysql_server_id` and `mysql_replication_role` by server (e.g. the master would be ID `1`, with the `mysql_replication_role` of `master`, and the slave would be ID `2`, with the `mysql_replication_role` of `slave`). The `mysql_replication_user` uses the same keys as individual list items in `mysql_users`, and is created on master servers, and used to replicate on all the slaves. 172 | 173 | `mysql_replication_master` needs to resolve to an IP or a hostname which is accessable to the Slaves (this could be a `/etc/hosts` injection or some other means), otherwise the slaves cannot communicate to the master. 174 | 175 | If the replication master has different IP addresses where you are running ansible and where the mysql replica is running, you can *optionally* specify a `mysql_replication_master_inventory_host` to access the machine (e.g. you run ansible on your local machine, but the mysql master and replica need to communicate on a different network) 176 | 177 | ```yaml 178 | mysql_hide_passwords: false 179 | ``` 180 | 181 | Do you need to hide tasks' output which contain passwords during the execution ? 182 | 183 | ### MariaDB usage 184 | 185 | This role works with either MySQL or a compatible version of MariaDB. On RHEL/CentOS 7+, the mariadb database engine was substituted as the default MySQL replacement package. No modifications are necessary though all of the variables still reference 'mysql' instead of mariadb. 186 | 187 | #### Ubuntu 14.04+ MariaDB configuration 188 | 189 | On Ubuntu, the package names are named differently, so the `mysql_package` variable needs to be altered. Set the following variables (at a minimum): 190 | 191 | mysql_packages: 192 | - mariadb-client 193 | - mariadb-server 194 | mysql_daemon: mariadb 195 | 196 | ## Dependencies 197 | 198 | If you have `ansible` installed (e.g. `pip3 install ansible`), none. 199 | 200 | If you have only installed `ansible-core`, be sure to require `community.mysql` in your `collections/requirements.yml` or install it manually with `ansible-galaxy collection install community.mysql`. 201 | 202 | ## Example Playbook 203 | 204 | - hosts: db-servers 205 | become: true 206 | vars_files: 207 | - vars/main.yml 208 | roles: 209 | - { role: geerlingguy.mysql } 210 | 211 | *Inside `vars/main.yml`*: 212 | 213 | mysql_root_password: super-secure-password 214 | mysql_databases: 215 | - name: example_db 216 | encoding: latin1 217 | collation: latin1_general_ci 218 | mysql_users: 219 | - name: example_user 220 | host: "%" 221 | password: similarly-secure-password 222 | priv: "example_db.*:ALL" 223 | 224 | ## License 225 | 226 | MIT / BSD 227 | 228 | ## Author Information 229 | 230 | This role was created in 2014 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). 231 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Set this to the user ansible is logging in as - should have root 3 | # or sudo access 4 | mysql_user_home: /root 5 | mysql_user_name: root 6 | mysql_user_password: root 7 | 8 | # The default root user installed by mysql - almost always root 9 | mysql_root_home: /root 10 | mysql_root_username: root 11 | mysql_root_password: root 12 | 13 | # Set this to `true` to forcibly update the root password. 14 | mysql_root_password_update: false 15 | mysql_user_password_update: false 16 | 17 | mysql_enabled_on_startup: true 18 | 19 | # Whether my.cnf should be updated on every run. 20 | overwrite_global_mycnf: true 21 | 22 | # The following variables have a default value depending on operating system. 23 | # mysql_config_file: /etc/my.cnf 24 | # mysql_config_include_dir: /etc/my.cnf.d 25 | 26 | # Whether to copy root user's config file with credentials to their homedir 27 | mysql_copy_root_user_mycnf: true 28 | 29 | # Pass in a comma-separated list of repos to use (e.g. "remi,epel"). Used only 30 | # for RedHat systems (and derivatives). 31 | mysql_enablerepo: "" 32 | 33 | # Define a custom list of packages to install; if none provided, the default 34 | # package list from vars/[OS-family].yml will be used. 35 | # mysql_packages: 36 | # - mysql 37 | # - mysql-server 38 | # - MySQL-python 39 | 40 | mysql_python_package_debian: python3-mysqldb 41 | 42 | # MySQL connection settings. 43 | mysql_port: "3306" 44 | mysql_bind_address: '0.0.0.0' 45 | mysql_skip_name_resolve: false 46 | mysql_datadir: /var/lib/mysql 47 | mysql_sql_mode: ~ 48 | # The following variables have a default value depending on operating system. 49 | # mysql_pid_file: /var/run/mysqld/mysqld.pid 50 | # mysql_socket: /var/lib/mysql/mysql.sock 51 | 52 | # Log file settings. 53 | mysql_log_file_group: mysql 54 | 55 | # Slow query log settings. 56 | mysql_slow_query_log_enabled: false 57 | mysql_slow_query_time: "2" 58 | # The following variable has a default value depending on operating system. 59 | # mysql_slow_query_log_file: /var/log/mysql-slow.log 60 | 61 | # Memory settings (default values optimized ~512MB RAM). 62 | mysql_key_buffer_size: "256M" 63 | mysql_max_allowed_packet: "64M" 64 | mysql_table_open_cache: "256" 65 | mysql_sort_buffer_size: "1M" 66 | mysql_read_buffer_size: "1M" 67 | mysql_read_rnd_buffer_size: "4M" 68 | mysql_myisam_sort_buffer_size: "64M" 69 | mysql_thread_cache_size: "8" 70 | 71 | # Query caching not available on Mysql version ≥ 8.0 72 | # On MariaDB, behavior changed on version ≥ 10.1.7: 73 | # * even if query cache usage (`query_cache_type`) is configured, it depends on the configured cache size 74 | # (`query_cache_size`), cf. https://mariadb.com/kb/en/server-system-variables/#query_cache_type: 75 | # * if you set size to 0, query caching is disabled 76 | # * if you set size to non-zero, query caching is enabled 77 | # * we are unsure of `query_cache_type = 2/DEMAND` behavior 78 | # * `query_cache_size` and `query_cache_limit` can still be configured and have the same semantics and default values as 79 | # their previous MySQL counterparts 80 | mysql_query_cache_type: "0" 81 | mysql_query_cache_size: "16M" 82 | mysql_query_cache_limit: "1M" 83 | 84 | mysql_max_connections: "151" 85 | mysql_tmp_table_size: "16M" 86 | mysql_max_heap_table_size: "16M" 87 | mysql_group_concat_max_len: "1024" 88 | mysql_join_buffer_size: "262144" 89 | 90 | # Other settings. 91 | mysql_lower_case_table_names: "0" 92 | mysql_wait_timeout: "28800" 93 | mysql_event_scheduler_state: "OFF" 94 | 95 | # InnoDB settings. 96 | mysql_innodb_file_per_table: "1" 97 | # Set .._buffer_pool_size up to 80% of RAM but beware of setting too high. 98 | mysql_innodb_buffer_pool_size: "256M" 99 | # Set .._log_file_size to 25% of buffer pool size. 100 | mysql_innodb_log_file_size: "64M" 101 | mysql_innodb_log_buffer_size: "8M" 102 | mysql_innodb_flush_log_at_trx_commit: "1" 103 | mysql_innodb_lock_wait_timeout: "50" 104 | 105 | # These settings require MySQL > 5.5. 106 | mysql_innodb_large_prefix: "1" 107 | mysql_innodb_file_format: "barracuda" 108 | 109 | # mysqldump settings. 110 | mysql_mysqldump_max_allowed_packet: "64M" 111 | 112 | # Logging settings. 113 | mysql_log: "" 114 | # The following variables have a default value depending on operating system. 115 | # mysql_log_error: /var/log/mysql/mysql.err 116 | # mysql_syslog_tag: mysql 117 | 118 | mysql_config_include_files: [] 119 | # - src: path/relative/to/playbook/file.cnf 120 | # - { src: path/relative/to/playbook/anotherfile.cnf, force: true } 121 | 122 | # Databases. 123 | mysql_databases: [] 124 | # - name: example 125 | # collation: utf8_general_ci 126 | # encoding: utf8 127 | # replicate: 1 128 | 129 | # Users. 130 | mysql_users: [] 131 | # - name: example 132 | # host: 127.0.0.1 133 | # password: secret 134 | # priv: *.*:USAGE 135 | 136 | mysql_disable_log_bin: false 137 | 138 | # Replication settings (replication is only enabled if master/user have values). 139 | mysql_server_id: "1" 140 | mysql_max_binlog_size: "100M" 141 | mysql_binlog_format: "ROW" 142 | mysql_expire_logs_days: "10" 143 | mysql_replication_role: '' 144 | mysql_replication_master: '' 145 | mysql_replication_master_inventory_host: "{{ mysql_replication_master }}" 146 | 147 | # Same keys as `mysql_users` above. 148 | mysql_replication_user: [] 149 | 150 | mysql_hide_passwords: false 151 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart mysql 3 | ansible.builtin.service: 4 | name: "{{ mysql_daemon }}" 5 | state: restarted 6 | sleep: 5 7 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | dependencies: [] 3 | 4 | galaxy_info: 5 | role_name: mysql 6 | author: geerlingguy 7 | description: MySQL server for RHEL/CentOS and Debian/Ubuntu. 8 | company: "Midwestern Mac, LLC" 9 | license: "license (BSD, MIT)" 10 | min_ansible_version: 2.12 11 | platforms: 12 | - name: Ubuntu 13 | versions: 14 | - all 15 | - name: Debian 16 | versions: 17 | - all 18 | - name: Archlinux 19 | versions: 20 | - all 21 | galaxy_tags: 22 | - database 23 | - mysql 24 | - mariadb 25 | - db 26 | - sql 27 | -------------------------------------------------------------------------------- /molecule/default/converge.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Converge 3 | hosts: all 4 | # become: true 5 | 6 | roles: 7 | - role: geerlingguy.mysql 8 | 9 | post_tasks: 10 | - name: Make sure we can connect to MySQL via Unix socket. 11 | ansible.builtin.command: "mysql -u root -proot -e 'show databases;'" 12 | changed_when: false 13 | 14 | - name: Make sure we can connect to MySQL via TCP on old MySQL version. 15 | ansible.builtin.command: "mysql -u root -proot -h 127.0.0.1 -e 'show databases;'" 16 | register: result 17 | changed_when: false 18 | failed_when: 19 | - result.rc != 0 and not ( 20 | ansible_system == "Linux" and ( 21 | (mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or 22 | (mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb') 23 | ) 24 | ) 25 | - result.rc == 0 and ( 26 | ansible_system == "Linux" and ( 27 | (mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or 28 | (mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb') 29 | ) 30 | ) 31 | 32 | - name: Make sure we can connect to MySQL via socket on modern MySQL version. 33 | ansible.builtin.command: "mysql -u root -e 'show databases;'" 34 | register: result 35 | changed_when: false 36 | failed_when: 37 | - result.rc == 0 and ( 38 | ansible_system == "Linux" and ( 39 | (mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or 40 | (mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb') 41 | ) 42 | ) 43 | - result.rc != 0 and not ( 44 | ansible_system == "Linux" and ( 45 | (mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or 46 | (mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb') 47 | ) 48 | ) 49 | -------------------------------------------------------------------------------- /molecule/default/molecule.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_name_check: 1 3 | dependency: 4 | name: galaxy 5 | options: 6 | ignore-errors: true 7 | driver: 8 | name: docker 9 | platforms: 10 | - name: instance 11 | image: "geerlingguy/docker-${MOLECULE_DISTRO:-rockylinux9}-ansible:latest" 12 | command: ${MOLECULE_DOCKER_COMMAND:-""} 13 | volumes: 14 | - /sys/fs/cgroup:/sys/fs/cgroup:rw 15 | cgroupns_mode: host 16 | privileged: true 17 | pre_build_image: true 18 | provisioner: 19 | name: ansible 20 | playbooks: 21 | converge: ${MOLECULE_PLAYBOOK:-converge.yml} 22 | -------------------------------------------------------------------------------- /tasks/configure.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Get MySQL version 3 | block: 4 | - name: Run mysql --version 5 | ansible.builtin.command: '{{ mysql_daemon }} --version' 6 | register: mysql_cli_version 7 | changed_when: false 8 | check_mode: false 9 | 10 | - name: Extract MySQL version 11 | ansible.builtin.set_fact: 12 | mysql_cli_version: >- 13 | {{ (mysql_cli_version.stdout | split(' '))[('mariadb' in (mysql_cli_version.stdout | lower)) | ternary(5, 3)][:-1] }} 14 | 15 | - name: Copy my.cnf global MySQL configuration. 16 | ansible.builtin.template: 17 | src: my.cnf.j2 18 | dest: "{{ mysql_config_file }}" 19 | owner: root 20 | group: root 21 | mode: 0644 22 | force: "{{ overwrite_global_mycnf }}" 23 | notify: restart mysql 24 | 25 | - name: Verify mysql include directory exists. 26 | ansible.builtin.file: 27 | path: "{{ mysql_config_include_dir }}" 28 | state: directory 29 | owner: root 30 | group: root 31 | mode: 0755 32 | when: mysql_config_include_files | length 33 | 34 | - name: Copy my.cnf override files into include directory. 35 | ansible.builtin.template: 36 | src: "{{ item.src }}" 37 | dest: "{{ mysql_config_include_dir }}/{{ item.src | basename }}" 38 | owner: root 39 | group: root 40 | mode: 0644 41 | force: "{{ item.force | default(false) }}" 42 | with_items: "{{ mysql_config_include_files }}" 43 | notify: restart mysql 44 | 45 | - name: Create slow query log file (if configured). 46 | ansible.builtin.command: "touch {{ mysql_slow_query_log_file }}" 47 | args: 48 | creates: "{{ mysql_slow_query_log_file }}" 49 | when: mysql_slow_query_log_enabled 50 | 51 | - name: Create datadir if it does not exist 52 | ansible.builtin.file: 53 | path: "{{ mysql_datadir }}" 54 | state: directory 55 | owner: mysql 56 | group: mysql 57 | mode: 0755 58 | setype: mysqld_db_t 59 | 60 | - name: Set ownership on slow query log file (if configured). 61 | ansible.builtin.file: 62 | path: "{{ mysql_slow_query_log_file }}" 63 | state: file 64 | owner: mysql 65 | group: "{{ mysql_log_file_group }}" 66 | mode: 0640 67 | when: mysql_slow_query_log_enabled 68 | 69 | - name: Create error log file (if configured). 70 | ansible.builtin.command: "touch {{ mysql_log_error }}" 71 | args: 72 | creates: "{{ mysql_log_error }}" 73 | when: 74 | - mysql_log | default(true) 75 | - mysql_log_error | default(false) 76 | tags: ['skip_ansible_galaxy'] 77 | 78 | - name: Set ownership on error log file (if configured). 79 | ansible.builtin.file: 80 | path: "{{ mysql_log_error }}" 81 | state: file 82 | owner: mysql 83 | group: "{{ mysql_log_file_group }}" 84 | mode: 0640 85 | when: 86 | - mysql_log | default(true) 87 | - mysql_log_error | default(false) 88 | tags: ['skip_ansible_galaxy'] 89 | 90 | - name: Ensure MySQL is started and enabled on boot. 91 | ansible.builtin.service: 92 | name: "{{ mysql_daemon }}" 93 | state: started 94 | enabled: "{{ mysql_enabled_on_startup }}" 95 | register: mysql_service_configuration 96 | -------------------------------------------------------------------------------- /tasks/databases.yml: -------------------------------------------------------------------------------- 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: "{{ item.state | default('present') }}" 8 | target: "{{ item.target | default(omit) }}" 9 | with_items: "{{ mysql_databases }}" 10 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Variable configuration. 3 | - ansible.builtin.include_tasks: variables.yml 4 | 5 | # Setup/install tasks. 6 | - ansible.builtin.include_tasks: setup-RedHat.yml 7 | when: ansible_os_family == 'RedHat' 8 | 9 | - ansible.builtin.include_tasks: setup-Debian.yml 10 | when: ansible_os_family == 'Debian' 11 | 12 | - ansible.builtin.include_tasks: setup-Archlinux.yml 13 | when: ansible_os_family == 'Archlinux' 14 | 15 | - name: Check if MySQL packages were installed. 16 | ansible.builtin.set_fact: 17 | mysql_install_packages: "{{ (rh_mysql_install_packages is defined and rh_mysql_install_packages.changed) 18 | or (deb_mysql_install_packages is defined and deb_mysql_install_packages.changed) 19 | or (arch_mysql_install_packages is defined and arch_mysql_install_packages.changed) }}" 20 | 21 | # Configure MySQL. 22 | - ansible.builtin.include_tasks: configure.yml 23 | - ansible.builtin.include_tasks: secure-installation.yml 24 | - ansible.builtin.include_tasks: databases.yml 25 | - ansible.builtin.include_tasks: users.yml 26 | - ansible.builtin.include_tasks: replication.yml 27 | -------------------------------------------------------------------------------- /tasks/replication.yml: -------------------------------------------------------------------------------- 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,REPLICATION CLIENT') }}" 8 | update_password: "{{ mysql_replication_user.update_password | default('always') }}" 9 | state: present 10 | no_log: "{{ mysql_hide_passwords }}" 11 | when: 12 | - mysql_replication_role == 'master' 13 | - mysql_replication_user.name is defined 14 | - (mysql_replication_master | length) > 0 15 | tags: ['skip_ansible_galaxy'] 16 | 17 | - name: Check slave replication status. 18 | mysql_replication: 19 | mode: getreplica 20 | login_user: "{{ mysql_root_username }}" 21 | login_password: "{{ mysql_root_password }}" 22 | no_log: "{{ mysql_hide_passwords }}" 23 | ignore_errors: true 24 | register: slave 25 | when: 26 | - mysql_replication_role == 'slave' 27 | - (mysql_replication_master | length) > 0 28 | tags: ['skip_ansible_galaxy'] 29 | 30 | # https://github.com/ansible/ansible/issues/82264 31 | - name: Check master replication status. 32 | mysql_replication: 33 | mode: getprimary 34 | delegate_to: "{{ mysql_replication_master_inventory_host | default(omit, true) }}" 35 | register: master 36 | when: 37 | - > 38 | (slave.Is_Slave is defined and not slave.Is_Slave) or 39 | (slave.Is_Replica is defined and not slave.Is_Replica) or 40 | (slave.Is_Slave is not defined and slave.Is_Replica is not defined and slave is failed) 41 | - mysql_replication_role == 'slave' 42 | - (mysql_replication_master | length) > 0 43 | tags: ['skip_ansible_galaxy'] 44 | 45 | - name: Configure replication on the slave. 46 | mysql_replication: 47 | mode: changeprimary 48 | master_host: "{{ mysql_replication_master }}" 49 | master_user: "{{ mysql_replication_user.name }}" 50 | master_password: "{{ mysql_replication_user.password }}" 51 | master_log_file: "{{ master.File }}" 52 | master_log_pos: "{{ master.Position }}" 53 | no_log: "{{ mysql_hide_passwords }}" 54 | ignore_errors: true 55 | when: 56 | - > 57 | (slave.Is_Slave is defined and not slave.Is_Slave) or 58 | (slave.Is_Replica is defined and not slave.Is_Replica) or 59 | (slave.Is_Slave is not defined and slave.Is_Replica is not defined and slave is failed) 60 | - mysql_replication_role == 'slave' 61 | - mysql_replication_user.name is defined 62 | - (mysql_replication_master | length) > 0 63 | 64 | - name: Start replication. 65 | mysql_replication: 66 | mode: startreplica 67 | when: 68 | - > 69 | (slave.Is_Slave is defined and slave.Is_Slave) or 70 | (slave.Is_Replica is defined and slave.Is_Replica) or 71 | (slave.Is_Slave is not defined and slave.Is_Replica is not defined and slave is failed) 72 | - mysql_replication_role == 'slave' 73 | - (mysql_replication_master | length) > 0 74 | tags: ['skip_ansible_galaxy'] 75 | -------------------------------------------------------------------------------- /tasks/secure-installation.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Ensure default user is present. 3 | mysql_user: 4 | name: "{{ mysql_user_name }}" 5 | host: 'localhost' 6 | password: "{{ mysql_user_password }}" 7 | priv: '*.*:ALL,GRANT' 8 | state: present 9 | no_log: "{{ mysql_hide_passwords }}" 10 | when: mysql_user_name != mysql_root_username 11 | 12 | # Has to be after the password assignment, for idempotency. 13 | - name: Copy user-my.cnf file with password credentials. 14 | ansible.builtin.template: 15 | src: "user-my.cnf.j2" 16 | dest: "{{ mysql_user_home }}/.my.cnf" 17 | owner: "{{ mysql_user_name }}" 18 | mode: 0600 19 | no_log: "{{ mysql_hide_passwords }}" 20 | when: > 21 | mysql_user_name != mysql_root_username 22 | and (mysql_install_packages | bool or mysql_user_password_update) 23 | 24 | - name: Disallow root login remotely 25 | ansible.builtin.command: '{{ mysql_daemon }} -NBe "{{ item }}"' 26 | with_items: 27 | - DELETE FROM mysql.user WHERE User='{{ mysql_root_username }}' AND Host NOT IN ('localhost', '127.0.0.1', '::1') 28 | changed_when: false 29 | 30 | - name: Get list of hosts for the root user. 31 | ansible.builtin.command: > 32 | {{ mysql_daemon }} -NBe 33 | "SELECT Host 34 | FROM mysql.user 35 | WHERE User = '{{ mysql_root_username }}' 36 | ORDER BY (Host='localhost') ASC" 37 | register: mysql_root_hosts 38 | changed_when: false 39 | check_mode: false 40 | when: mysql_install_packages | bool or mysql_root_password_update 41 | 42 | # Note: We do not use mysql_user for this operation, as it doesn't always update 43 | # the root password correctly. See: https://goo.gl/MSOejW 44 | # Set root password for MySQL >= 8.4 and MariaDB ≥ 10.4 45 | - name: Update MySQL root authentication via socket for localhost (Linux, MySQL ≥ 8.4) 46 | ansible.builtin.shell: > 47 | {{ mysql_daemon }} -u root -NBe 48 | "ALTER USER '{{ mysql_root_username }}'@'{{ item }}' 49 | IDENTIFIED {{ (mysql_daemon == 'mariadb') | ternary('VIA unix_socket', 'WITH auth_socket') }}; FLUSH PRIVILEGES;" 50 | no_log: "{{ mysql_hide_passwords }}" 51 | with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}" 52 | when: 53 | - (mysql_install_packages | bool) or mysql_root_password_update 54 | - ansible_system == "Linux" 55 | - (mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or 56 | (mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb') 57 | 58 | # Note: We do not use mysql_user for this operation, as it doesn't always update 59 | # the root password correctly. See: https://goo.gl/MSOejW 60 | # Set root password for 5.7.x. ≤ MySQL < 8.4 and MariaDB ≥ 10.4 61 | - name: Update MySQL root password for localhost root account (5.7.x ≤ MySQL < 8.4) 62 | ansible.builtin.shell: > 63 | {{ mysql_daemon }} -u root -NBe 64 | "ALTER USER '{{ mysql_root_username }}'@'{{ item }}' 65 | IDENTIFIED {{ (mysql_daemon == 'mariadb') | ternary('VIA', 'WITH') }} mysql_native_password 66 | BY '{{ mysql_root_password }}'; FLUSH PRIVILEGES;" 67 | no_log: "{{ mysql_hide_passwords }}" 68 | with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}" 69 | when: 70 | - (mysql_install_packages | bool) or mysql_root_password_update 71 | - ( 72 | mysql_daemon == 'mysql' and ( 73 | mysql_cli_version is version('5.7', '>=') and 74 | mysql_cli_version is version('8.4', '<') 75 | ) 76 | ) or 77 | (mysql_daemon == 'mariadb' and mysql_cli_version is version('10.4', '<')) 78 | 79 | # Set root password for MySQL < 5.7.x. 80 | - name: Update MySQL root password for localhost root account (< 5.7.x). 81 | ansible.builtin.shell: > 82 | {{ mysql_daemon }} -NBe 83 | 'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}"); FLUSH PRIVILEGES;' 84 | no_log: "{{ mysql_hide_passwords }}" 85 | with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}" 86 | when: 87 | - (mysql_install_packages | bool) or mysql_root_password_update 88 | - (mysql_daemon == 'mysql' and mysql_cli_version is version('5.7', '<')) 89 | 90 | # Has to be after the root password assignment, for idempotency. 91 | - name: Copy .my.cnf file with root password credentials. 92 | ansible.builtin.template: 93 | src: "root-my.cnf.j2" 94 | dest: "{{ mysql_root_home }}/.my.cnf" 95 | owner: root 96 | group: root 97 | mode: 0600 98 | no_log: "{{ mysql_hide_passwords }}" 99 | when: 100 | - mysql_install_packages | bool or mysql_root_password_update 101 | - mysql_copy_root_user_mycnf 102 | 103 | - name: Get list of hosts for the anonymous user. 104 | ansible.builtin.command: > 105 | {{ mysql_daemon }} -NBe "SELECT Host FROM mysql.user WHERE User = ''" 106 | register: mysql_anonymous_hosts 107 | changed_when: false 108 | check_mode: false 109 | 110 | - name: Remove anonymous MySQL users. 111 | mysql_user: 112 | name: "" 113 | host: "{{ item }}" 114 | state: absent 115 | with_items: "{{ mysql_anonymous_hosts.stdout_lines|default([]) }}" 116 | 117 | - name: Remove MySQL test database. 118 | mysql_db: 119 | name: 'test' 120 | state: absent 121 | -------------------------------------------------------------------------------- /tasks/setup-Archlinux.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Ensure MySQL Python libraries are installed. 3 | community.general.pacman: 4 | name: mysql-python 5 | state: present 6 | 7 | - name: Ensure MySQL packages are installed. 8 | community.general.pacman: 9 | name: "{{ mysql_packages }}" 10 | state: present 11 | register: arch_mysql_install_packages 12 | 13 | - name: Run mysql_install_db if MySQL packages were changed. 14 | ansible.builtin.command: mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql 15 | when: arch_mysql_install_packages.changed 16 | tags: ['skip_ansible_lint'] 17 | -------------------------------------------------------------------------------- /tasks/setup-Debian.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Check if MySQL is already installed. 3 | ansible.builtin.stat: 4 | path: "{{ mysql_config_file }}" 5 | register: mysql_installed 6 | 7 | - name: Update apt cache if MySQL is not yet installed. 8 | ansible.builtin.apt: 9 | update_cache: true 10 | changed_when: false 11 | when: not mysql_installed.stat.exists 12 | 13 | - name: Ensure MySQL Python libraries are installed. 14 | ansible.builtin.apt: 15 | name: "{{ mysql_python_package_debian }}" 16 | state: present 17 | 18 | - name: Ensure MySQL packages are installed. 19 | ansible.builtin.apt: 20 | name: "{{ mysql_packages }}" 21 | state: present 22 | policy_rc_d: 101 23 | register: deb_mysql_install_packages 24 | 25 | # Because Ubuntu starts MySQL as part of the install process, we need to stop 26 | # mysql and remove the logfiles in case the user set a custom log file size. 27 | - name: Ensure MySQL is stopped after initial install. 28 | ansible.builtin.service: 29 | name: "{{ mysql_daemon }}" 30 | state: stopped 31 | when: not mysql_installed.stat.exists 32 | 33 | - name: Delete innodb log files created by apt package after initial install. 34 | ansible.builtin.file: 35 | path: "{{ mysql_datadir }}/{{ item }}" 36 | state: absent 37 | with_items: 38 | - ib_logfile0 39 | - ib_logfile1 40 | when: > 41 | not mysql_installed.stat.exists 42 | and ansible_distribution_major_version | int < 12 43 | -------------------------------------------------------------------------------- /tasks/setup-RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Ensure MySQL packages are installed. 3 | ansible.builtin.yum: 4 | name: "{{ mysql_packages }}" 5 | state: present 6 | enablerepo: "{{ mysql_enablerepo | default(omit, true) }}" 7 | register: rh_mysql_install_packages 8 | -------------------------------------------------------------------------------- /tasks/users.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Ensure MySQL users are present. 3 | mysql_user: 4 | name: "{{ item.name }}" 5 | host: "{{ item.host | default('localhost') }}" 6 | password: "{{ item.password }}" 7 | priv: "{{ item.priv | default('*.*:USAGE') }}" 8 | state: "{{ item.state | default('present') }}" 9 | append_privs: "{{ item.append_privs | default(false) }}" 10 | encrypted: "{{ item.encrypted | default(false) }}" 11 | column_case_sensitive: "{{ item.case_sensitive | default(false) }}" 12 | update_password: "{{ item.update_password | default('always') }}" 13 | with_items: "{{ mysql_users }}" 14 | no_log: "{{ mysql_hide_passwords }}" 15 | -------------------------------------------------------------------------------- /tasks/variables.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Variable configuration. 3 | - name: Include OS-specific variables. 4 | ansible.builtin.include_vars: "{{ item }}" 5 | with_first_found: 6 | - files: 7 | - "vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml" 8 | - "vars/{{ ansible_os_family }}.yml" 9 | skip: true 10 | 11 | - name: Define mysql_packages. 12 | ansible.builtin.set_fact: 13 | mysql_packages: "{{ __mysql_packages | list }}" 14 | when: mysql_packages is not defined 15 | 16 | - name: Define mysql_daemon. 17 | ansible.builtin.set_fact: 18 | mysql_daemon: "{{ __mysql_daemon }}" 19 | when: mysql_daemon is not defined 20 | 21 | - name: Define mysql_slow_query_log_file. 22 | ansible.builtin.set_fact: 23 | mysql_slow_query_log_file: "{{ __mysql_slow_query_log_file }}" 24 | when: mysql_slow_query_log_file is not defined 25 | 26 | - name: Define mysql_log_error. 27 | ansible.builtin.set_fact: 28 | mysql_log_error: "{{ __mysql_log_error }}" 29 | when: mysql_log_error is not defined 30 | 31 | - name: Define mysql_syslog_tag. 32 | ansible.builtin.set_fact: 33 | mysql_syslog_tag: "{{ __mysql_syslog_tag }}" 34 | when: mysql_syslog_tag is not defined 35 | 36 | - name: Define mysql_pid_file. 37 | ansible.builtin.set_fact: 38 | mysql_pid_file: "{{ __mysql_pid_file }}" 39 | when: mysql_pid_file is not defined 40 | 41 | - name: Define mysql_config_file. 42 | ansible.builtin.set_fact: 43 | mysql_config_file: "{{ __mysql_config_file }}" 44 | when: mysql_config_file is not defined 45 | 46 | - name: Define mysql_config_include_dir. 47 | ansible.builtin.set_fact: 48 | mysql_config_include_dir: "{{ __mysql_config_include_dir }}" 49 | when: mysql_config_include_dir is not defined 50 | 51 | - name: Define mysql_socket. 52 | ansible.builtin.set_fact: 53 | mysql_socket: "{{ __mysql_socket }}" 54 | when: mysql_socket is not defined 55 | 56 | - name: Define mysql_supports_innodb_large_prefix. 57 | ansible.builtin.set_fact: 58 | mysql_supports_innodb_large_prefix: "{{ __mysql_supports_innodb_large_prefix }}" 59 | when: mysql_supports_innodb_large_prefix is not defined 60 | -------------------------------------------------------------------------------- /templates/my.cnf.j2: -------------------------------------------------------------------------------- 1 | {{ ansible_managed | comment }} 2 | 3 | [client] 4 | #password = your_password 5 | port = {{ mysql_port }} 6 | socket = {{ mysql_socket }} 7 | 8 | [mysqld] 9 | port = {{ mysql_port }} 10 | bind-address = {{ mysql_bind_address }} 11 | datadir = {{ mysql_datadir }} 12 | socket = {{ mysql_socket }} 13 | pid-file = {{ mysql_pid_file }} 14 | {% if mysql_skip_name_resolve %} 15 | skip-name-resolve 16 | {% endif %} 17 | {% if mysql_sql_mode is not none %} 18 | sql_mode = {{ mysql_sql_mode }} 19 | {% endif %} 20 | 21 | # Logging configuration. 22 | {% if mysql_log_error == 'syslog' or mysql_log == 'syslog' %} 23 | syslog 24 | syslog-tag = {{ mysql_syslog_tag }} 25 | {% else %} 26 | {% if mysql_log %} 27 | log = {{ mysql_log }} 28 | {% endif %} 29 | log-error = {{ mysql_log_error }} 30 | {% endif %} 31 | 32 | {% if mysql_slow_query_log_enabled %} 33 | # Slow query log configuration. 34 | slow_query_log = 1 35 | slow_query_log_file = {{ mysql_slow_query_log_file }} 36 | long_query_time = {{ mysql_slow_query_time }} 37 | {% endif %} 38 | 39 | 40 | {% if mysql_disable_log_bin and not mysql_replication_master %} 41 | # Disable binlog to save disk space 42 | disable-log-bin 43 | {% endif %} 44 | 45 | {% if mysql_replication_master %} 46 | # Replication 47 | server-id = {{ mysql_server_id }} 48 | 49 | {% if mysql_replication_role == 'master' %} 50 | log_bin = mysql-bin 51 | log-bin-index = mysql-bin.index 52 | expire_logs_days = {{ mysql_expire_logs_days }} 53 | max_binlog_size = {{ mysql_max_binlog_size }} 54 | binlog_format = {{mysql_binlog_format}} 55 | 56 | {% for db in mysql_databases %} 57 | {% if db.replicate|default(1) %} 58 | binlog_do_db = {{ db.name }} 59 | {% else %} 60 | binlog_ignore_db = {{ db.name }} 61 | {% endif %} 62 | {% endfor %} 63 | {% endif %} 64 | 65 | {% if mysql_replication_role == 'slave' %} 66 | read_only 67 | relay-log = relay-bin 68 | relay-log-index = relay-bin.index 69 | {% endif %} 70 | {% endif %} 71 | 72 | # Disabling symbolic-links is recommended to prevent assorted security risks 73 | symbolic-links = 0 74 | 75 | # User is ignored when systemd is used (fedora >= 15). 76 | user = mysql 77 | 78 | # http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html 79 | ;performance_schema 80 | 81 | # Memory settings. 82 | key_buffer_size = {{ mysql_key_buffer_size }} 83 | max_allowed_packet = {{ mysql_max_allowed_packet }} 84 | table_open_cache = {{ mysql_table_open_cache }} 85 | sort_buffer_size = {{ mysql_sort_buffer_size }} 86 | read_buffer_size = {{ mysql_read_buffer_size }} 87 | read_rnd_buffer_size = {{ mysql_read_rnd_buffer_size }} 88 | myisam_sort_buffer_size = {{ mysql_myisam_sort_buffer_size }} 89 | thread_cache_size = {{ mysql_thread_cache_size }} 90 | {% if (mysql_daemon == 'mysql' and mysql_cli_version is version('8.0', '<')) or 91 | mysql_daemon == 'mariadb' %} 92 | query_cache_type = {{ mysql_query_cache_type }} 93 | query_cache_size = {{ mysql_query_cache_size }} 94 | query_cache_limit = {{ mysql_query_cache_limit }} 95 | {% endif %} 96 | max_connections = {{ mysql_max_connections }} 97 | tmp_table_size = {{ mysql_tmp_table_size }} 98 | max_heap_table_size = {{ mysql_max_heap_table_size }} 99 | group_concat_max_len = {{ mysql_group_concat_max_len }} 100 | join_buffer_size = {{ mysql_join_buffer_size }} 101 | 102 | # Other settings. 103 | wait_timeout = {{ mysql_wait_timeout }} 104 | lower_case_table_names = {{ mysql_lower_case_table_names }} 105 | event_scheduler = {{ mysql_event_scheduler_state }} 106 | 107 | # InnoDB settings. 108 | {% if mysql_supports_innodb_large_prefix and ( 109 | (mysql_daemon == 'mysql' and mysql_cli_version is version('8.0', '<')) or 110 | (mysql_daemon == 'mariadb' and mysql_cli_version is version('10.6', '<'))) %} 111 | innodb_large_prefix = {{ mysql_innodb_large_prefix }} 112 | innodb_file_format = {{ mysql_innodb_file_format }} 113 | {% endif %} 114 | innodb_file_per_table = {{ mysql_innodb_file_per_table }} 115 | innodb_buffer_pool_size = {{ mysql_innodb_buffer_pool_size }} 116 | innodb_log_file_size = {{ mysql_innodb_log_file_size }} 117 | innodb_log_buffer_size = {{ mysql_innodb_log_buffer_size }} 118 | innodb_flush_log_at_trx_commit = {{ mysql_innodb_flush_log_at_trx_commit }} 119 | innodb_lock_wait_timeout = {{ mysql_innodb_lock_wait_timeout }} 120 | 121 | [mysqldump] 122 | quick 123 | max_allowed_packet = {{ mysql_mysqldump_max_allowed_packet }} 124 | 125 | [mysqld_safe] 126 | pid-file = {{ mysql_pid_file }} 127 | 128 | {% if mysql_config_include_files | length %} 129 | # * IMPORTANT: Additional settings that can override those from this file! 130 | # The files must end with '.cnf', otherwise they'll be ignored. 131 | # 132 | !includedir {{ mysql_config_include_dir }} 133 | {% endif %} 134 | -------------------------------------------------------------------------------- /templates/root-my.cnf.j2: -------------------------------------------------------------------------------- 1 | {{ ansible_managed | comment }} 2 | 3 | [client] 4 | user="{{ mysql_root_username }}" 5 | {% if ansible_system == "Linux" and ( 6 | (mysql_cli_version is version('8.4', '>=') and mysql_daemon == 'mysql') or 7 | (mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb') 8 | ) %} 9 | socket={{ mysql_socket }} 10 | {% else %} 11 | password="{{ mysql_root_password }}" 12 | {% endif %} 13 | -------------------------------------------------------------------------------- /templates/user-my.cnf.j2: -------------------------------------------------------------------------------- 1 | {{ ansible_managed | comment }} 2 | 3 | [client] 4 | user="{{ mysql_user_name }}" 5 | password="{{ mysql_user_password }}" 6 | -------------------------------------------------------------------------------- /vars/Archlinux.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_daemon: mariadb 3 | __mysql_packages: 4 | - mariadb 5 | __mysql_slow_query_log_file: /var/log/mysql/mysql-slow.log 6 | __mysql_log_error: /var/log/mysql.err 7 | __mysql_syslog_tag: mysql 8 | __mysql_pid_file: /run/mysqld/mysqld.pid 9 | __mysql_config_file: /etc/mysql/my.cnf 10 | __mysql_config_include_dir: /etc/mysql/conf.d 11 | __mysql_socket: /run/mysqld/mysqld.sock 12 | __mysql_supports_innodb_large_prefix: true 13 | -------------------------------------------------------------------------------- /vars/Debian-10.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_daemon: mariadb 3 | __mysql_packages: 4 | - default-mysql-server 5 | mysql_log_file_group: adm 6 | __mysql_slow_query_log_file: /var/log/mysql/mysql-slow.log 7 | __mysql_log_error: /var/log/mysql/mysql.log 8 | __mysql_syslog_tag: mariadb 9 | __mysql_pid_file: /run/mysqld/mysqld.pid 10 | __mysql_config_file: /etc/mysql/my.cnf 11 | __mysql_config_include_dir: /etc/mysql/conf.d 12 | __mysql_socket: /run/mysqld/mysqld.sock 13 | __mysql_supports_innodb_large_prefix: true 14 | -------------------------------------------------------------------------------- /vars/Debian-11.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_daemon: mariadb 3 | __mysql_packages: 4 | - default-mysql-server 5 | mysql_log_file_group: adm 6 | __mysql_slow_query_log_file: /var/log/mysql/mariadb-slow.log 7 | __mysql_log_error: /var/log/mysql/mysql.log 8 | __mysql_syslog_tag: mysqld 9 | __mysql_pid_file: /run/mysqld/mysqld.pid 10 | __mysql_config_file: /etc/mysql/my.cnf 11 | __mysql_config_include_dir: /etc/mysql/conf.d 12 | __mysql_socket: /run/mysqld/mysqld.sock 13 | __mysql_supports_innodb_large_prefix: true 14 | -------------------------------------------------------------------------------- /vars/Debian-12.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_daemon: mariadb 3 | __mysql_packages: 4 | - default-mysql-server 5 | mysql_log_file_group: adm 6 | __mysql_slow_query_log_file: /var/log/mysql/mariadb-slow.log 7 | __mysql_log_error: /var/log/mysql/mysql.log 8 | __mysql_syslog_tag: mysqld 9 | __mysql_pid_file: /run/mysqld/mysqld.pid 10 | __mysql_config_file: /etc/mysql/my.cnf 11 | __mysql_config_include_dir: /etc/mysql/conf.d 12 | __mysql_socket: /run/mysqld/mysqld.sock 13 | __mysql_supports_innodb_large_prefix: false 14 | -------------------------------------------------------------------------------- /vars/Debian.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_daemon: mysql 3 | __mysql_packages: 4 | - mysql-common 5 | - mysql-server 6 | mysql_log_file_group: adm 7 | __mysql_slow_query_log_file: /var/log/mysql/mysql-slow.log 8 | __mysql_log_error: /var/log/mysql/mysql.err 9 | __mysql_syslog_tag: mysql 10 | __mysql_pid_file: /var/run/mysqld/mysqld.pid 11 | __mysql_config_file: /etc/mysql/my.cnf 12 | __mysql_config_include_dir: /etc/mysql/conf.d 13 | __mysql_socket: /var/run/mysqld/mysqld.sock 14 | __mysql_supports_innodb_large_prefix: true 15 | -------------------------------------------------------------------------------- /vars/RedHat-7.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_daemon: mariadb 3 | __mysql_packages: 4 | - mariadb 5 | - mariadb-server 6 | - mariadb-libs 7 | - MySQL-python 8 | - perl-DBD-MySQL 9 | __mysql_slow_query_log_file: /var/log/mysql-slow.log 10 | __mysql_log_error: /var/log/mariadb/mariadb.log 11 | __mysql_syslog_tag: mariadb 12 | __mysql_pid_file: /var/run/mariadb/mariadb.pid 13 | __mysql_config_file: /etc/my.cnf 14 | __mysql_config_include_dir: /etc/my.cnf.d 15 | __mysql_socket: /var/lib/mysql/mysql.sock 16 | __mysql_supports_innodb_large_prefix: true 17 | -------------------------------------------------------------------------------- /vars/RedHat-8.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_daemon: mariadb 3 | __mysql_packages: 4 | - mariadb 5 | - mariadb-server 6 | - mariadb-connector-c 7 | - python3-PyMySQL 8 | - perl-DBD-MySQL 9 | __mysql_slow_query_log_file: /var/log/mysql-slow.log 10 | __mysql_log_error: /var/log/mariadb/mariadb.log 11 | __mysql_syslog_tag: mariadb 12 | __mysql_pid_file: /var/run/mariadb/mariadb.pid 13 | __mysql_config_file: /etc/my.cnf 14 | __mysql_config_include_dir: /etc/my.cnf.d 15 | __mysql_socket: /var/lib/mysql/mysql.sock 16 | # The entries controlled by this value should not be used with MariaDB >= 10.2.2 17 | # See https://github.com/frappe/bench/issues/681#issuecomment-398984706 18 | __mysql_supports_innodb_large_prefix: false 19 | -------------------------------------------------------------------------------- /vars/RedHat-9.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_daemon: mariadb 3 | __mysql_packages: 4 | - mariadb 5 | - mariadb-server 6 | - mariadb-connector-c 7 | - python3-PyMySQL 8 | - perl-DBD-MySQL 9 | __mysql_slow_query_log_file: /var/log/mysql-slow.log 10 | __mysql_log_error: /var/log/mariadb/mariadb.log 11 | __mysql_syslog_tag: mariadb 12 | __mysql_pid_file: /var/run/mariadb/mariadb.pid 13 | __mysql_config_file: /etc/my.cnf 14 | __mysql_config_include_dir: /etc/my.cnf.d 15 | __mysql_socket: /var/lib/mysql/mysql.sock 16 | # The entries controlled by this value should not be used with MariaDB >= 10.2.2 17 | # See https://github.com/frappe/bench/issues/681#issuecomment-398984706 18 | __mysql_supports_innodb_large_prefix: false 19 | -------------------------------------------------------------------------------- /vars/Ubuntu.yml: -------------------------------------------------------------------------------- 1 | --- 2 | __mysql_log_error: /var/log/mysql/error.log 3 | --------------------------------------------------------------------------------