ansible 部署 mysql
时间: 2024-01-19 12:18:14 浏览: 144
以下是使用Ansible部署MySQL的步骤:
1. 安装Ansible
```shell
pip install ansible==2.9
```
2. 配置Ansible到主机免密登录
在Ansible主机上执行以下命令:
```shell
ssh-keygen -t rsa
ssh-copy-id <mysql_master_ip>
ssh-copy-id <mysql_slave_ip>
```
3. 创建Ansible的inventory文件
在Ansible主机上创建一个名为`inventory.ini`的文件,并添加以下内容:
```ini
[mysql_master]
<mysql_master_ip>
[mysql_slave]
<mysql_slave_ip>
```
4. 创建Ansible的playbook文件
在Ansible主机上创建一个名为`mysql.yml`的文件,并添加以下内容:
```yaml
---
- name: Install MySQL
hosts: mysql_master:mysql_slave
become: true
tasks:
- name: Install MySQL Server
apt:
name: mysql-server
state: present
tags: install
- name: Configure MySQL Master
mysql_replication:
mode: master
master_host: "{{ groups['mysql_master'][0] }}"
master_user: replication
master_password: replication_password
state: present
tags: configure_master
- name: Configure MySQL Slave
mysql_replication:
mode: slave
master_host: "{{ groups['mysql_master'][0] }}"
master_user: replication
master_password: replication_password
state: present
tags: configure_slave
```
5. 执行Ansible playbook
在Ansible主机上执行以下命令:
```shell
ansible-playbook -i inventory.ini mysql.yml
```
请注意,上述步骤中的`<mysql_master_ip>`和`<mysql_slave_ip>`需要替换为实际的MySQL主机和从机的IP地址。
阅读全文