Install mysql through Ansible Playbook

Pankaj kushwaha
4 min readJan 13, 2021

--

Introducing
Compile and install mysql-5.6.22 by batch via ansible-playbook and initialize it, and then we just need to start creating the database with the database.

Ideas for Ansible-playbook settings:

1. Configure variables in vars via main.yml, primarily the storage directory for source code and the installation directory

. 2. Using copy.yml in tasks to pass source code files to a remote server’s source code storage directory.

3. Through the Install.yml tasks, the mysql install.sh template is named to install mysql in the installation directory specified in the variable.

4. Call and install the copy module

In Tasks 5 through main.yml. From mysql.yml: mysql install Call the playbook

The directory structure of the playbook
[root@pankaj ansible]# cd /etc/ansible/
[root@pankaj ansible]# mkdir -p roles/mysql_install/{files,handlers,meta,tasks,templates,vars}
[root@pankaj 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

Description:

files: store the source files and configuration files that need to be synchronized to the remote server;

  • handlers: operations that need to be performed when resources change, if there is no such directory, it can be left blank or empty; meta: role definitions can be left blank;
  • tasks: The mysql installation process becomes a task that needs to be executed;
  • templates: template files used to execute mysql installation, generally scripts;
  • vars: variables defined in this installation

Specific operation
1. Create a mysql role file for calling mysql_install

[root@pankaj ansible]# vim mysql.yml
- hosts: pankaj
remote_user: root
gather_facts: False
roles:
— mysql_install

Create a variable file

[root@pankaj 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

Create task file

[root@pankaj ansible]# cd /etc/ansible/roles/mysql_install/tasks
[root@pankaj ansible]# vim copy.yml
- 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@pankaj ansible]# vim install.yml
#Execute template file for installation
- name: install mysql
shell: bash {{source_dir}}/mysql_install.sh
[root@pankaj ansible]# vim main.yml
#Reference copy, install module
- include: copy.yml
- include: install.yml

Note:
a.copy If you copy a directory, you need to add a recursive parameter, recurse;
b.copy if you copy a directory, no directory will be created on the target server;
c.copy if you copy a file to a directory on the target server, you need Add /home/ap/src/ to the dest parameter instead of /home/ap/src, otherwise ansible will copy the file to src instead of putting it in the src directory.

4. Write a template script

#!/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

In addition to compiling and installing mysql, this script also initializes the mysql database, makes corresponding modifications to the configuration file my.cnf whose parameters have been left blank, sets a password, and starts the database.

5. Custom Installation
subsequent according to the actual situation by modifying vars / main.yml relevant parameters for custom installation.

Execute playbook

#Check file
[root@pankaj ansible]# ansible-playbook -C mysql.yml

#Execute playbook
[root@pankaj ansible]# ansible-playbook mysql.yml

More example on practical case of batch mysql reinstallation Ansible

ansible all -m command -a "service mysqld stop"
ansible all -m file -a "dest=/home/mysql state=absent"
ansible all -m file -a "dest=/home/mysql_ex state=absent"
ansible all -m file -a "dest=/home/mysql state=directory"
ansible all -m file -a "dest=/home/mysql/data state=directory"
ansible all -m file -a "dest=/home/mysql /log state=directory"
ansible all -m command -a "chown mysql: -R /home/mysql/"
ansible all -m command -a "mysql_install_db --datadir=/home/mysql/data/ --user=mysql "
ansible all -m command -a "service mysqld start"
ansible all -m command -a "cat /root/.mysql_secret"

I like to learn new and better ways of doing things when working on a scale, and feel free to ask questions and make suggestions.
Also, check out another story on this.
Thanks for reading this.

--

--

Pankaj kushwaha
Pankaj kushwaha

Written by Pankaj kushwaha

Database/System Administrator | DevOPS | Cloud Specialist | DevOPS

No responses yet