天天看點

通過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
Mysql_slave1 192.168.2.218
Mysql_slave2 192.168.2.219

具體操作

首先三台遠端主機都已配置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           
#!/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           

繼續閱讀