Java互联网科技

CentOS 7使用ansible批量部署mysql-5.7

2020-05-15  本文已影响0人  Java码农石头

1.环境准备

准备3台新装的虚拟机,部署服务器的内存至少大于2G,否则编译时可能会报错。

2.Ansible-Playbook配置思路

files中的mysql_install.sh为源码安装mysql的执行脚本

tasks中的copy.yml为复制所需文件至目标服务器上

tasks中的install.yml调用mysql_install.sh脚本来执行,将mysql安装到变量定义中的路径下

tasks中的main.yml调用copy模块和install模块

templates中的my.cnf.j2为mysql的主配置文件

mysql_install.yml调用剧本:mysql_install

目录结构

├── hosts

├── mysql_install.yml

└── roles

└── mysql_install

├── default

├── files

│ ├── mysql-boost-5.7.30.tar.gz

│ └── mysql_install.sh

├── handlers

├── meta

├── tasks

│ ├── copy.yml

│ ├── install.yml

│ └── main.yml

├── templates

│ └── my.cnf.j2

└── vars

└── main.yml

3.关闭防火墙和selinux

~]# systemctl stop firewalld

~]# systemctl disable firewalld

~]# setenforce 0

~]# vi /etc/selinux/config

SELINUX=disabled

4.安装Ansible

更换yum源,安装ansible

~]# yum -y install wget

~]# rm -rf /etc/yum.repo.d/*

~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

~]# yum -y install ansible

5.下载带有boost的mysql-5.7.30.tar.gz

~]# wget -P /tmp https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.30.tar.gz

6.设置hosts解析,并复制给其它服务器

~]# vi /etc/hosts

192.168.55.105 ansible

192.168.55.171 master

192.168.55.102 slave

~]# scp /etc/hosts root@192.168.55.171:/etc

~]# scp /etc/hosts root@192.168.55.102:/etc

7.修改hosts

~]# cd /etc/ansible

ansible]# vi hosts

[db_server]

master

slave

8.新建mysql_install.yml

ansible]# vi mysql_install.yml

- hosts: db_server

  remote_user: root

  roles:

    - mysql_install

9.roles/mysql_install/vars/main.yml

mysql_boost: mysql-boost-5.7.30

mysql_version: mysql-5.7.30

source_dir: /tmp

install_dir: /usr/local/mysql

data_dir: /data/mysql

10.roles/mysql_install/templates/my.cnf.j2

[mysqld]

basedir=/usr/local/mysql

datadir=/data/mysql

log-error=/data/mysql/mysql.log

pid-file=/usr/local/mysql/mysql.pid

socket=/usr/local/mysql/mysql.sock

11.roles/mysql_install/tasks/copy.yml

- name: copy mysql items

  copy: src={{item}} dest={{source_dir}}

  with_items:

    - mysql-boost-5.7.30.tar.gz

    - mysql_install.sh

- name: copy my.cnf

  template: src=my.cnf.j2 dest=/etc/my.cnf

12.roles/mysql_install/tasks/install.yml

- name: install mysql

  shell: /bin/bash {{source_dir}}/mysql_install.sh

13.roles/mysql_install/tasks/main.yml

- include: copy.yml

- include: install.yml

14.roles/mysql_install/files/mysql_install.sh

#!/bin/bash

INSTALL_DIR=/usr/local/mysql

DATADIR=/data/mysql

MYSQL_BOOST='mysql-boost-5.7.30'

MYSQL_VERSION='mysql-5.7.30'

SOURCE_DIR=/tmp

#cmake install mysql5.7

install_mysql(){

        PASSWD='(Ellay123)'

        if [ ! -d $INSTALL_DIR ];then

                mkdir -p $INSTALL_DIR

        fi

        if [ ! -d $DATADIR ];then

                mkdir -p $DATADIR

        fi

        yum -y install cmake make gcc gcc-c++ ncurses-devel bison-devel openssl-devel

        egrep "^mysql" /etc/group >&/dev/null

        if [ $? -ne 0 ];then

                groupadd mysql

        fi

        id mysql >&/dev/null

        if [ $? -ne 0 ];then

                useradd -r -g mysql -s /sbin/nologin mysql

        fi

        cd $SOURCE_DIR

        tar xf $MYSQL_BOOST.tar.gz

        cd $MYSQL_VERSION

        cmake . -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \

        -DMYSQL_DATADIR=$DATADIR \

        -DSYSCONFDIR=/etc \

        -DWITH_BOOST=$SOURCE_DIR/$MYSQL_VERSION/boost

        if [ $? -ne 0 ];then

                echo "cmake failes!"

                exit $?

        fi

        make

        if [ $? -ne 0 ];then

                echo "make mysql failed!"

                exit $?

        fi

        make install

        if [ $? -ne 0 ];then

                echo "install mysql failed!"

                exit $?

        fi

        sleep 2

#mysql installization and startup

        chown -R mysql:mysql $INSTALL_DIR

        chown -R mysql:mysql $DATADIR

        cp -p $INSTALL_DIR/support-files/mysql.server /etc/init.d/mysqld

        chkconfig --add mysqld

        chkconfig mysqld on

        rm -rf $DATADIR/*

        $INSTALL_DIR/bin/mysqld --defaults-file=/etc/my.cnf --initialize --basedir=$INSTALL_DIR --datadir=$DATADIR --user=mysql

        echo "export PATH=$PATH:$INSTALL_DIR/bin" >> /etc/profile

        source /etc/profile

        /etc/init.d/mysqld start

        if [ $? -ne 0 ];then

                source /etc/profile

                /etc/init.d/mysqld start

                echo "mysql start failed!"

                exit $?

        fi

        passwd=`grep "temporary password" $DATADIR/mysql.log | awk '{print $NF}'`

        mysqladmin -uroot -p"$passwd" password "$PASSWD"

}

install_mysql

15.检查playbook并执行

~]# ansible-playbook -C /etc/ansible/mysql_install.yml

#检查无报错执行playbook

~]# ansible-playbook /etc/ansible/mysql_install.yml

上一篇下一篇

猜你喜欢

热点阅读