工具: Ansible Playbook 安装 Mysql 并配置主从复制


准备工作

主机信息

IP 名称 角色
192.168.114.133 node1 master
192.168.114.134 node2 slave
192.168.114.135 node3 slave

安装ansible

Ansible 是一个自动化运维管理的工具, 可以支持同时管理多台节点,ansible playbook 支持 编写 yml 格式的配置,来管理多个主机节点。

ansible软件 不需要每个节点都安装, 只需要指定其中一个节点作为管理节点即可,在此示例中,我们选择 node1 作为我们的管理节点来安装ansible

自动化安装脚本
  • 脚本安装ansible

  • 脚本配置免密登录

    配置免密登录 首先需要每个服务器的密码, 这里我们三个主机的密码 统一设置成相同密码 配置在文件 ssh_password中

  • 解析 config.ini

配置文件 config.ini
[ssh_password]
halou

[mysql]
192.168.114.133 master
192.168.114.134 slave
192.168.114.135 slave
脚本 auto_install_ansible.sh
#!/bin/bash

#auto insall ansible
#自动化安装ansible软件


CUR_PATH=$(readlink -f $(dirname $0))

#打印日志
function result_echo
{
    if [ $? -eq 0 ]; then 
        echo "$1" | tee -a $CUR_PATH/log.log
    else
        echo "$2" | tee -a $CUR_PATH/log.log
        exit 1
    fi
}

HOST_LIST=`sed -n '//[mysql/]/,//[.*/]/p' ./config.ini | grep -v "/[.*/]" | grep -v ^$ | grep -v ^# | awk '{print $1}'`
SSH_PASSWORD=`sed -n '//[ssh_password/]/,//[.*/]/p' ./config.ini | grep -v "/[.*/]" | grep -v ^$ | grep -v ^# `

yum repolist | grep epel &> /dev/null

if [ $? -ne 0 ];then
    yum install epel-release -y
    yum makecache
fi

yum install sshpass ansible -y

#生成秘钥,并发送给各个节点执行
echo -e "y/n" | ssh-keygen -t rsa -q -N "" -f ~/.ssh/id_rsa


for host in $HOST_LIST;do
    sshpass -p$SSH_PASSWORD ssh-copy-id -o StrictHostKeyChecking=no root@$host &> /dev/null
    ssh -o StrictHostKeyChecking=no root@$host 'ls -al' &> /dev/null

    result_echo "Ok... No password login is success" "Failed...Use no password login failed, please check!!!"
    
done	



安装 mariadb

使用ansible-playbook 编写安装脚本 yaml

注意事项

  • 自定义主机操作列表 hosts 文件

    [mariadb_hosts]
    192.168.114.133 server_id='1' mysql_role=master
    192.168.114.134 server_id='2' mysql_role=salve
    192.168.114.135 server_id='3' mysql_role=salve
    
  • 定义需要应用的变量,位置 根目录/group_vars/all.yml

    MYSQL_DATA_DIR: /www/mysql/data_dir
    MASTER_IP:  192.168.114.133
    
  • 安装脚本主题 auto_install_mariadb.yml

    - hosts: mariadb_hosts
    
      tasks:
        - name: "if repo list exists"
          shell: "yum repolist | grep epel &> /dev/null"
          register: repolist_result
          failed_when: false
          when: mysql_role == "master"
    
        - name: "install epel_release"
          shell: " yum install epel-release -y"
          when: 
            - mysql_role == "master"
            - repolist_result.rc != 0
           
    
        - name: "stop firewalld"
          shell: systemctl stop firewalld && systemctl disable firewalld
        
        - name: setenforce zero
          shell: getenforce | grep Disabled &> /dev/null || setenforce 0
        
        - name: stop SeLinux
          lineinfile: path=/etc/sysconfig/selinux regexp="^SELINUX=" line="SELINUX=disabled"
    
        - name: delete data dir if exists
          shell: "rm -rf {{ MYSQL_DATA_DIR }}"
        
        - name: mkdir data dir
          shell: "[[ -d {{ MYSQL_DATA_DIR }} ]] || mkdir -p {{ MYSQL_DATA_DIR }}"
    
        - name: yum install mariadb
          shell: yum install mariadb mariadb-server -y
    
        - name: set datadir
          lineinfile: path=/etc/my.cnf regexp="^datadir" line="datadir={{ MYSQL_DATA_DIR }}" insertafter="/[mysqld/]"
    
        - name: server_id
          lineinfile: path=/etc/my.cnf line="server_id={{ server_id }}" insertafter="/[mysqld/]"
        
        - name: set master 
          lineinfile: path=/etc/my.cnf regexp="^log-bin" line="log-bin=master-bin" insertafter="^/[mysqld/]"
          when: mysql_role == "master"
        - name: set slave
          lineinfile: path=/etc/my.cnf line="relay-log=slave-log" insertafter="^/[mysqld/]"
          when: mysql_role == "salve"
    
        - name: chown mysql dir
          shell: chown -R mysql:mysql {{ MYSQL_DATA_DIR }} 
    
        - name: restart mariadb
          shell: systemctl restart mariadb
    
        - name: grant slave user
          shell: mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'demo_pass'"
          when: mysql_role == "master"
    
        - name: master bin log name
          shell: "mysql -e /"show master status//G;/" | grep File | awk -F: '{print $2}' | awk '{gsub(/^//s+|//s+$/, /"/");print}' > ~/master_name"
          when: mysql_role == "master"
    
        - name: master bin position
          shell: "mysql -e /"show master status//G;/" | grep Position | awk -F: {'print $2'} | awk '{gsub(/^//s+|//s+$/, /"/");print}' > ~/postion_name"
          
          when: mysql_role == "master"
    
        - name: copy master name
          ansible.builtin.copy:
            src: ~/master_name
            dest: ~/master_name
        - name: copy postion
          ansible.builtin.copy:
            src: ~/postion_name
            dest: ~/postion_name
    
        - name: cat master name
          shell: cat ~/master_name
          register: master_name
    
        - name: cat postion 
          shell: cat ~/postion_name
          register: master_postion
    
        - name: reset slave
          shell: mysql -e "reset slave "
          when: mysql_role == "salve"
        
        - name: change master
          shell: mysql -e "CHANGE MASTER TO master_host='{{ MASTER_IP }}', master_user='slave_user', master_password='demo_pass', master_log_file='{{ master_name.stdout }}',master_log_pos={{ master_postion.stdout }} "
          when: mysql_role == "salve"
         
        
        - name: start slave
          shell: mysql -e "start slave "
          when: mysql_role == "salve"
    

参考资料:

https://github.com/ansible/ansible-examples/blob/master/mongodb/playbooks/testsharding.yml 【ansible 安装mongodb示例】

https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html 【ansible 变量引用】

https://www.cnblogs.com/haloujava/p/16561883.html 【一键安装Mysql并配置主从复制】

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/282782.html

(0)
上一篇 2022年8月29日
下一篇 2022年8月29日

相关推荐

发表回复

登录后才能评论