brief introduction
_install mysql-5.6.22 through ansible-playbook batch compilation and initialization, then we only need to start the database to build the database.
ansible-playbook configuration idea:
1. Configure variables through main.yml in vars, mainly source code storage directory and installation directory.
2. Transfer source files to source storage directories on remote servers through copy.yml in tasks
3. Install MySQL into the installation directory defined in the variable by calling the template mysql_install.sh through install.yml in tasks
4. Call copy module and install module through main.yml in tasks
5. Call the script (playbook) through mysql.yml: mysql_install
Directory structure of 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
Explain:
Files: Store source files and configuration files that need to be synchronized to remote servers.
handlers: Operations needed when resources change. If there is no directory, it can be built or empty.
meta: role definition can be left blank;
tasks: The mysql installation process becomes the task that needs to be executed;
templates: Template files used to execute mysql installation, usually scripts;
vars: Variables defined for this installation
Specific operation
1. Create MySQL role file for calling mysql_install
[root@test ansible]# vim mysql.yml
- hosts: test
remote_user: root
gather_facts: False
roles:
- mysql_install
2. Create variable files
#Create variables
[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. Create task files
[root@test ansible]# cd /etc/ansible/roles/mysql_install/tasks
[root@test ansible]# vim copy.yml
#Copy source code to target server
- name: copy mysql source code to client
copy: src={{mysql_version}}.tar.gz dest={{source_dir}} owner=root group=root
#Copy the configuration file to the target server
- name: copy my.cnf to client
copy: src=my.cnf dest=/etc/my.cnf owner=root group=root
#Copy the template file to the target server
- 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
#Execute template files for installation
- name: install mysql
shell: bash {{source_dir}}/mysql_install.sh
[root@test ansible]# vim main.yml
#Reference to copy and install modules
- include: copy.yml
- include: install.yml
Be careful:
If a.copy copies a directory, it needs to add a recursive parameter, recurse.
b.copy If the directory is copied, no directory will be created on the target server;
c.copy If you copy a file to a directory of the target server, you need to add / home/ap/src /, not / home/ap/src /, to the dest parameter, otherwise ansible will copy the file as src, not in the SRC directory.
4. Writing template scripts
#!/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 installation is complete--------+"
echo "+---------------------------+"
fi
#/etc/init.d/mysqld stop
#add path
echo "export PATH=$PATH:$INSTALL_DIR/bin" >> /etc/profile
source /etc/profile
}
install_mysql
This script not only compiles and installs mysql, but also initializes MySQL database, modifies my.cnf, sets password and starts a series of operations such as database.
5. Customized Installation
Subsequently, according to the actual situation, we can customize the installation by modifying the relevant parameters in vars/main.yml.