通过ansible批量安装部署mysql

本文是通过ansible-playbook的roles功能实现批量编译安装mysql-5.7.23和初始化。
系统环境

服务器 IP地址 操作系统 所需软件
ansible主机 192.168.2.203 Centos 7 64位 ansible
Mysql_master 192.168.2.217 Centos 7 64位
Mysql_slave1 192.168.2.218 Centos 7 64位
Mysql_slave2 192.168.2.219 Centos 7 64位

具体操作
首先三台远程主机都已配置ssh-key授权,已打通ssh连接通道,并且都能正常ping通。
1、hosts文件添加主机组

[db_server]
192.168.2.217
192.168.2.218
192.168.2.219

2、创建roles目录

cd /etc/ansible/roles/
mkdir -p mysql_install/{default,files,handlers,meta,tasks,templates,vars}

3、下载安装包

cd mysql_install/files/
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23.tar.gz
wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

4、创建关闭selinux的配置模板文件
vim /etc/ansible/roles/mysql_install/templates/config.j2

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
#SELINUXTYPE=targeted

5、创建mysql的配置模板文件
vim /etc/ansible/roles/mysql_install/templates/my.cnf.j2

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
explicit_defaults_for_timestamp=true
datadir=/data/mysql/data
socket=/data/mysql/run/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

log-error=/data/mysql/logs/mysqld.log
pid-file=/data/mysql/run/mysqld.pid
[mysqld_safe]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

6、创建调用roles的playbook作业

cd /etc/ansible/roles/
vim mysql_install.yml
- hosts: db_server
  remote_user: root
  roles:
    - mysql_install

7、创建关闭selinux的tasks作业
vim mysql_install/tasks/selinux.yml

- name: Modify the configuration file for SELinux
  template: src=config.j2 dest=/etc/selinux/config
- name: close selinux
  shell: setenforce 0

8、创建变量vars定义文件
vim mysql_install/vars/main.yml

mysql_version: mysql-5.7.23
boost_version: boost_1_59_0
source_dir: /tmp
install_dir: /data/mysql
data_dir: /data/mysql/data

9、创建文件分发作业
vim mysql_install/tasks/copy.yml

- name: 分发mysql安装包、boost安装包和mysql_install.sh脚本
  copy: src={{ item }} dest={{ source_dir }}
  with_fileglob:
    - /etc/ansible/roles/mysql_install/files/*
- name: 分发mysql配置文件
  template: src=my.cnf.j2 dest=/etc/my.cnf

10、创建mysql安装作业
vim mysql_install/tasks/install.yml

- name: install mysql
  shell: bash {{source_dir}}/mysql_install.sh

11、创建tasks的main.yml文件
vim mysql_install/tasks/main.yml

- include: selinux.yml
- include: copy.yml
- include: install.yml

12、编写mysql安装脚本
vim mysql_install/files/mysql_install.sh

#!/bin/bash
INSTALL_DIR=/data/mysql
DATADIR=/data/mysql/data
BOOST_VERSION='boost_1_59_0'
VERSION='mysql-5.7.23'
SOURCE_DIR=/tmp

#camke install mysql5.7.X
install_mysql(){
     PASSWD='123456'
        if [ ! -d $DATADIR ];then
                mkdir -p $INSTALL_DIR/{data,run,logs}
        fi
        yum install cmake make gcc gcc-c++  ncurses-devel bison-devel -y
        id mysql &>/dev/null
        if [ $? -ne 0 ];then
                useradd mysql -s /sbin/nologin -M
        fi
        chown -R mysql:mysql $INSTALL_DIR
        cd $SOURCE_DIR
        tar zxvf $BOOST_VERSION.tar.gz
        tar zxvf $VERSION.tar.gz
        cd $VERSION
        cmake . -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
        -DMYSQL_DATADIR=$DATADIR \
        -DMYSQL_UNIX_ADDR=$INSTALL_DIR/run/mysql.sock \
        -DDEFAULT_CHARSET=utf8 \
        -DDEFAULT_COLLATION=utf8_general_ci \
        -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_MYISAM_STORAGE_ENGINE=1 \
        -DWITH_PARTITION_STORAGE_ENGINE=1 \
        -DWITH_BOOST=$SOURCE_DIR/$BOOST_VERSION \
        -DENABLED_LOCAL_INFILE=1 \
        -DEXTRA_CHARSETS=all

        make -j `grep processor /proc/cpuinfo | wc -l`
        make install
        if [ $? -ne 0 ];then
                echo "install mysql is failed!"
                exit $?
        fi
        sleep 2

        #MySQL initialization and startup
        cp -p $INSTALL_DIR/support-files/mysql.server /etc/init.d/mysqld
        if [ -d $INSTALL_DIR/logs ];then
            touch $INSTALL_DIR/logs/mysqld.log
            chown -R mysql:mysql $INSTALL_DIR/logs/mysqld.log
        else
            echo "No logs directory and mysqld.log file!"
            exit $?
        fi
        chown -R mysql:mysql $DATADIR
        rm -f $DATADIR/*
        $INSTALL_DIR/bin/mysqld --initialize --basedir=$INSTALL_DIR --datadir=$DATADIR --user=mysql
        /etc/init.d/mysqld start
        if [ $? -ne 0 ];then
                echo "mysql start is failed!"
                exit $?
        fi
        chkconfig --add mysqld
        chkconfig mysqld on
        root_pass=`grep 'temporary password' $INSTALL_DIR/logs/mysqld.log | awk '{print $11}'`
        $INSTALL_DIR/bin/mysql --connect-expired-password -uroot -p$root_pass -e "alter user 'root'@'localhost' identified by '$PASSWD';"
        if [ $? -eq 0 ];then
                echo "+---------------------------+"
                echo "+------mysql安装完成--------+"
                echo "+---------------------------+"
        fi
        #add path
        echo "export PATH=$PATH:$INSTALL_DIR/bin" >> /etc/profile
        source /etc/profile
}
install_mysql

mysql的最终角色路径如下

整个playbook作业主要实现关闭selinux、推送mysql配置、安装mysql和初始化mysql。
13、运行mysql_install.yml作业
检查playbook作业
ansible-playbook -C mysql_install.yml
运行playbook作业
ansible-playbook mysql_install.yml
安装时间比较久,大概半个小时左右,可以喝杯茶,顺便撩撩旁边的妹子,哈哈。在节点机器上面明显可以看到mysql_install.sh脚本在运行,请耐心等待。

以上整个项目脚本下载地址:
http://down.51cto.com/data/2452677

转载于:https://blog.51cto.com/andyxu/2157060

你可能感兴趣的:(通过ansible批量安装部署mysql)