最近在搞mariadb的高可用安装, 直接使用的官方ansible, 坑太多了, 现在记录下来
官方脚本下载: ansible-galaxy install geerlingguy.mysql
需要修改的地方:
- roles/tasks/main.yml
12345678910111213141516171819202122232425262728293031323334---# Variable configuration.- include_tasks: variables.yml# Setup/install tasks.- include_tasks: setup-RedHat.ymlwhen: ansible_os_family == 'RedHat' and not uninstall_mysql- include_tasks: setup-Debian.ymlwhen: ansible_os_family == 'Debian' and not uninstall_mysql- include_tasks: setup-Archlinux.ymlwhen: ansible_os_family == 'Archlinux' and not uninstall_mysql- name: Check if MySQL packages were installed.set_fact:mysql_install_packages: "{{ (rh_mysql_install_packages is defined and rh_mysql_install_packages.changed)or (deb_mysql_install_packages is defined and deb_mysql_install_packages.changed)or (arch_mysql_install_packages is defined and arch_mysql_install_packages.changed) }}"when: not uninstall_mysql- name: Include task list in play only if the condition is trueinclude_tasks: "{{ item }}"with_items:- configure.yml- secure-installation.yml- databases.yml- users.yml- replication.ymlwhen: not uninstall_mysql- name: Uninstall mariadbinclude: uninstall.ymlwhen: uninstall_mysql
- roles/tasks/configure.yml: 如果要自定义一些目录的话, 需要创建目录并修改文件夹权限
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116---- name: Get MySQL version.command: 'mysql --version'register: mysql_cli_versionchanged_when: falsecheck_mode: false- name: Copy my.cnf global MySQL configuration.template:src: my.cnf.j2dest: "{{ mysql_config_file }}"owner: rootgroup: rootmode: 0644force: "{{ overwrite_global_mycnf }}"notify: restart mysql- name: Verify mysql include directory exists.file:path: "{{ mysql_config_include_dir }}"state: directoryowner: rootgroup: rootmode: 0755when: mysql_config_include_files | length- name: Copy my.cnf override files into include directory.template:src: "{{ item.src }}"dest: "{{ mysql_config_include_dir }}/{{ item.src | basename }}"owner: rootgroup: rootmode: 0644force: "{{ item.force | default(False) }}"with_items: "{{ mysql_config_include_files }}"notify: restart mysql- name: Create slow query log file (if configured).shell: >mysql_slow_query_log_file={{ mysql_slow_query_log_file }};mkdir -p ${mysql_slow_query_log_file%/*};touch ${mysql_slow_query_log_file}when: mysql_slow_query_log_enabled- name: Create datadir if it does not existfile:path: "{{ mysql_datadir }}"state: directoryowner: mysqlgroup: mysqlmode: 0755setype: mysqld_db_t- name: Set ownership on slow query log file (if configured).file:path: "{{ mysql_slow_query_log_file }}"state: fileowner: mysqlgroup: "{{ mysql_log_file_group }}"mode: 0640when: mysql_slow_query_log_enabled- name: Create error log directory (if configured).shell: >mysql_log_error={{ mysql_log_error }};mkdir -p ${mysql_log_error%/*};touch ${mysql_log_error};chown -R mysql:{{ mysql_log_file_group }} ${mysql_log_error%/*}when:- mysql_log == ""- mysql_log_error != ""tags: ['skip_ansible_galaxy']- name: Set ownership on error log file (if configured).file:path: "{{ mysql_log_error }}"state: fileowner: mysqlgroup: "{{ mysql_log_file_group }}"mode: 0640when:- mysql_log == ""- mysql_log_error != ""tags: ['skip_ansible_galaxy']- name: Create socket/pid directoryshell: >mysql_pid_file={{ mysql_pid_file }};mysql_socket={{ mysql_socket }};for file in `echo $mysql_pid_file $mysql_socket`; dodir=${file%/*};if [[ ! -d $dir ]]; thenmkdir -p ${dir}chown mysql:mysql ${dir}chmod 0755 ${dir}elseecho "$dir already exist"fidone- name: Create tmpdir if it does not existfile:path: "{{ mysql_tmpdir }}"state: directoryowner: mysqlgroup: mysqlmode: 0777- name: init mysql dbshell: >mysql_install_db --user=mysql --basedir=/usr --datadir={{ mysql_datadir }}ignore_errors: true- name: Ensure MySQL is started and enabled on boot.service: "name={{ mysql_daemon }} state=started enabled={{ mysql_enabled_on_startup }}"register: mysql_service_configuration - roles/tasks/database.yml: 注意修改默认字符集
1234567891011---- name: Ensure MySQL databases are present.mysql_db:name: "{{ item.name }}"login_user: "{{ mysql_root_username }}"login_password: "{{ mysql_root_password }}"login_unix_socket: "{{ mysql_socket }}"collation: "{{ item.collation | default('utf8mb4_unicode_ci') }}"encoding: "{{ item.encoding | default('utf8mb4') }}"state: "{{ item.state | default('present') }}"with_items: "{{ mysql_databases }}" - roles/tasks/variables.yml:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859---# Variable configuration.- name: Include OS-specific variables.include_vars: "{{ item }}"with_first_found:- files:- "vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"- "vars/{{ ansible_os_family }}.yml"skip: true- name: Define mysql_packages.set_fact:mysql_packages: "{{ __mysql_packages | list }}"when: mysql_packages is not defined- name: Define mysql_daemon.set_fact:mysql_daemon: "{{ __mysql_daemon }}"when: mysql_daemon is not defined- name: Define mysql_slow_query_log_file.set_fact:mysql_slow_query_log_file: "{{ __mysql_slow_query_log_file }}"when: mysql_slow_query_log_file is not defined- name: Define mysql_log_error.set_fact:mysql_log_error: "{{ __mysql_log_error }}"when: mysql_log_error is not defined- name: Define mysql_syslog_tag.set_fact:mysql_syslog_tag: "{{ __mysql_syslog_tag }}"when: mysql_syslog_tag is not defined- name: Define mysql_pid_file.set_fact:mysql_pid_file: "{{ __mysql_pid_file }}"when: mysql_pid_file is not defined- name: Define mysql_config_file.set_fact:mysql_config_file: "{{ __mysql_config_file }}"when: mysql_config_file is not defined- name: Define mysql_config_include_dir.set_fact:mysql_config_include_dir: "{{ __mysql_config_include_dir }}"when: mysql_config_include_dir is not defined- name: Define mysql_socket.set_fact:mysql_socket: "{{ __mysql_socket }}"when: mysql_socket is not defined- name: Define mysql_supports_innodb_large_prefix.set_fact:mysql_supports_innodb_large_prefix: "{{ __mysql_supports_innodb_large_prefix }}"when: mysql_supports_innodb_large_prefix is not defined
- roles/tasks/secure-installation.yml: 此文件坑较多, 若用官方的, 会出现没有mysql.user库的报错, 因为存在匿名帐户, 直接用mysql -uroot -p登录的话, 会看到没有默认的mysql库, 但是其实已经有库了, 只是登录时如果不写-h的话, 默认就直接解析到通过hostname了, 在my.cnf里添加skip-name-resolve,可以不通过DNS解析. 所以只能通过mysql -hlocalhost -uroot -p方式都能正确地进入到库, 所以更改如下: [ mysql -NBe 改为: mysql -NB -h localhost -e ]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107---- name: Ensure default user is present.mysql_user:name: "{{ mysql_user_name }}"host: 'localhost'password: "{{ mysql_user_password }}"priv: '*.*:ALL,GRANT'state: presentwhen: mysql_user_name != mysql_root_username# Has to be after the password assignment, for idempotency.- name: Copy user-my.cnf file with password credentials.template:src: "user-my.cnf.j2"dest: "{{ mysql_user_home }}/.my.cnf"owner: "{{ mysql_user_name }}"mode: 0600when: >mysql_user_name != mysql_root_usernameand (mysql_install_packages | bool or mysql_user_password_update)# - name: Disallow root login remotely# command: 'mysql -NBe "{{ item }}"'# with_items:# - DELETE FROM mysql.user WHERE User='{{ mysql_root_username }}' AND Host NOT IN ('localhost', '127.0.0.1', '::1')# changed_when: false- name: Allow root login remotelycommand: 'mysql -NB -h localhost -e "{{ item }}"'with_items:- GRANT ALL PRIVILEGES ON *.* TO '{{ mysql_root_username }}'@'%' IDENTIFIED BY '{{ mysql_root_password }}' WITH GRANT OPTION;changed_when: false- name: Get list of hosts for the root user.command: mysql -NB -h localhost -e"SELECT HostFROM mysql.userWHERE User = '{{ mysql_root_username }}'ORDER BY (Host='localhost') ASC"register: mysql_root_hostschanged_when: falsecheck_mode: falsewhen: mysql_install_packages | bool or mysql_root_password_update# Note: We do not use mysql_user for this operation, as it doesn't always update# the root password correctly. See: https://goo.gl/MSOejW# Set root password for MySQL >= 5.7.x.- name: Update MySQL root password for localhost root account (5.7.x).shell: >mysql -u root -NB -h localhost -e'ALTER USER "{{ mysql_root_username }}"@"{{ item }}"IDENTIFIED WITH mysql_native_password BY "{{ mysql_root_password }}";'with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"when: >((mysql_install_packages | bool) or mysql_root_password_update)and ('5.7.' in mysql_cli_version.stdout or '8.0.' in mysql_cli_version.stdout)# Set root password for MySQL < 5.7.x.- name: Update MySQL root password for localhost root account (< 5.7.x).shell: >mysql -NB -h localhost -e'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}");'with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"when: >((mysql_install_packages | bool) or mysql_root_password_update)and ('5.7.' not in mysql_cli_version.stdout and '8.0.' not in mysql_cli_version.stdout)# Has to be after the root password assignment, for idempotency.- name: Copy .my.cnf file with root password credentials.template:src: "root-my.cnf.j2"dest: "{{ mysql_root_home }}/.my.cnf"owner: rootgroup: rootmode: 0600when: mysql_install_packages | bool or mysql_root_password_update# - name: Get list of hosts for the anonymous user.# command: mysql -NB -h localhost -e 'SELECT Host FROM mysql.user WHERE User = ""'# register: mysql_anonymous_hosts# changed_when: false# check_mode: false# - name: Remove anonymous MySQL users.# mysql_user:# name: ""# host: "{{ item }}"# state: absent# with_items: "{{ mysql_anonymous_hosts.stdout_lines|default([]) }}"- name: Remove anonymous usersmysql_user:name: ''host: 'localhost'host_all: truelogin_user: rootlogin_password: "{{ mysql_root_password }}"login_unix_socket: "{{ mysql_socket }}"state: absent- name: Remove the test databasemysql_db:name: testlogin_user: rootlogin_password: "{{ mysql_root_password }}"login_unix_socket: "{{ mysql_socket }}"state: absent - roles/tasks/replication.yml, 此处注意, 官方脚本没有复制端口, 需要加上master_port, 以防不是默认3306端口:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768---- name: Ensure replication user exists on master.mysql_user:name: "{{ mysql_replication_user.name }}"host: "{{ mysql_replication_user.host | default('%') }}"password: "{{ mysql_replication_user.password }}"priv: "{{ mysql_replication_user.priv | default('*.*:REPLICATION SLAVE,REPLICATION CLIENT') }}"login_user: "{{ mysql_root_username }}"login_password: "{{ mysql_root_password }}"login_unix_socket: "{{ mysql_socket }}"state: presentwhen:- mysql_replication_role == 'master'- mysql_replication_user.name is defined- mysql_replication_master != ''tags: ['skip_ansible_galaxy']- name: Check slave replication status.mysql_replication:mode: getslavelogin_user: "{{ mysql_replication_user.name }}"login_password: "{{ mysql_replication_user.password }}"login_unix_socket: "{{ mysql_socket }}"ignore_errors: trueregister: slavewhen:- mysql_replication_role == 'slave'- mysql_replication_master != ''tags: ['skip_ansible_galaxy']- name: Check master replication status.mysql_replication:mode: getmasterlogin_unix_socket: "{{ mysql_socket }}"delegate_to: "{{ mysql_replication_master }}"register: masterwhen:- (slave.Is_Slave is defined and not slave.Is_Slave) or (slave.Is_Slave is not defined and slave is failed)- mysql_replication_role == 'slave'- mysql_replication_master != ''tags: ['skip_ansible_galaxy']- name: Configure replication on the slave.mysql_replication:mode: changemastermaster_host: "{{ mysql_replication_master }}"master_port: "{{ hostvars[mysql_replication_master].mysql_port }}"master_user: "{{ mysql_replication_user.name }}"master_password: "{{ mysql_replication_user.password }}"master_log_file: "{{ master.File }}"master_log_pos: "{{ master.Position }}"login_unix_socket: "{{ mysql_socket }}"ignore_errors: truewhen:- (slave.Is_Slave is defined and not slave.Is_Slave) or (slave.Is_Slave is not defined and slave is failed)- mysql_replication_role == 'slave'- mysql_replication_user.name is defined- mysql_replication_master != ''- name: Start replication.mysql_replication:mode: startslavelogin_unix_socket: "{{ mysql_socket }}"when:- (slave.Is_Slave is defined and not slave.Is_Slave) or (slave.Is_Slave is not defined and slave is failed)- mysql_replication_role == 'slave'- mysql_replication_master != ''tags: ['skip_ansible_galaxy']
- roles/tasks/uninstall.yml(新增):
123456789101112131415161718192021222324252627---- name: stop mysql serviceservice:name: "{{ mysql_daemon }}"state: stoppedenabled: falsetags: uninstall- name: Uninstall mysql packagespackage:name: "{{ item }}"state: absentwith_items: "{{ mysql_packages }}"tags: uninstall- name: delete mysql related filesfile:path: "{{ item }}"state: absentwith_items:- "{{ mysql_datadir }}"- "{{ mysql_config_file }}"- "{{ mysql_config_include_dir }}.d"- "{{ mysql_slow_query_log_file }}"- "{{ mysql_log_error }}"tags: uninstall
- roles/tasks/
- vars/RedHat-7.yml:
12345678910111213141516---__mysql_daemon: mariadb__mysql_packages:- mariadb- mariadb-server- mariadb-libs- MySQL-python- perl-DBD-MySQL__mysql_slow_query_log_file: /var/log/mariadb/mysql-slow.log__mysql_log_error: /var/log/mariadb/mariadb.log__mysql_syslog_tag: mariadb__mysql_pid_file: /var/run/mariadb/mariadb.pid__mysql_config_file: /etc/my.cnf__mysql_config_include_dir: /etc/my.cnf.d__mysql_socket: /var/lib/mysql/mysql.sock__mysql_supports_innodb_large_prefix: true
- templates/my.cnf.j2:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153# {{ ansible_managed }}[client]#password = your_passwordport = {{ mysql_port }}socket = {{ mysql_socket }}default-character-set = utf8mb4[mysqld]port = {{ mysql_port }}bind-address = {{ mysql_bind_address }}datadir = {{ mysql_datadir }}socket = {{ mysql_socket }}pid-file = {{ mysql_pid_file }}tmpdir = {{ mysql_tmpdir }}lc_messages_dir = /usr/share/mysqllc_messages = en_USskip-external-lockingdefault-time-zone = '+08:00'character_set_server = utf8mb4{% if mysql_skip_name_resolve %}skip-name-resolve{% endif %}{% if mysql_sql_mode %}sql_mode = {{ mysql_sql_mode }}{% endif %}# Logging configuration.{% if mysql_log_error == 'syslog' or mysql_log == 'syslog' %}syslogsyslog-tag = {{ mysql_syslog_tag }}{% else %}{% if mysql_log %}log = {{ mysql_log }}{% endif %}log-error = {{ mysql_log_error }}{% endif %}{% if mysql_slow_query_log_enabled %}# Slow query log configuration.slow_query_log = 1slow_query_log_file = {{ mysql_slow_query_log_file }}long_query_time = {{ mysql_slow_query_time }}{% endif %}{% if mysql_replication_master %}# Replicationserver-id = {{ mysql_server_id }}{% if mysql_replication_role == 'master' %}log_bin = mysql-binlog-bin-index = mysql-bin.indexexpire_logs_days = {{ mysql_expire_logs_days }}max_binlog_size = {{ mysql_max_binlog_size }}binlog_format = {{mysql_binlog_format}}{% for db in mysql_databases %}{% if db.replicate|default(1) %}binlog_do_db = {{ db.name }}{% else %}binlog_ignore_db = {{ db.name }}{% endif %}{% endfor %}{% endif %}{% if mysql_replication_role == 'slave' %}read_onlyrelay-log = relay-binrelay-log-index = relay-bin.index{% if slave_skip_errors %}slave_skip_errors = {{ slave_skip_errors }}{% endif %}{% endif %}{% endif %}# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links = 0# User is ignored when systemd is used (fedora >= 15).user = mysql# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html;performance_schema# Memory settings.key_buffer_size = {{ mysql_key_buffer_size }}max_allowed_packet = {{ mysql_max_allowed_packet }}table_open_cache = {{ mysql_table_open_cache }}sort_buffer_size = {{ mysql_sort_buffer_size }}bulk_insert_buffer_size = {{ mysql_bulk_insert_buffer_size }}read_buffer_size = {{ mysql_read_buffer_size }}read_rnd_buffer_size = {{ mysql_read_rnd_buffer_size }}myisam_sort_buffer_size = {{ mysql_myisam_sort_buffer_size }}thread_cache_size = {{ mysql_thread_cache_size }}{% if '8.0.' not in mysql_cli_version.stdout %}query_cache_type = {{ mysql_query_cache_type }}query_cache_size = {{ mysql_query_cache_size }}query_cache_limit = {{ mysql_query_cache_limit }}{% endif %}max_connections = {{ mysql_max_connections }}max_user_connections = {{ mysql_max_user_connections }}tmp_table_size = {{ mysql_tmp_table_size }}max_heap_table_size = {{ mysql_max_heap_table_size }}group_concat_max_len = {{ mysql_group_concat_max_len }}join_buffer_size = {{ mysql_join_buffer_size }}# Other settings.connect_timeout = {{ mysql_connect_timeout }}wait_timeout = {{ mysql_wait_timeout }}lower_case_table_names = {{ mysql_lower_case_table_names }}event_scheduler = {{ mysql_event_scheduler_state }}myisam_recover_options = BACKUPconcurrent_insert = 2default_storage_engine = InnoDB# InnoDB settings.{% if mysql_supports_innodb_large_prefix and '8.0.' not in mysql_cli_version.stdout %}innodb_large_prefix = {{ mysql_innodb_large_prefix }}innodb_file_format = {{ mysql_innodb_file_format }}{% endif %}innodb_file_per_table = {{ mysql_innodb_file_per_table }}innodb_buffer_pool_size = {{ mysql_innodb_buffer_pool_size }}innodb_log_file_size = {{ mysql_innodb_log_file_size }}innodb_log_buffer_size = {{ mysql_innodb_log_buffer_size }}innodb_flush_log_at_trx_commit = {{ mysql_innodb_flush_log_at_trx_commit }}innodb_lock_wait_timeout = {{ mysql_innodb_lock_wait_timeout }}innodb_strict_mode = 0innodb_open_files = 400innodb_io_capacity = 400innodb_flush_method = O_DIRECT[database]default-character-set = utf8mb4[mysqldump]quickmax_allowed_packet = {{ mysql_mysqldump_max_allowed_packet }}[mysqld_safe]pid-file = {{ mysql_pid_file }}nice = 0{% if mysql_config_include_files | length %}# * IMPORTANT: Additional settings that can override those from this file!# The files must end with '.cnf', otherwise they'll be ignored.#!includedir {{ mysql_config_include_dir }}{% endif %}[galera]binlog_format = row
- defaults/main.yml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138---# If install or uninstall mysqluninstall_mysql: false# Set this to the user ansible is logging in as - should have root# or sudo accessmysql_user_home: /rootmysql_user_name: rootmysql_user_password: root# The default root user installed by mysql - almost always rootmysql_root_home: /rootmysql_root_username: rootmysql_root_password: "Yonyou*123"# Set this to `true` to forcibly update the root password.mysql_root_password_update: truemysql_user_password_update: falsemysql_enabled_on_startup: true# Whether my.cnf should be updated on every run.overwrite_global_mycnf: true# The following variables have a default value depending on operating system.# mysql_config_file: /etc/my.cnf# mysql_config_include_dir: /etc/my.cnf.d# Pass in a comma-separated list of repos to use (e.g. "remi,epel"). Used only# for RedHat systems (and derivatives).mysql_enablerepo: ""# Define a custom list of packages to install; if none provided, the default# package list from vars/[OS-family].yml will be used.# mysql_packages:# - mysql# - mysql-server# - MySQL-python# MySQL connection settings.mysql_port: "3306"mysql_bind_address: '0.0.0.0'mysql_skip_name_resolve: true# slave_skip_errors: 1062:主键冲突 Duplicate entry '%s' for key %dslave_skip_errors: '1062'mysql_datadir: /var/lib/mysqlmysql_tmpdir: "{{ mysql_datadir }}/tmp"mysql_sql_mode: ''# The following variables have a default value depending on operating system.mysql_pid_file: "{{ mysql_datadir }}/mariadb.pid"mysql_socket: "{{ mysql_datadir }}/mysql.sock"# Log file settings.mysql_log_file_group: mysql# Slow query log settings.mysql_slow_query_log_enabled: falsemysql_slow_query_time: "10"# The following variable has a default value depending on operating system.# mysql_slow_query_log_file: /var/log/mysql-slow.log# Memory settings (default values optimized ~512MB RAM).mysql_key_buffer_size: "256M"mysql_max_allowed_packet: "100M"mysql_table_open_cache: "400"mysql_sort_buffer_size: "4M"mysql_bulk_insert_buffer_size: "16M"mysql_read_buffer_size: "2M"mysql_read_rnd_buffer_size: "4M"mysql_myisam_sort_buffer_size: "512M"mysql_thread_cache_size: "128"mysql_query_cache_type: "0"mysql_query_cache_size: "64M"mysql_query_cache_limit: "1M"mysql_max_connections: "1000"mysql_max_user_connections: "1000"mysql_tmp_table_size: "32M"mysql_max_heap_table_size: "32M"mysql_group_concat_max_len: "1024"mysql_join_buffer_size: "262144"# Other settings.mysql_lower_case_table_names: "1"mysql_connect_timeout: "5"mysql_wait_timeout: "31536000"mysql_event_scheduler_state: "OFF"# InnoDB settings.mysql_innodb_file_per_table: "1"# Set .._buffer_pool_size up to 80% of RAM but beware of setting too high.mysql_innodb_buffer_pool_size: "2048M"# Set .._log_file_size to 25% of buffer pool size.mysql_innodb_log_file_size: "64M"mysql_innodb_log_buffer_size: "1024M"mysql_innodb_flush_log_at_trx_commit: "1"mysql_innodb_lock_wait_timeout: "50"# These settings require MySQL > 5.5.mysql_innodb_large_prefix: "1"mysql_innodb_file_format: "barracuda"# mysqldump settings.mysql_mysqldump_max_allowed_packet: "64M"# Logging settings.mysql_log: ""# The following variables have a default value depending on operating system.# mysql_log_error: /var/log/mysql/mysql.err# mysql_syslog_tag: mysqlmysql_config_include_files: []# - src: path/relative/to/playbook/file.cnf# - { src: path/relative/to/playbook/anotherfile.cnf, force: yes }# Databases.mysql_databases: []# - name: example# collation: utf8_general_ci# encoding: utf8# replicate: 1# Users.mysql_users: []# - name: example# host: 127.0.0.1# password: secret# priv: *.*:USAGE# Replication settings (replication is only enabled if master/user have values).mysql_server_id: "1"mysql_max_binlog_size: "100M"mysql_binlog_format: "ROW"mysql_expire_logs_days: "10"mysql_replication_role: ''mysql_replication_master: ''# Same keys as `mysql_users` above.mysql_replication_user: []
- 最后, playbook文件:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647- name: deploy mysql clusterhosts: mysqlpre_tasks:- name: Override variables for MySQL (RedHat).set_fact:mysql_users:- name: replicationhost: '%'password: replication*Yonyou*123#priv: "*.*:ALL"mysql_replication_user:name: replicationhost: '%'password: replication*Yonyou*123#priv: "*.*:ALL"mysql_databases:- name: yonyou_cloudcollation: utf8mb4_unicode_ciencoding: utf8mb4replicate: 1- name: eoscloudcollation: utf8mb4_unicode_ciencoding: utf8mb4replicate: 1- name: disconfcollation: utf8mb4_unicode_ciencoding: utf8mb4replicate: 1- name: pushservercollation: utf8mb4_unicode_ciencoding: utf8mb4replicate: 1- name: serv_metacollation: utf8mb4_unicode_ciencoding: utf8mb4replicate: 1- name: zipkincollation: utf8mb4_unicode_ciencoding: utf8mb4replicate: 1- name: monitorcollation: utf8mb4_unicode_ciencoding: utf8mb4replicate: 1# when: ansible_os_family == "RedHat"roles:- { role: mysql }
hosts.ini里需要如下定义:
123[mysql]172.20.48.227 mysql_replication_role=master mysql_replication_master=172.20.48.227 mysql_server_id=1 mysql_port=3306 mysql_root_password=aa*123 mysql_datadir=/var/lib/mysql ansible_ssh_user=root ansible_ssh_port=22172.20.48.254 mysql_replication_role=slave mysql_replication_master=172.20.48.227 mysql_server_id=2 mysql_port=3306 mysql_root_password=aa*123 mysql_datadir=/var/lib/mysql ansible_ssh_user=root ansible_ssh_port=22
1 Comment
Bill Dabear · 06/11/2020 at 3:57 PM
I just found out it comes out tomorrow