集群运维自动化工具ansible使用playbook安装mysql
上次介绍了如何使用ansibleplaybook安装zabbix客户端(https://www.nhooo.com/article/52158.htm),这次介绍一下如何使用playbook安装mysql。
下面是安装mysql的信息:
mysql_basedir:/data/mysql/basedir源码目录 mysql_datadir:/data/mysql/datadir数据目录 mysql_user:mysqlmysql用户 mysql_database_user:root数据库用户 mysql_passwd:'E4yR3WnoluSFTCBAI'数据库密码 mysql_port:3306mysql监听端口 mysql_sock:/data/mysql/datadir/mysql.sockmysql的sock mysql_charset:utf8mysql字符集 mysql_collation:utf8_general_cimysql排序方式 mysql_version:Percona-Server-5.5.21-rel25.1.tar.gzmysql版本
下面是安装mysql的playbook结构
10:24:06#treemysql_* mysql_delete ├──files ├──handlers ├──meta │└──main.yml ├──tasks │├──delete.yml │└──main.yml ├──templates └──vars └──main.yml mysql_install ├──files │└──mysql.tar.gz ├──handlers ├──meta │└──main.yml ├──tasks │├──copy.yml │├──delete.yml │├──install.yml │└──main.yml ├──templates │├──install_mysql.sh │├──my.cnf │├──mysqld │└──mysql_security.sh └──vars └──main.yml 12directories,15files
playbook的mysql安装的是
10:32:06#catmysql_install.yml --- -hosts:"{{host}}" remote_user:"{{user}}" gather_facts:True roles: -common -mysql_install
playbook的mysql协助的是
10:32:44#catmysql_delete.yml --- -hosts:"{{host}}" remote_user:"{{user}}" gather_facts:True roles: -mysql_delete
由于在上篇安装zabbix客户端已经介绍了结构,所以现在不介绍了。
10:26:00#timeansible-playbookmysql_install.yml--extra-vars"host=192.168.240.17user=root"--private-key=/root/test.pem PLAY[192.168.240.17]********************************************************* GATHERINGFACTS*************************************************************** ok:[192.168.240.17] TASK:[common|Installinitializtionrequiresoftware]*********************** changed:[192.168.240.17] TASK:[mysql_install|CopyMysqlSoftwareToRedhatClient]****************** changed:[192.168.240.17] TASK:[mysql_install|CreateMysqlUserInRedhatClient]******************** changed:[192.168.240.17] TASK:[mysql_install|CopyMysqlStartScriptToRedhatClient]************** changed:[192.168.240.17] TASK:[mysql_install|CopyInstallMysqlScriptToRedhatClient]*********** changed:[192.168.240.17] TASK:[mysql_install|CopyMysqlConfigToRedhatClient]******************** changed:[192.168.240.17] TASK:[mysql_install|CopyMysqlSecurityScriptToRedhatClient]*********** changed:[192.168.240.17] TASK:[mysql_install|CreateMysqlInstallDir]****************************** ok:[192.168.240.17] TASK:[mysql_install|UncompressionMysqlSoftwareToRedhatClient]********* changed:[192.168.240.17] TASK:[mysql_install|ModifyMysqlDirPermissionInRedhatClient]********** ok:[192.168.240.17]=>(item=/data/mysql/datadir) ok:[192.168.240.17]=>(item=/data/mysql/basedir) TASK:[mysql_install|InstallMysqlScriptInRedhatClient]***************** changed:[192.168.240.17] TASK:[mysql_install|StartMyqlSecurityScriptInRedhatClient]*********** changed:[192.168.240.17] TASK:[mysql_install|AddBootStartMysqlServiceInRedhatClient]********* changed:[192.168.240.17] TASK:[mysql_install|DeleteMysqlcompressionSoftwareInRedhatClient]**** changed:[192.168.240.17] PLAYRECAP******************************************************************** 192.168.240.17:ok=15changed=12unreachable=0failed=0 real2m1.596s user0m8.815s sys0m0.848s
2、安装后测试
登陆192.168.240.17的测试机查看mysql安装情况
[root@ip-10-10-240-21tmp]#ll total16 lrwxrwxrwx1rootroot30Jul1322:27mysql.sock->/data/mysql/datadir/mysql.sock -rw-------1rootroot256Jul1006:07tmp.0PLkgCq81n -rw-------1rootroot197Jul905:35yum_save_tx-2014-07-09-09-35ibcBiO.yumtx -rw-rw-r--1zabbixzabbix320Jul1021:39zabbix_agentd.log -rw-rw-r--1zabbixzabbix5Jul1021:39zabbix_agentd.pid [root@ip-10-10-240-21tmp]#ps-ef|grepmysql root213331022:27?00:00:00/bin/sh/data/mysql/basedir/bin/mysqld_safe--defaults-file=/data/mysql/datadir/my.cnf--datadir=/data/mysql/datadir--pid-file=/data/mysql/datadir/mysql.pid mysql2215621333222:27?00:00:08/data/mysql/basedir/bin/mysqld--defaults-file=/data/mysql/datadir/my.cnf--basedir=/data/mysql/basedir--datadir=/data/mysql/datadir--plugin-dir=/data/mysql/basedir/lib/plugin--user=mysql--log-error=/data/mysql/datadir/mysql-error.log--open-files-limit=10240--pid-file=/data/mysql/datadir/mysql.pid--socket=/data/mysql/datadir/mysql.sock--port=3306 root2244020233022:33pts/000:00:00grepmysql [root@ip-10-10-240-21tmp]#ll/data/mysql/ total8 drwxr-xr-x13mysqlmysql4096Jun2603:59basedir drwxr-xr-x4mysqlmysql4096Jul1322:28datadir [root@ip-10-10-240-21tmp]#ll/etc/init.d/mysqld -rwxr-xr-x1rootroot10905Jul1322:27/etc/init.d/mysqld [root@ip-10-10-240-21tmp]#/etc/init.d/mysqldstatus MySQL(PerconaServer)running(22156)[OK] [root@ip-10-10-240-21tmp]#chkconfig--list|grepmysql mysqld0:off1:off2:on3:on4:on5:on6:off [root@ip-10-10-240-21tmp]#mysql-h192.168.240.17-uroot-pE4yR3WnoluSFTCBAI ERROR1045(28000):Accessdeniedforuser'root'@'192.168.240.17'(usingpassword:YES) [root@ip-10-10-240-21tmp]#mysql-h10.10.240.21-uroot-pE4yR3WnoluSFTCBAI WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis8 Serverversion:5.5.21-logSourcedistribution Copyright(c)2000,2011,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |mysql| |performance_schema| +--------------------+ 3rowsinset(0.00sec) mysql>usemysql Readingtableinformationforcompletionoftableandcolumnnames Youcanturnoffthisfeaturetogetaquickerstartupwith-A Databasechanged mysql>selecthost,user,passwordfromuser; +--------------+--------+-------------------------------------------+ |host|user|password| +--------------+--------+-------------------------------------------+ |10.10.240.21|root|*BE78618CBAFFF409CE17D81579C1678B94439BE1| |%|zabbix|*DEEF4D7D88CD046ECA02A80393B7780A63E7E789| +--------------+--------+-------------------------------------------+ 2rowsinset(0.00sec)
可以看到已经按照我的要求安装完成,下面在进行删除测试。
3、删除
10:49:18#timeansible-playbookmysql_delete.yml--extra-vars"host=192.168.240.17user=root"--private-key=/root/test.pem PLAY[192.168.240.17]********************************************************* GATHERINGFACTS*************************************************************** ok:[192.168.240.17] TASK:[mysql_delete|StopMysqlService]************************************* changed:[192.168.240.17] TASK:[mysql_delete|DeleteMysqlBootStartScript]************************* changed:[192.168.240.17] TASK:[mysql_delete|DeleteMysqlDirAndSocket]**************************** changed:[192.168.240.17] TASK:[mysql_delete|DeleteMysqlUser]************************************** changed:[192.168.240.17] TASK:[mysql_delete|DeleteMysqlServiceStartScript]********************** changed:[192.168.240.17] PLAYRECAP******************************************************************** 192.168.240.17:ok=6changed=5unreachable=0failed=0 real0m25.248s user0m0.632s sys0m0.102s
4、删除后测试
[root@ip-10-10-240-21tmp]#ll/tmp/ total16 -rw-------1rootroot256Jul1006:07tmp.0PLkgCq81n -rw-------1rootroot197Jul905:35yum_save_tx-2014-07-09-09-35ibcBiO.yumtx -rw-rw-r--1zabbixzabbix320Jul1021:39zabbix_agentd.log -rw-rw-r--1zabbixzabbix5Jul1021:39zabbix_agentd.pid [root@ip-10-10-240-21tmp]#ll/data/ total4 drwxr-xr-x3rootroot4096Jul1022:11webroot [root@ip-10-10-240-21tmp]#ps-ef|grepmysql root2295520233022:50pts/000:00:00grepmysql [root@ip-10-10-240-21tmp]#ll/etc/init.d/mysql ls:cannotaccess/etc/init.d/mysql:Nosuchfileordirectory [root@ip-10-10-240-21tmp]#chkconfig--list|grepmysql [root@ip-10-10-240-21tmp]#
可以看到都删除完成。
如果大家想使用我的例子,可以从百度云的地址下载(文件大于50M,没办法放到附件或者51下载中心)地址是http://pan.baidu.com/s/1mgG8jY4,然后放到/etc/ansible目录里,下面是压缩包里的内容
11:20:08#unzip-vmysql_install.zip Archive:mysql_install.zip LengthMethodSizeCmprDateTimeCRC-32Name ---------------------------------------------------- 113Defl:N8921%07-14-201410:32aaed0763mysql_install.yml 99Defl:N8217%07-14-201410:3270c2a028mysql_delete.yml 0Stored00%07-03-201417:2900000000roles/common/ 0Stored00%06-30-201415:5800000000roles/common/meta/ 267Defl:N17833%06-30-201415:5831ee20ecroles/common/meta/main.yml 0Stored00%07-11-201409:3100000000roles/common/tasks/ 586Defl:N26854%07-08-201410:181c0af2a1roles/common/tasks/main.yml 0Stored00%06-19-201413:4300000000roles/common/handlers/ 58Defl:N4817%06-19-201413:438d058053roles/common/handlers/main.yml 0Stored00%06-19-201413:3000000000roles/common/vars/ 0Stored00%06-19-201413:3000000000roles/common/templates/ 0Stored00%06-19-201413:3000000000roles/common/files/ 0Stored00%06-24-201411:2700000000roles/mysql_install/ 0Stored00%07-08-201414:1200000000roles/mysql_install/meta/ 198Defl:N14925%07-08-201414:12b03e00ebroles/mysql_install/meta/main.yml 0Stored00%07-14-201409:2700000000roles/mysql_install/tasks/ 201Defl:N15324%07-14-201409:2751de730broles/mysql_install/tasks/delete.yml 65Defl:N4137%07-14-201409:27a3197ca7roles/mysql_install/tasks/main.yml 1281Defl:N32974%07-14-201409:2773d60454roles/mysql_install/tasks/copy.yml 1162Defl:N37568%07-14-201409:27e5a7341croles/mysql_install/tasks/install.yml 0Stored00%06-19-201413:3000000000roles/mysql_install/handlers/ 0Stored00%07-14-201409:5100000000roles/mysql_install/vars/ 313Defl:N17744%07-14-201409:51bf4476a7roles/mysql_install/vars/main.yml 0Stored00%07-14-201411:1100000000roles/mysql_install/templates/ 518Defl:N24154%07-14-201409:15529052bfroles/mysql_install/templates/install_mysql.sh 10905Defl:N376566%06-26-201414:06e35e7b82roles/mysql_install/templates/mysqld 2122Defl:N82361%07-14-201409:49f91c74abroles/mysql_install/templates/my.cnf 794Defl:N27565%07-03-201413:15560ee63eroles/mysql_install/templates/mysql_security.sh 0Stored00%06-26-201413:5900000000roles/mysql_install/files/ 61805835Defl:N604138462%06-26-201416:18f61e317droles/mysql_install/files/mysql.tar.gz 0Stored00%06-24-201411:2700000000roles/mysql_delete/ 0Stored00%07-08-201414:1200000000roles/mysql_delete/meta/ 197Defl:N14725%07-08-201414:12fe6b0ef8roles/mysql_delete/meta/main.yml 0Stored00%07-14-201409:3900000000roles/mysql_delete/tasks/ 692Defl:N24565%07-14-201409:39dccd57b4roles/mysql_delete/tasks/delete.yml 22Stored220%06-27-201413:30728ce4aaroles/mysql_delete/tasks/main.yml 0Stored00%06-19-201413:3000000000roles/mysql_delete/handlers/ 0Stored00%07-03-201413:1600000000roles/mysql_delete/vars/ 313Defl:N17744%07-14-201410:56bf4476a7roles/mysql_delete/vars/main.yml 0Stored00%06-27-201413:3000000000roles/mysql_delete/templates/ 0Stored00%06-27-201413:3000000000roles/mysql_delete/files/ ------------------------- 61825741604214302%41files