未分类
ansible安装mariadb
最近在搞mariadb的高可用安装, 直接使用的官方ansible, 坑太多了, 现在记录下来 官方脚本下载: ansible-galaxy install geerlingguy.mysql 需要修改的地方: roles/tasks/main.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
--- # Variable configuration. - include_tasks: variables.yml # Setup/install tasks. - include_tasks: setup-RedHat.yml when: ansible_os_family == 'RedHat' and not uninstall_mysql - include_tasks: setup-Debian.yml when: ansible_os_family == 'Debian' and not uninstall_mysql - include_tasks: setup-Archlinux.yml when: 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 true include_tasks: "{{ item }}" with_items: - configure.yml - secure-installation.yml - databases.yml - users.yml - replication.yml when: not uninstall_mysql - name: Uninstall mariadb include: uninstall.yml when: uninstall_mysql |
roles/tasks/configure.yml: 如果要自定义一些目录的话, 需要创建目录并修改文件夹权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
--- - name: Get MySQL version. command: 'mysql --version' register: mysql_cli_version changed_when: false check_mode: false - name: Copy my.cnf global MySQL configuration. template: src: my.cnf.j2 dest: "{{ mysql_config_file }}" owner: root group: root mode: 0644 force: "{{ overwrite_global_mycnf }}" notify: restart mysql - name: Verify mysql include directory exists. file: path: "{{ mysql_config_include_dir }}" state: directory owner: root group: root mode: 0755 when: 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: root group: root mode: 0644 force: "{{ 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 exist file: path: "{{ mysql_datadir }}" state: directory owner: mysql group: mysql mode: 0755 setype: mysqld_db_t - name: Set ownership on slow query log file (if configured). file: path: "{{ mysql_slow_query_log_file }}" state: file owner: mysql group: "{{ mysql_log_file_group }}" mode: 0640 when: 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: file owner: mysql group: "{{ mysql_log_file_group }}" mode: 0640 when: - mysql_log == "" - mysql_log_error != "" tags: ['skip_ansible_galaxy'] - name: Create socket/pid directory shell: > mysql_pid_file={{ mysql_pid_file }}; mysql_socket={{ mysql_socket }}; for file in `echo $mysql_pid_file $mysql_socket`; do dir=${file%/*}; if [[ ! -d $dir ]]; then mkdir -p ${dir} chown mysql:mysql ${dir} chmod 0755 ${dir} else echo "$dir already exist" fi done - name: Create tmpdir if it does not exist file: path: "{{ mysql_tmpdir }}" state: directory owner: mysql group: mysql mode: 0777 - name: init mysql db shell: > 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: 注意修改默认字符集
1 2 3 4 5 6 7 8 9 10 11 |
--- - 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
--- # 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 ] Read more…