角色部署MySQL主从

角色部署MySQL主从

准备工作

[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim hosts 
...
[mysql]
node1
node2
[mysql_master]
node1
[mysql_slave]
node2

创建mariadb角色

[root@ansible ansible]# cd roles/
[root@ansible roles]# ansible-galaxy init mariadb
- Role mariadb was created successfully

部署mariadb

编写角色模板playbook

[root@ansible roles]# cd mariadb/
[root@ansible mariadb]# vim tasks/main.yml
[root@ansible mariadb]# cat tasks/main.yml
---
# tasks file for mariadb
- name: stop firewalld
  service:
    name: firewalld
    state: stopped
    enabled: no

- name: stop selinux
  lineinfile:
    path: /etc/selinux/config
    regexp: 'SELINUX='
    line: SELINUX=disabled

- name: stop selinux1
  shell:
    cmd: setenforce 0

- name: set yum
  script: yum.sh

- name: install mariadb
  yum: 
    name: "{{ packages }}"
  vars:
    packages:
    - mariadb-server
    - mariadb

- name: cp config1
  template:
    src: mastermy.cnf.j2
    dest: /etc/my.cnf
  when: inventory_hostname in {{ groups.mysql_master }}

- name: cp config2
  template:
    src: slavemy.cnf.j2
    dest: /etc/my.cnf
  when: inventory_hostname in {{ groups.mysql_master }}

- name: start mariadb
  service:
    name: mariadb
    state: restarted
    enabled: yes

- name: grant for root
  shell:
    cmd: mysql -uroot -e "grant all privileges on *.* to root@'%' identified by 'redhat';"

- name: master
  shell:
    cmd: mysql -uroot -e "grant replication slave on *.* to 'user'@'slave' identified by 'redhat';"
  when: inventory_hostname in {{ groups.mysql_master }}

- name: master
  shell:
    cmd: mysql -uroot -e "change master to master_host='master',master_user='user',master_password='redhat';"
  when: inventory_hostname in {{ groups.mysql_slave }}

- name: start slave
  shell:
    cmd: mysql -uroot -e "start slave;"
  when: inventory_hostname in {{ groups.mysql_slave }}

编写yum.sh脚本

[root@ansible mariadb]# vim files/yum.sh
[root@ansible mariadb]# cat files/yum.sh
#!/bin/bash
rm -rf /etc/yum.repos.d/*
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

编写template文件

[root@ansible mariadb]# vim templates/mastermy.cnf.j2
[root@ansible mariadb]# cat templates/mastermy.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=10
[root@ansible mariadb]# vim templates/slavemy.cnf.j2
[root@ansible mariadb]# cat templates/slavemy.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=20

编写执行playbook

[root@ansible mariadb]# vim mariadb.yml
[root@ansible mariadb]# cat mariadb.yml
---
- name: mariadb
  hosts: mysql
  roles:
    - mariadb

执行

[root@ansible mariadb]# ansible-playbook mariadb.yml
PLAY RECAP **************************************************************************************
node1                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=10   changed=9    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   

验证

[root@node2 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
                Slave_IO_State: Connecting to master
                   Master_Host: master
                   Master_User: user
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: 
           Read_Master_Log_Pos: 4
                Relay_Log_File: mariadb-relay-bin.000001
                 Relay_Log_Pos: 4
         Relay_Master_Log_File: 
              Slave_IO_Running: Connecting
             Slave_SQL_Running: Yes

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值