简介
通过ansible-playbook批量编译安装mysql-5.6.22并进行初始化,后续我们只需要启动数据库进行建库即可。
ansible-playbook配置思路:
1.通过vars中的main.yml配置变量,主要为源码存放目录及安装目录
2.通过tasks中的copy.yml将源码文件传输到异地服务器上源码存放目录
3.通过tasks中的install.yml调用模板mysql_install.sh,将mysql安装到变量中定义的安装目录
4.通过tasks中的main.yml调用copy模块和install模块
5.通过mysql.yml调用剧本(playbook):mysql_install
playbook的目录结构
[root@test ansible]# cd /etc/ansible/
[root@test ansible]# mkdir -p roles/mysql_install/{files,handlers,meta,tasks,templates,vars}
[root@test ansible]# tree /etc/ansible
├── ansible.cfg
├── hosts
├── mysql.yml
├── roles
│ └── mysql_install
│ ├── files
│ │ ├── my.cnf
│ │ └── mysql-5.6.22.tar.gz
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ ├── copy.yml
│ │ ├── install.yml
│ │ └── main.yml
│ ├── templates
│ │ └── mysql_install.sh
│ └── vars
│ └── main.yml
说明:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当资源发生变化时需要进行的操作,若没有此目录可以不建或为空;
meta:角色定义可留空;
tasks:mysql安装过程成需要进行的执行的任务;
templates:用于执行mysql安装的模板文件,一般为脚本;
vars:本次安装定义的变量
具体操作
1.创建mysql角色文件,用于调用mysql_install
[root@test ansible]# vim mysql.yml
- hosts: test
remote_user: root
gather_facts: False
roles:
- mysql_install
2.创建变量文件
#创建变量
[root@test ansible]# cd /etc/ansible/roles/mysql_install/vars
vim mail.yml
mysql_version: mysql-5.6.22
source_dir: /home/ap/src
install_dir: /home/ap/mysql
data_dir: /home/ap/mysql/data
3.创建任务文件
[root@test ansible]# cd /etc/ansible/roles/mysql_install/tasks
[root@test ansible]# vim copy.yml
#复制源码至目标服务器
- name: copy mysql source code to client
copy: src={{mysql_version}}.tar.gz dest={{source_dir}} owner=root group=root
#复制配置文件至目标服务器
- name: copy my.cnf to client
copy: src=my.cnf dest=/etc/my.cnf owner=root group=root
#复制模板文件至目标服务器
- name: copy mysql install script to client
template: src=mysql_install.sh dest={{source_dir}} owner=root group=root mode=0775
[root@test ansible]# vim install.yml
#执行模板文件进行安装
- name: install mysql
shell: bash {{source_dir}}/mysql_install.sh
[root@test ansible]# vim main.yml
#引用copy、install模块
- include: copy.yml
- include: install.yml
注意:
a.copy如果复制目录,需要加上递归参数,recurse;
b.copy如果复制目录,没有目录将会在目标服务器上创建;
c.copy如果复制文件到目标服务器的某一个目录下,需要在dest参数上加上/home/ap/src/,而不是/home/ap/src,否则ansible将会把文件复制为src,而不是放在src目录下。
4.编写模板脚本
#!/bin/bash
INSTALL_DIR={{install_dir}}
DATADIR={{data_dir}}
INNODB_DIR=$DATADIR/innodb
VERSION='{{mysql_version}}'
SOURCE_DIR={{source_dir}}
#export LANG=zh_CN.UTF-8
#Source function library.
. /etc/init.d/functions
#camke install mysql5.6.X
install_mysql(){
#read -p "please input a password for root: " PASSWD
PASSWD='core2017'
if [ ! -d $DATADIR ];then
mkdir -p $DATADIR
fi
if [ ! -d $INNODB_DIR ];then
mkdir -p $INNODB_DIR
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
#useradd mysql -s /sbin/nologin -M
#change datadir owner to mysql
chown -R mysql.mysql $DATADIR
cd $SOURCE_DIR
tar xf $VERSION.tar.gz
cd $VERSION
cmake . -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
-DMYSQL_DATADIR=$DATADIR \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DMYSQL_TCP_PORT=3306
make && make install
if [ $? -ne 0 ];then
action "install mysql is failed!" /bin/false
exit $?
fi
sleep 2
#copy config and start file
#/bin/cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf
#modify /etc/my.cnf
sed -i "s:mysqld =:mysqld = $INSTALL_DIR/bin/mysqld_safe:g" /etc/my.cnf
sed -i "s:mysqladmin =:mysqladmin = $INSTALL_DIR/bin/mysqladmin:g" /etc/my.cnf
sed -i "s:datadir =:datadir = $DATADIR:g" /etc/my.cnf
sed -i "s:slow_query_log_file=:slow_query_log_file=$DATADIR:g" /etc/my.cnf
sed -i "s:log-error=:log-error=$DATADIR:g" /etc/my.cnf
sed -i "s:innodb_data_home_dir =:innodb_data_home_dir = $INNODB_DIR:g" /etc/my.cnf
sed -i "s:innodb_log_group_home_dir =:innodb_log_group_home_dir = $INNODB_DIR:g" /etc/my.cnf
cp $INSTALL_DIR/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
#init mysql
$INSTALL_DIR/scripts/mysql_install_db --basedir=$INSTALL_DIR --datadir=$DATADIR --user=mysql
if [ $? -ne 0 ];then
action "install mysql is failed!" /bin/false
exit $?
fi
#check mysql
/etc/init.d/mysqld start
if [ $? -ne 0 ];then
action "mysql start is failed!" /bin/false
exit $?
fi
chkconfig --add mysqld
chkconfig mysqld on
$INSTALL_DIR/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user='root';"
$INSTALL_DIR/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='127.0.0.1' and user='root';"
#$INSTALL_DIR/bin/mysql -e "delete from mysql.user where password='';"
$INSTALL_DIR/bin/mysql -e "flush privileges;"
#/usr/local/mysql/bin/mysql -e "select version();" >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "+---------------------------+"
echo "+------mysql安装完成--------+"
echo "+---------------------------+"
fi
#/etc/init.d/mysqld stop
#add path
echo "export PATH=$PATH:$INSTALL_DIR/bin" >> /etc/profile
source /etc/profile
}
install_mysql
此脚本除了编译安装mysql,还初始化mysql数据库,对已经参数留空的配置文件my.cnf进行相应的修改,设置密码并启动数据库等系列操作。
5.定制安装
后续可根据实际情况,通过修改vars/main.yml中的相关参数,进行定制安装。
执行playbook
#检查文件
[root@test ansible]# ansible-playbook -C mysql.yml
#执行playbook
[root@test ansible]# ansible-playbook mysql.yml