在本文中,我們將介紹如何使用Ansible自動化部署MySQL主從。
在開始之前,請確保在您的環境中已經安裝了Ansible。
1. 在主節點上安裝MySQL
---
- hosts: master
tasks:
- name: 安裝MySQL
apt:
name: mysql-server
state: present
become: true
2. 在主節點上創建用于從節點復制的用戶
---
- hosts: master
tasks:
- name: 創建用于從節點復制的用戶
mysql_user:
name: replication
password: "password"
priv: "*.*:REPLICATION SLAVE"
host: "%"
state: present
3. 在主節點上開啟二進制日志
---
- hosts: master
tasks:
- name: 開啟二進制日志
mysql_variables:
name: log_bin
value: ON
become: true
4. 在從節點上安裝MySQL
---
- hosts: slave
tasks:
- name: 安裝MySQL
apt:
name: mysql-server
state: present
become: true
5. 在從節點上設置主節點信息
---
- hosts: slave
tasks:
- name: 設置主節點信息
mysql_replication:
mode: slave
replication_user: replication
replication_password: "password"
master_host: "{{ hostvars['master']['ansible_host'] }}"
master_port: 3306
state: started
become: true
6. 在從節點上開始復制
---
- hosts: slave
tasks:
- name: 開始復制
mysql_replication:
mode: slave
replication_user: replication
replication_password: "password"
master_host: "{{ hostvars['master']['ansible_host'] }}"
master_port: 3306
state: started
become: true
使用以上Ansible Playbook執行部署MySQL主從,可以大大簡化MySQL主從部署的過程。通過自動化,您可以更高效地完成工作,并減少出錯的可能性。
下一篇mysql 全部庫