自动安装mysql5.7-shell脚本实现与ansible脚本实现
安装包:
mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
环境:centos6.7X64
1、shell脚本安装方法
#!/bin/bash if [ ! -f mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz ];then echo "don't have the file mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz,plse upload" fi echo "1......compress the package." tar zxf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql yum install -y libaio* echo "2.......create the data dir and mysql user grp dir" mkdir -p /data/mysql groupadd mysql useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql chown -R mysql /usr/local/mysql chrp -R mysql /usr/local/mysql chown -R mysql /data/mysql echo "3.......install mysql" rm -rf /data/mysql/* /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql cp -rf /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf cp -rf /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql sed -i 's/^basedir=$/basedir=\/usr\/local\/mysql/g' /etc/init.d/mysql sed -i 's/^datadir=$/datadir=\/data\/mysql/g' /etc/init.d/mysql service mysql start echo "PATH=/usr/local/mysql/bin:$PATH">>/etc/profile source /etc/profile echo "4.......configure the conf" chmod 755 /etc/init.d/mysql chkconfig --add mysql chkconfig --level 345 mysql on
2、ansible脚本
运行命令ansible installmysql5.7.yaml
--- #安装mysql5.7,安装目录/usr/local/mysql5.7,数据目录 /data/mysql - hosts: test remote_user: root gather_facts: no tasks: - name: 1、安装依赖包 yum: name=libaio* - name: 2、拷贝并解压mysql压缩包 unarchive: src=mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz dest=/usr/local - name: 3、重命令mysql目录 shell: rm -rf /usr/local/mysql5.7 && mv /usr/local/mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql5.7 - name: 4、创建data目录/data/mysql file: path="/data/mysql" state=directory - name: 5、创建用户 user: name=user shell=/sbin/nologin - name: 6、改目录权限 file: path=/data/mysql state=directory owner=mysql group=mysql recurse=yes - name: 7、改目录权限 file: path=/usr/local/mysql5.7 state=directory owner=mysql group=mysql recurse=yes - name: 8、清除data目录下 shell: rm -rf /data/mysql/* - name: 9、复制配置文件 shell: cp -rf /usr/local/mysql5.7/support-files/my-default.cnf /etc/my.cnf && cp -rf /usr/local/mysql5.7/support-files/mysql.server /etc/init.d/mysql - name: 10、替换配置文件 lineinfile: path="/etc/init.d/mysql" regexp='^basedir=' line='basedir=/usr/local/mysql5.7' - name: 11、替换配置文件2 lineinfile: path="/etc/init.d/mysql" regexp='^datadir=' line='datadir=/data/mysql' - name: 12、初始化数据库 shell: rm -rf /data/mysql/* && /usr/local/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/data/mysql && /usr/local/mysql5.7/bin/mysql_ssl_rsa_setup --datadir=/data/mysql - name: 13、配置profile环境变量 shell: echo "PATH=/usr/local/mysql5.7/bin:$PATH">>/etc/profile && source /etc/profile - name: 14、替换mysqld_safe中mysqld路径 replace: path='/usr/local/mysql5.7/bin/mysqld_safe' regexp='(/usr/local/mysql)' replace='/usr/local/mysql5.7' # shell: sed -i 's#/usr/local/mysql#/usr/local/mysql5.7#g' /usr/local/mysql5.7/bin/mysqld_safe - name: 15、启动mysql服务 service: name=mysql state=started enabled=yes