Ansible部署主从数据库
# Ansible部署主从数据库
节点 | IP | 主机名 |
---|---|---|
ansible | 172.25.253.137 | ansible |
master | 172.25.253.140 | master |
slave | 172.25.253.139 | slave |
# 查看当前的文件树
[root@ansible ansible]# tree role/
role/
├── mariadb.yaml
├── master
│ ├── file
│ │ ├── hosts
│ │ └── local.repo
│ ├── tasks
│ │ ├── main.yaml
│ │ ├── mariadb.yaml
│ │ └── master.yaml
│ └── templates
│ ├── server.cnf1.j2
│ └── server.cnf2.j2
└── slave
└── tasks
├── main.yaml
└── slave.yaml
# 配置ansible的hosts
# 1.添加主机清单 变量
[root@ansible ansible]# vim /etc/ansible/hosts
[server]
172.25.253.140 masterip=140
172.25.253.139 slaveip=139
# 2.创建master和slave角色
[root@ansible ~]# mkdir -p ansible/role/{master,slave}
# 创建角色需要的文件夹和文件
# 3.master和slave创建目录
[root@ansible ~]# cd ansible/role/master/
[root@ansible master]# mkdir file tasks templates
[root@ansible ~]# cd ansible/role/slave/
[root@ansible master]# mkdir tasks
# 4.创建master的相关文件
[root@ansible master]# tree file/
file/
├── hosts
├── local.repo
└── server.cnf
[root@ansible master]# tree tasks/
tasks/
├── main.yaml
├── mariadb.yaml
└── master.yaml
[root@ansible master]# tree templates/
templates/
├── server.cnf
├── server.cnf1.j2
└── server.cnf2.j2
# 5.创建slave的task文件
[root@ansible slave]# tree tasks/
tasks/
├── main.yaml
└── slave.yaml
0 directories, 2 files
# playbook的详细介绍
- master角色负责两个节点的配置hosts、配置yum源、安装软件、修改配置文件、授权
- slave角色负责slave节点的加入从节点操作
- 复制hosts和local.repo文件到master角色的file目录
[root@ansible file]# ll
total 12
-rw-r--r-- 1 root root 203 Oct 13 12:26 hosts
-rw-r--r-- 1 root root 76 Oct 12 21:14 local.repo
[root@ansible file]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.253.140 master
172.25.253.139 slave
[root@ansible file]# cat local.repo
[centos]
name=centos
baseurl=http://172.25.253.144/file/centos/
gpgcheck=0
- 查看tasks
[root@ansible tasks]# ll
total 12
-rw-r--r-- 1 root root 49 Oct 12 22:14 main.yaml
-rw-r--r-- 1 root root 653 Oct 13 12:29 mariadb.yaml
-rw-r--r-- 1 root root 329 Oct 13 11:34 master.yaml
# 编写Master的tasks
# 6.编写master的清单列表
[root@ansible tasks]# cat mariadb.yaml
- name: copy hosts
copy: src=./file/hosts dest=/etc/hosts
- name: delete repo
shell: rm -rf /etc/yum.repos.d/*
- name: copy repo
copy: src=./file/local.repo dest=/etc/yum.repos.d/local.repo
- name: install mariadb
yum:
name:
- mariadb
- mariadb-server
state: present
- name: copy config1
template: src=server.cnf1.j2 dest=/etc/my.cnf.d/server.cnf
when: ansible_fqdn == "master"
- name: copy config2
template: src=server.cnf2.j2 dest=/etc/my.cnf.d/server.cnf
when: ansible_fqdn == "slave"
- name: start mariadb
service: name=mariadb state=started
- name: mysql password
shell: mysqladmin -uroot password 000000
[root@ansible tasks]# cat master.yaml
- name: grant privileges
shell: mysql -uroot -p000000 -e " grant all privileges on *.* to root@'%' identified by '000000';"
when: ansible_fqdn == "master"
- name: grant replication
shell: mysql -uroot -p000000 -e " grant replication slave on *.* to 'user'@'slave' identified by '000000';"
when: ansible_fqdn == "master"
[root@ansible tasks]# cat main.yaml
- include: mariadb.yaml
- include: master.yaml
# 使用变量修改两个配置文件
# 7.修改两个配置文件
- 本地安装mariadb,复制/etc/my.cnf.d/server.cnf到templates目录命名为j2的文件
[root@ansible templates]# ll
total 8
-rw-r--r-- 1 root root 816 Oct 12 21:50 server.cnf1.j2
-rw-r--r-- 1 root root 815 Oct 12 21:50 server.cnf2.j2
[root@ansible templates]# cat server.cnf1.j2
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = {{ masterip }}
[root@ansible templates]# cat server.cnf2.j2
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = {{ slaveip }}
# 编写Slave的tasks
# 8.编写slave角色的清单
[root@ansible role]# cd slave/
[root@ansible slave]# ll
total 0
drwxr-xr-x 2 root root 41 Oct 13 11:40 tasks
[root@ansible tasks]# ll
total 8
-rw-r--r-- 1 root root 23 Oct 12 22:18 main.yaml
-rw-r--r-- 1 root root 268 Oct 12 22:23 slave.yaml
# 9.编写清单
[root@ansible tasks]# cat slave.yaml
- name: start slave
shell: mysql -uroot -p000000 -e "change master to master_host='master',master_user='user',master_password='000000';"
when: ansible_fqdn == "slave"
- name: slave
shell: mysql -uroot -p000000 -e "start slave;"
when: ansible_fqdn == "slave"
[root@ansible tasks]# cat main.yaml
- include: slave.yaml
# 编写Role的playbook
# 10.在role的目录下编写剧本
- 该剧本使用role角色调用
[root@ansible role]# ll
total 4
-rw-r--r-- 1 root root 86 Oct 12 22:20 mariadb.yaml
drwxr-xr-x 6 root root 60 Oct 12 21:52 master
drwxr-xr-x 4 root root 31 Oct 13 11:16 slave
[root@ansible role]# cat mariadb.yaml
- hosts: server
remote_user: root
roles:
- role: master
- role: slave
# 11.执行脚本
[root@ansible role]# ansible-playbook mariadb.yaml
PLAY [server] ********************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************
ok: [172.25.253.139]
ok: [172.25.253.140]
TASK [master : copy hosts] *******************************************************************************************
ok: [172.25.253.139]
ok: [172.25.253.140]
·
·
·
PLAY RECAP ***********************************************************************************************************
172.25.253.139 : ok=10 changed=7 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
172.25.253.140 : ok=10 changed=7 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
# 12.检查集群是否正常
[root@ansible role]# ansible server -m shell -a "mysql -uroot -p000000 -e 'show slave status\G'"
172.25.253.140 | CHANGED | rc=0 >>
172.25.253.139 | CHANGED | rc=0 >>
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: master
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 666
Relay_Log_File: mariadb-relay-bin.000004
Relay_Log_Pos: 950
Relay_Master_Log_File: mysql-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: 666
Relay_Log_Space: 1530
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: 140
上次更新: 2023/11/28, 22:03:59