1、编辑mysql.sh脚本
vim mysql.sql
【
#/bin/bash
#脚本安装 mysql,上传安装包至 /root
cd /root
#安装日志
mysql_log=/root/mysql.log
# mysql 安装包名
mysql_package=mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
# 解压 mysql 安装包后文件夹名称
mysql_package_dir=mysql-8.0.19-linux-glibc2.12-x86_64
#存放数据库的目录
mysql_dbdata=/usr/local/mysql/data
#centos6 检查 mysql
if [ `rpm -qa | grep -i mysql | wc -l ` -ge 1 ]
then
rpm -qa | grep -i mysql >> uninstall_mysql.txt
file=$(cat uninstall_mysql.txt)
for name in $file
do
rpm -e --nodeps $name
done
fi
rm -rf uninstall_mysql.txt
#centos7 检查 mariadb
if [ `rpm -qa | grep -i mariadb | wc -l ` -ge 1 ]
then
rpm -qa | grep -i mariadb >> uninstall_mariadb.txt
file=$(cat uninstall_mariadb.txt)
for name in $file
do
rpm -e --nodeps $name
done
fi
rm -rf uninstall_mariadb.txt
#添加 mysql 用户
useradd -r -s /sbin/nologin mysql
#解压 mysql_package 至 /usr/local
tar -xvf $mysql_package -C /usr/local/
cd /usr/local
#创建软连接
ln -s $mysql_package_dir mysql
#创建 mysql 数据存放目录
mkdir $mysql_dbdata
#修改文件夹所属者
chown -R mysql:mysql mysql
chown -R mysql:mysql mysql/
#添加环境变量路径,使生效
echo PATH=/usr/local/mysql/bin:$PATH > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
#生成数据库,默认生成密码打印在屏幕上,登录时会用到,记得保存
cd /usr/local/mysql
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=$mysql_dbdata >> $mysql_log
#创建RSA private key
bin/mysql_ssl_rsa_setup --datadir=$mysql_dbdata
#生成配置文件
if [ -e /etc/my.cnf ]
then
mv /etc/my.cnf /etc/my.cnf.bak
rm -rf /etc/my.cnf
fi
touch /etc/my.cnf
echo "[client]" >> /etc/my.cnf
echo "port = 3306" >> /etc/my.cnf
echo "default-character-set=utf8" >> /etc/my.cnf
echo "[mysqld]" >> /etc/my.cnf
echo "character_set_server=utf8" >> /etc/my.cnf
echo "init_connect='SET NAMES utf8'" >> /etc/my.cnf
echo "basedir=/usr/local/mysql" >> /etc/my.cnf
echo "datadir=$mysql_dbdata" >> /etc/my.cnf
echo "socket=/tmp/mysql.sock" >> /etc/my.cnf
echo "#登录密码报错,取消屏蔽后重启可不输入密码进入mysql" >> /etc/my.cnf
echo "#skip-grant-tables" >> /etc/my.cnf
echo "[mysqld_safe]" >> /etc/my.cnf
echo "lower_case_table_names = 1" >> /etc/my.cnf
echo "log-error=/var/log/mysqld.log" >> /etc/my.cnf
echo "pid-file=/var/run/mysqld/mysqld.pid" >> /etc/my.cnf
#设置启动服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#添加启动服务
chkconfig --add mysqld
】
2、编辑mysql.yml剧本
【
---
- hosts: s103
remote_user: root
tasks:
- name: copy tar
copy: src=/data/mysql/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz dest=/root/
- name: copy sh
copy: src=/data/ansible/playbook/mysql.sh dest=/root/
- name: install
script: /data/ansible/playbook/mysql.sh
- name: start mysql
service: name=mysql state=started
】
3、验证
ansible-playbook mysql.yml
被控端 192.168.59.103,mysql服务正常启动,mysql可以登陆
标签:二进制,cnfecho,etc,ansible,usr,mysql,--,my From: https://www.cnblogs.com/leihongnu/p/16737481.html