调用角色install_mysql.yml
- hosts: mysql
vars:
- action: install
roles:
- mysql
mysql roles目录结构
main.yml
- name: check mysql port {{ MYSQL_PORT }}
shell:
cmd: |
if ss -nutlp | grep -w ":{{ MYSQL_PORT }} " &> /dev/null;then
echo "Mysql Port Is Up"
else
echo "Mysql Port Is Down"
fi
register: check_result
- name: show check status
debug: var=check_result.stdout_lines
- include: mysql.yml
when: '"Down" in check_result.stdout and action == "install"'
- include: mysql.yml
when: '"Up" in check_result.stdout and action == "uninstall"'
mysql.yml
- name: create {{ ANSIBLE_DIR }}/mysql
file: path={{ ANSIBLE_DIR }}/mysql state=directory
- name: copy install_mysql.sh to {{ ANSIBLE_DIR }}/mysql
copy: src=install_mysql.sh dest={{ ANSIBLE_DIR }}/mysql
- block:
- name: create {{ MYSQL_DATA }}/sql
file: path={{ MYSQL_DATA }}/sql state=directory mode=0755
- name: copy mysqld.cnf to {{ MYSQL_DATA }}
copy: src=mysqld.cnf dest={{ MYSQL_DATA }}
- name: copy rpms.zip to {{ ANSIBLE_DIR }}/mysql
copy: src=rpms.zip dest={{ ANSIBLE_DIR }}/mysql/
when: action == "install"
- name: mysql rpm {{ action }}
shell: cd {{ ANSIBLE_DIR }}/mysql && bash install_mysql.sh {{ action }} {{ MYSQL_DATA }} {{ MYSQL_IP }} {{ MYSQL_PORT }} {{ MYSQL_ADMIN_USER}} {{ MYSQL_ADMIN_PWD }}
register: result
failed_when: result.rc != 0
- name: show excute info
debug: var=result.stdout_lines verbosity=0
install_mysql.sh
#!/bin/bash
<<INFO
AUTHOR:运维@小兵
DATE:2021-10-16
DESCRIBE:rpm方式安装MySQL
SYSTEM:CentOS7/RedHat7
WARNING:
MODIFY:
INFO
set -e
source /etc/profile
WORKDIR=$(cd `dirname $0`;pwd)
echo "Current Excute: bash ${WORKDIR}/$0 $@"
MYSQL_DATA=$2
MYSQL_IP=$3
MYSQL_PORT=$4
MYSQL_ADMIN_USER=$5
MYSQL_ADMIN_PWD=$6
Check_Env(){
echo "INFO:Begin Check Env..."
if ss -nutlp | grep -w ":${MYSQL_PORT} " &> /dev/null;then
echo "INFO:Mysql Is Installed" && exit 0
fi
[[ ! -f ${WORKDIR}/rpms.zip ]] && echo "ERROR:${WORKDIR}/rpms.zip Not Found" && exit 1
if [[ ! -f ${MYSQL_DATA}/mysqld.cnf ]];then
echo "ERROR:${MYSQL_DATA}/mysqld.cnf Not Found" && exit 1
fi
}
Install_Mysql(){
Check_Env
echo "INFO:Begin Install Mysql Server..."
cd ${WORKDIR}
unzip -qo rpms.zip
rpm -ivh ${WORKDIR}/rpms/*.rpm --nodeps --force
[[ ! -f /etc/my.cnf.bak ]] && cp /etc/my.cnf{,.bak}
\cp ${MYSQL_DATA}/mysqld.cnf /etc/my.cnf
systemctl start mysqld
systemctl enable mysqld
}
Modify_Root_Pwd(){
local init_password=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}' | tail -n1)
mysql -uroot -p"${init_password}" --connect-expired-password -e "set password=password('${MYSQL_ADMIN_PWD}')"
mysql -uroot -p"${MYSQL_ADMIN_PWD}" -e "create USER if NOT EXISTS ${MYSQL_ADMIN_USER}@'%' identified by '${MYSQL_ADMIN_PWD}';"
mysql -uroot -p"${MYSQL_ADMIN_PWD}" -e "grant all privileges on *.* to ${MYSQL_ADMIN_USER}@'%' WITH GRANT OPTION;"
mysql -uroot -p"${MYSQL_ADMIN_PWD}" -e "flush privileges;"
if ! mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${MYSQL_ADMIN_USER} -p${MYSQL_ADMIN_PWD} -e 'show databases;' &> /dev/null;then
echo "ERROR:Install Mysql Server Fail" && exit 1
else
echo "INFO:Install Mysql Server Success"
fi
}
Uninstall_Mysql(){
if ! ss -nutlp | grep -w ":${MYSQL_PORT} " &> /dev/null;then
echo "INFO:Mysql Is Uninstalled" && exit 0
else
systemctl stop mysqld
yum remove mysql-community-server -y
rm -rf ${MYSQL_DATA}
rm -rf /var/lib/mysql
rm -f /etc/my.cnf
rm -f /var/log/mysqld.log
if ! ss -nutlp | grep -w ":${MYSQL_PORT} " &> /dev/null;then
echo "INFO:Uninstall Mysql Success"
else
echo "ERROR:Uninstall Mysql Fail" && exit 1
fi
fi
}
[[ $# -ne 6 ]] && echo "ERROR:Invalid Param!!! eg:bash $0 (install|uninstall) mysql数据目录 mysql_ip mysql端口 mysql管理员 mysql管理员密码" && exit 1
case $1 in
install)
Install_Mysql
Modify_Root_Pwd;;
uninstall)
Uninstall_Mysql;;
*)
echo "ERROR:Invalid Param!!! eg:bash $0 (install|uninstall) mysql数据目录 mysql_ip mysql端口 mysql管理员 mysql管理员密码" && exit 1
esac
mysqld.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
plugin-load=validate_password.so
validate-password=OFF
character_set_server=utf8mb4
collation-server=utf8mb4_general_ci
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
max_connections=3000
wait_timeout=3600
transaction_isolation=READ-COMMITTED
innodb_log_buffer_size=167772160
innodb_log_file_size=1024M
innodb_strict_mode=0
max_heap_table_size=512M
max_allowed_packet=67108864
lower_case_table_names=1