首页 > 数据库 >ansible角色部署mysql主从复制

ansible角色部署mysql主从复制

时间:2022-11-10 20:33:22浏览次数:45  
标签:主从复制 slave name ansible master mysql node4

ansible角色部署mysql主从复制

[root@ansible ansible]# vim hosts 
[mysql]
node1
node4

[mysql_master]
node1

[mysql_slave]
node4

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

[root@ansible mysql]# cat templates/my.cnf.j2 
[mysqld]
log_bin=mysqld
server_id=20

[root@ansible templates]# cat master.cnf.j2 
[mysqld]
log_bin=mysql-bin
server_id=10
[root@ansible templates]# cat slave.cnf.j2 
[myslqd]
log_bin=mysql-bin
server_id=20

[root@ansible mysql]# cat tasks/main.yml 
---
# tasks file for mysql
- name: mount
  mount: 
    src: /dev/cdrom
    path: /mnt
    fstype: iso9660
    state: mounted

- name: clear repo 
  shell: 
    cmd: rm -rf /etc/yum.repos.d.*

- name: set repo1
  yum_repository: 
    file: server 
    name: aa
    description: aa1
    baseurl: file:///mnt/BaseOS
    enabled: yes
    gpgcheck: no


- name: set repo2
  yum_repository: 
    file: server 
    name: bb
    description: bb2
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

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

- name: stop selinux2
  shell: 
    cmd: setenforce 0

- name: install mariadb
  dnf: 
    name: 
      - mariadb
      - mariadb-server
    state: present

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

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

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

- name: grant 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: slave
  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 }}


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

PLAY [mysql] *******************************************************************

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

TASK [mysql : mount] ***********************************************************
ok: [node4]
ok: [node1]

TASK [mysql : clanr repo] ******************************************************
[WARNING]: Consider using the file module with state=absent rather than running
'rm'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [node4]
changed: [node1]

TASK [mysql : set repo1] *******************************************************
ok: [node4]
ok: [node1]

TASK [mysql : set repo2] *******************************************************
ok: [node4]
ok: [node1]

TASK [mysql : stop selinux] ****************************************************
changed: [node4]
ok: [node1]

TASK [mysql : stop selinux2] ***************************************************
changed: [node4]
changed: [node1]

TASK [mysql : install mariadb] *************************************************
ok: [node1]
changed: [node4]

TASK [mysql : cp config] *******************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_master }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_master }}
skipping: [node4]
ok: [node1]

TASK [mysql : cp config2] ******************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
skipping: [node1]
changed: [node4]

TASK [mysql : start mariadb] ***************************************************
changed: [node1]
changed: [node4]

TASK [mysql : grant root] ******************************************************
changed: [node4]
changed: [node1]

TASK [mysql : master] **********************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_master }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_master }}
skipping: [node4]
changed: [node1]

TASK [mysql : slave] ***********************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
skipping: [node1]
changed: [node4]

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

PLAY RECAP *********************************************************************
node1                      : ok=12   changed=5    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   
node4                      : ok=13   changed=9    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

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: node4-relay-bin.000001
                 Relay_Log_Pos: 4
         Relay_Master_Log_File: 
              Slave_IO_Running: Connecting
             Slave_SQL_Running: Yes
               Replicate_Do_DB: 
           Replicate_Ignore_DB: 
            Replicate_Do_Table: 
        Replicate_Ignore_Table: 
       Replicate_Wild_Do_Table: 
   Replicate_Wild_Ignore_Table: 
                    Last_Errno: 0
                    Last_Error: 
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 0
               Relay_Log_Space: 256
               Until_Condition: None
                Until_Log_File: 
                 Until_Log_Pos: 0
            Master_SSL_Allowed: No
            Master_SSL_CA_File: 
            Master_SSL_CA_Path: 
               Master_SSL_Cert: 
             Master_SSL_Cipher: 
                Master_SSL_Key: 
         Seconds_Behind_Master: NULL
 Master_SSL_Verify_Server_Cert: No
                 Last_IO_Errno: 2005
                 Last_IO_Error: error connecting to master 'user@master:3306' - retry-time: 60  maximum-retries: 86400  message: Unknown MySQL server host 'master' (-2)
                Last_SQL_Errno: 0
                Last_SQL_Error: 
   Replicate_Ignore_Server_Ids: 
              Master_Server_Id: 0
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.000 sec)

标签:主从复制,slave,name,ansible,master,mysql,node4
From: https://www.cnblogs.com/loronoa/p/16878658.html

相关文章

  • mysql 设置定时事件
    --查看当前所有事件showevents;--查看定时器功能是否开启showvariableslike'event_scheduler';--开启/关闭定时器功能setglobalevent_scheduler=on/off;-......
  • 如何解决 MySQL 错误:用户root@localhost的访问被拒绝
    介绍大多数MySQL用户遇到ERROR1698(28000):Accessdeniedforuser'root'@'localhost'。当您尝试使用root用户连接到MySQL时,通常会在新安装MySQL时出现此错误消......
  • MySql企业版Percona-Server部署方案
    一.MySql数据库版本说明官方文档:https://www.percona.com/downloads/Percona-Server-LATEST/#自从甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险。漏洞修补和版本升级......
  • MySQL-InnerDB原理
     InnerDB实现原理 它是MySQL从5.5版本卡死的默认的存储引擎,是第一份支持ACID特性的MySQL存储引擎,特点是行锁设计,支持MVCC(多版本并发控制),支持外键......
  • MySQL-数据库优化
     数据库优化: 数据库设计:1.字段选型:数字类型:tinyintsmalintmediumintintbigint字符类型:charvarchar事件类型:datedate......
  • MySQL-InnoDB的MVCC
     InnoDBMVCCInnoDB的MVCC,其实是通过undolog来实现的,可以理解为是通过在每行记录后面保存两个隐藏的列来实现的,分别保存了这个行的创建时间,一个保......
  • MySQL-索引类型优缺点
     MySQL主要集中索引类型:FULLTEXT,HASH,BTREE,RTREE 1.FULLTEXT即为全文索引,目前只有MyISAM支持。不过目前只有CHAR,VARCHAR,TEXT列上可以创建全文索引。......
  • MySQL聚簇索引和非聚簇索引
     聚簇索引和非聚簇索引 在InnoDB中,表数据文件本身就是按B+Tree组织的一个索引结构,聚簇索引就是按照每张表的主键构造一颗B+树,同时叶子节点中存放的就是整张表......
  • mysql多个结果集拼接一行
    五个不同的结果,查询出来,拼接成一行 SELECT*FROM ( SELECT plant_nameasplantName FROMpv_power_plant <iftest="plantId!=nullandplantId!=''"......
  • MySQL的InnerDB和MySAM索引实现
     InnoDB索引实现 InnoDB也使用B+Tree作为索引结构,但具体实现方式却与MyISAM截然不同。InnoDB的主索引:MyISAM索引文件和数据文件是分离的,索引文件仅保......