Ansible部署主从数据库

作用 ip地址 域名解析
软件提供节点httpd 192.168.200.219 无
ansible节点 192.168.200.245 ansible
master 192.168.200.140 node1
slave 192.168.200.252 node2
使用ansible部署主从复制数据库
使用环境:
操作步骤:
先进行基础配置:
在ansible节点:
配置yum源
[root@ansible ~]# vi /etc/hosts
##加入以下两行内容
192.168.200.140 node1
192.168.200.252 node2
##接着给以上两个主机配置免密登录
[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id 192.168.200.140
[root@ansible ~]# ssh-copy-id 192.168.200.252
[root@ansible ~]# cd
[root@ansible ~]# rm -rf /etc/yum.repos.d/*
[root@ansible ~]# curl -O http://192.168.200.219/cscc/iaas/ansible.tar.gz
[root@ansible ~]# tar -xzvf ansible.tar.gz
[root@ansible ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
enabled=1
gpgcheck=0
baseurl=http://192.168.200.219/cscc/iaas/centos/
[ansible]
name=ansible
enabled=1
gpgcheck=0
baseurl=file:///root/ansible
[root@ansible ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo name status
ansible ansible 22
centos centos 4,070
安装ansible
准备template文件
写playbook
repolist: 4,092
[root@ansible ~]# yum install ansible -y
[root@ansible ~]# mkdir /opt/mariadb_ansible
[root@ansible ~]# cp -a /etc/ansible/ansible.cfg /opt/mariadb_ansible/
##配置仅本文件夹内的ansible
[root@ansible ~]# vi /opt/mariadb_ansible/ansible.cfg
inventory = /opt/mariadb_ansible/inventory
roles_path = /opt/mariadb_ansible/roles
host_key_checking = False
[privilege_escalation]
##下方四行取消注释
[root@ansible ~]# vi inventory
##加入以下内容
[mysql]
master
slave
[mysql_master]
master
[mysql_slave]
slave
[root@ansible mariadb_ansible]# vi master.cnf.j2
##写入以下内容
[mysqld]
server-id=1
log_bin=master-bin
log_bin_index=master-bin.index
binlog_do_db=test
[root@ansible mariadb_ansible]# vi slave.cnf.j2
##写入以下内容
[mysqld]
server-id=2
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
[root@ansible mariadb_ansible]# vi db.yaml
---
- hosts: all
tasks:
- name: clear repo
shell:
cmd: rm -rf /etc/yum.repos.d/*
- name: set repo
yum_repository:
file: server
name: centos
baseurl: http://192.168.200.219/cscc/iaas/centos/
enabled: yes
直接执行
gpgcheck: no
description: 111
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
- name: install mariadb
yum:
name:
- mariadb-server
- mariadb
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 for root
shell:
cmd: mysql -uroot -e "grant all privileges on *.* to root@'%' identified by
'12345';"
- name: master
shell:
cmd: mysql -uroot -e "grant replication slave on *.* to 'user'@'slave的ip'
identified by '12345';"
when: inventory_hostname in groups['mysql_master']
- name: slave
shell:
cmd: mysql -uroot -e "change master to master_host='master的
ip',master_user='user',master_password='12345';"
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 mariadb_ansible]# ansible-playbook db.yaml
检查
至此完成
[root@slave ~]# mysql -uroot -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.97
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 536
Relay_Log_File: slave-relay-bin.000004
Relay_Log_Pos: 821
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: Yes
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: 536
Relay_Log_Space: 1400
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Skill_Jay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值