部署环境:系统——rocky.9 ; mysql—

master_slave.yaml :

- name: rocky-主从复制
  hosts: all
  remote_user: root
  tasks:
    - name: "部署MySQL"
      yum:
        name: mysql-server
        state: present
        disable_gpg_check: yes
    - name: "拷贝配置文件"
      template:
        src: ./my.cnf
        dest: /etc/my.cnf
    - name: "启动MySQL"
      service:
        name: mysqld
        state: started
    - include_tasks: master.yaml
      when: role == "master"
      ignore_errors: yes
    - include_tasks: slave.yaml
      when: role == "slave"
master.yaml :
- name: "主库操作"
  script: "master.sh"
- name: "拿取主库信息文件"
  fetch:
    src: /root/master.txt
    dest: /root/
  ignore_errors: yes
slave.yaml :
- name: "拷贝主库信息文件"
  copy:
    src: /root/192.168.58.190/root/master.txt
    dest: /root/master.txt
- name: "修改密码"
  script: "pass.sh"
- name: "配置从库"
  script: "slave.sh"
master.sh :
#!/bin/bash
mysqladmin -uroot -p`awk '/A temporary password/{p=$NF}END{print p}' /var/log/mysqld.log` password "Yuanli@0405" && \
mysql -pYuanli@0405 -e "create user 'slave'@'%' identified by 'Yuanli@0405';" && \
mysql -pYuanli@0405 -e "grant replication slave on *.* to 'slave'@'%';" && \
mysql -pYuanli@0405 -e "flush privileges" && \
mysql -pYuanli@0405 -e "show master status" >/root/master.txt
slave.sh :
#!/bin/bash
cd /root/ && \
logbin=`cat master.txt | awk 'NR==2{print $1}'`
num=`cat master.txt | awk 'NR==2{print $2}'`
cat > slave.txt <<EOF
  CHANGE MASTER TO
MASTER_HOST='192.168.58.190',
MASTER_USER='slave',
MASTER_PASSWORD='Yuanli@0405',
MASTER_PORT=3306,
MASTER_LOG_FILE='$logbin',
MASTER_LOG_POS=${num},
MASTER_CONNECT_RETRY=10;
EOF
sleep 1
cat slave.txt | mysql -pYuanli@0405 && \
mysql -uroot -pYuanli@0405 -e "start slave;"
pass.sh :

#!/bin/bash

pas=`awk '/A temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`

mysqladmin -uroot -p$pas password "Yuanli@0405"

本次项目使用了playbook自动化部署,其中大量使用script代理shell命令;在实现主从复制后可能会出现的一个问题:IO线程2061错误;

解决方案:

主库:重新创建MySQL复制账号,创建账号时指定身份验证方法

—8.0 ; 过程——playbook实现自动化;
mysql> CREATE USER 'repl'@'%‘ identified with mysql_native_password by 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
mysql> FLUSH PRIVILEGES;

从库:stop slave;

reset slave all;
此作者没有提供个人介绍
最后更新于 2024-12-19