AI智能
改变未来

通过 ansible 部署二进制 mysql 8

通过 ansible 部署二进制 mysql 8

#安装ansible并配置[root@ansible ~]#yum -y install ansible[root@ansible ~]#vim /etc/ansible/hosts[dbservers]10.0.0.18[root@ansible ~]#ansible dbservers --list-hostshosts (1):10.0.0.18[root@ansible ~]#ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:SHA256:LqM4c9HgY483iuh1Qrvit92VT9c17QuRh96phKjLKmk root@ansibleThe key\'s randomart image is:+---[RSA 3072]----+|                 ||                 ||                 ||    .         o .||   o o  S    + oo||  . * ..  o o =.+||   =.*o .+ o = +.|| o+EB.*oo o o o .||+o**==.*.  . . . |+----[SHA256]-----+[root@ansible ~]#[root@ansible ~]#ssh-copy-id 10.0.0.18:/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: \"/root/.ssh/id_rsa.pub\"/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysroot@10.0.0.18\'s password:Number of key(s) added: 1Now try logging into the machine, with:   \"ssh \'10.0.0.18\'\"and check to make sure that only the key(s) you wanted were added.#创建二进制安装所需的文件[root@ansible ~]#mkdir -p /data/ansible/files[root@ansible ~]#ll /data/ansible/files/total 473708-rw-r--r-- 1 root root 485074552 Jul 30 16:48 mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz[root@ansible ~]#vim /data/ansible/files/my.cnf[mysqld]datadir=/data/mysqlskip_name_resolve=1socket=/tmp/mysql.socklog-error=/data/mysql/mysql.logpid-file=/data/mysql/mysql.pid[client]port=3306socket=/tmp/mysql.sock[mysqld_safe]vim /data/ansible/files/secure_mysql.sh#!/bin/bash#!/bin/bashpasswd=`grep \"temporary password\" /data/mysql/mysql.log|sed -nr \'s/^.*\\: (.*)$/\\1/p\'`mysqladmin -uroot -p`echo $passwd` password mageduexpect <<EOFspawn /usr/local/mysql/bin/mysql_secure_installationexpect {\"Enter password for user root:\" {send magedu\\n;exp_continue}\"Press y|Y for Yes, any other key for No:\" {send n\\n;exp_continue}\"Press y|Y for Yes, any other key for No\" {send y\\n;exp_continue}\"Press y|Y for Yes, any other key for No\" {send y\\n;exp_continue}\"Press y|Y for Yes, any other key for No\" {send y\\n;exp_continue}\"Press y|Y for Yes, any other key for No\" {send y\\n;exp_continue}}expect eofEOF[root@ansible ~]#tree /data/ansible/files//data/ansible/files/├── my.cnf├── mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz└── secure_mysql.sh0 directories, 3 files[root@ansible ~]#vim /data/ansible/install_mysql.yml---#insatll mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz- hosts: dbserversremote_user: rootgather_facts: notasks:- name: istall packagesyum: name=mysql,libaio,perl-Data-Dumper,perl-Getopt-Long,expect,ncurses-compat-libs- name: create mysql groupgroup: name=mysql gid=306- name:  create mysql useruser: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql- name: copy tar to remote host and file modeunarchive: src=/data/ansible/files/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz dest=/usr/local/ owner=root group=root- name: create linkfile /usr/local/mysqlfile: src=/usr/local/mysql-8.0.19-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link- name: PATH variableshell: echo \'PATH=/usr/local/mysql/bin:$PATH\' > /etc/profile.d/mysql.sh;source /etc/profile.d/mysql.sh- name: config my.cnfcopy: src=/data/ansible/files/my.cnf  dest=/etc/my.cnf- name: data dirshell: mysqld --initialize --user=mysql --datadir=/data/mysqltags: data- name: service scriptshell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld- name: enable serviceshell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld ontags: service- name: secure scriptscript: /data/ansible/files/secure_mysql.shtags: script#执行mysql.yml[root@ansible ~]#ansible-playbook /data/ansible/install_mysql.ymlPLAY [dbservers] ***************************************************************************TASK [istall packages] *********************************************************************changed: [10.0.0.18]TASK [create mysql group] ******************************************************************changed: [10.0.0.18]TASK [create mysql user] *******************************************************************changed: [10.0.0.18]TASK [copy tar to remote host and file mode] ***********************************************changed: [10.0.0.18]TASK [create linkfile /usr/local/mysql] ****************************************************changed: [10.0.0.18]TASK [PATH variable] ***********************************************************************changed: [10.0.0.18]TASK [config my.cnf] ***********************************************************************changed: [10.0.0.18]TASK [data dir] ****************************************************************************changed: [10.0.0.18]TASK [service script] **********************************************************************changed: [10.0.0.18]TASK [enable service] **********************************************************************changed: [10.0.0.18]TASK [secure script] ***********************************************************************changed: [10.0.0.18]PLAY RECAP *********************************************************************************10.0.0.18                  : ok=11   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0#验证被管理端结果[root@dbserver ~]#mysql -uroot -pmagedumysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor.  Commands end with ; or \\g.Your MySQL connection id is 11Server version: 8.0.19 MySQL Community Server - GPLCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.mysql>
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 通过 ansible 部署二进制 mysql 8