Ansible-角色部署MySQL主从

本文详细介绍了如何通过Ansible配置和部署MySQL的主从复制。首先,我们配置主机环境,接着创建并调用特定的角色来设定主从关系。在过程中,使用了模板文件进行配置,并且应用了mysqld角色来确保正确设置。最后,通过检查确认MySQL主从复制已经成功建立。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置主机

[root@ansible ~]# cd /etc/ansible/
[mysql]
node4
node5
[my_master]
node4
[my_slave]
node5

创建角色

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

调用角色配置主从

tasks任务
[root@ansible ansible]# vim roles/mysqld/tasks/main.yml 
---
# tasks file for roles/mysqld
- name: set firewalld
  service: 
    name: firewalld
    state: stopped
    enabled: no
- name: stop selinux
  lineinfile:
    path: /etc/sysconfig/selinux
    regexp: '^SELINUX='
    line: 'SELINUX=disabled'
- name: setenforce 0
  shell: setenforce 0
- name: install mariadb
  yum: 
    name: 
      - mariadb-server
      - mariadb
    state: present
- name: copy moban1
  template: 
    src: my_master.cnf.j2
    dest: /etc/my.cnf
  when: inventory_hostname in  {{ groups.my_master }}
- name: copy moban2
  template:
    src: my_slave.cnf.j2
    dest: /etc/my.cnf
  when: inventory_hostname in  {{ groups.my_slave }}
- name: restare mariadb
  service: 
    name: mariadb
    state: started
    enabled: yes
- name: quanxian
  shell: mysql -uroot -e "GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';"
- name: quanxian2
  shell: mysql -uroot -e "GRANT REPLICATION SLAVE ON *.* TO 'sb'@'slave' IDENTIFIED BY '123456';"
  when: inventory_hostname in  {{ groups.my_master }}
- name: quanxian3
  shell: mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='192.168.40.103', MASTER_USER='sb', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;"
  when: inventory_hostname in  {{ groups.my_slave }}
- name: start
  shell: mysql -uroot -e "start slave;"
  when: inventory_hostname in  {{ groups.my_slave }}

模板文件

[root@ansible ~]# vim /etc/ansible/roles/mysqld/templates/my_master.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=66
[root@ansible ~]# vim /etc/ansible/roles/mysqld/templates/my_slave.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=99

调用mysqld角色

[root@ansible ansible]# vim mysql.yml
---
- name: use mysqld
  hosts: mysql
  roles:
    - mysqld

[root@ansible ansible]# ansible-playbook mysql.yml 

PLAY [use mysqld] *******************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [node5]
ok: [node4]

TASK [mysqld : set firewalld] *******************************************************************************
ok: [node4]
ok: [node5]

TASK [mysqld : stop selinux] ********************************************************************************
ok: [node5]
ok: [node4]

TASK [mysqld : setenforce 0] ********************************************************************************
changed: [node4]
changed: [node5]

TASK [mysqld : install mariadb] *****************************************************************************
ok: [node4]
ok: [node5]

TASK [mysqld : copy moban1] *********************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_master }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_master }}
skipping: [node5]
ok: [node4]

TASK [mysqld : copy moban2] *********************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_slave }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_slave }}
skipping: [node4]
ok: [node5]

TASK [mysqld : restare mariadb] *****************************************************************************
changed: [node4]
changed: [node5]

TASK [mysqld : quanxian] ************************************************************************************
changed: [node4]
changed: [node5]

TASK [mysqld : quanxian2] ***********************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_master }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_master }}
skipping: [node5]
changed: [node4]

TASK [mysqld : quanxian3] ***********************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_slave }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_slave }}
skipping: [node4]
changed: [node5]

TASK [mysqld : start] ***************************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_slave }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in  {{ groups.my_slave }}
skipping: [node4]
changed: [node5]

PLAY RECAP **************************************************************************************************
node4                      : ok=9    changed=4    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   
node5                      : ok=10   changed=5    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

查看

[root@node4 ~]# cat /etc/my.cnf
[mysqld]
log_bin=mysql-bin
server_id=66
[root@node5 ~]# cat /etc/my.cnf
[mysqld]
log_bin=mysql-bin
server_id=99

[root@node5 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.3.28-MariaDB-log 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: 192.168.40.103
                   Master_User: sb
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: 
           Read_Master_Log_Pos: 4
                Relay_Log_File: node5-relay-bin.000001
                 Relay_Log_Pos: 4
         Relay_Master_Log_File: 
              Slave_IO_Running: Connecting
             Slave_SQL_Running: Yes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值