码迷,mamicode.com
首页 > 数据库 > 详细

3.4 实例 playbook安装mysql

时间:2015-07-21 23:49:36      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

1.创建mysql的playbook结构

1
2
3
4
5
6
7
8
9
10
11
[root@ansible roles]# pwd
/etc/ansible/roles
[root@ansible roles]# mkdir -p mysql_install/{files,handlers,meta,tasks,templates,vars}
[root@ansible roles]# tree mysql_install/
mysql_install/
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars



2.创建需要用到的vars

1
2
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/vars/main.yml
mysql_version: mysql-5.5.37



3.创建远程安装脚本

 1 [root@ansible roles]# cat /etc/ansible/roles/mysql_install/templates/mysql_install.sh
 2 #!/bin/bash
 3   
 4 DATADIR=/data/mysql/data
 5 VERSION={{mysql_version}}
 6 export LANG=zh_CN.UTF-8
 7   
 8 #Source function library.
 9 . /etc/init.d/functions
10   
11 #camke install mysql5.5.X
12 install_mysql(){
13         #read -p "please input a password for root: " PASSWD
14     PASSWD=ly36843
15         if [ ! -d $DATADIR ];then
16                 mkdir -p $DATADIR
17         fi
18         yum install cmake make gcc-c++ bison-devel ncurses-devel -y
19         id mysql &>/dev/null
20         if [ $? -ne 0 ];then
21                 useradd mysql -s /sbin/nologin -M
22         fi
23         #useradd mysql -s /sbin/nologin -M
24         #change datadir owner to mysql
25         chown -R mysql.mysql $DATADIR
26         cd
27         #wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.38.tar.gz
28         tar xf $VERSION.tar.gz
29         cd $VERSION
30         cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/$VERSION 31         -DMYSQL_DATADIR=$DATADIR 32         -DMYSQL_UNIX_ADDR=$DATADIR/mysql.sock 33         -DDEFAULT_CHARSET=utf8 34         -DDEFAULT_COLLATION=utf8_general_ci 35         -DENABLED_LOCAL_INFILE=ON 36         -DWITH_INNOBASE_STORAGE_ENGINE=1 37         -DWITH_FEDERATED_STORAGE_ENGINE=1 38         -DWITH_BLACKHOLE_STORAGE_ENGINE=1 39         -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 40         -DWITHOUT_PARTITION_STORAGE_ENGINE=1
41         make && make install
42         if [ $? -ne 0 ];then
43                 action "install mysql is failed!"  /bin/false
44                 exit $?
45         fi
46         sleep 2
47         #link
48         ln -s /usr/local/$VERSION/ /usr/local/mysql
49         ln -s /usr/local/mysql/bin/* /usr/bin/
50         #copy config and start file
51         /bin/cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf
52         cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
53         chmod 700 /etc/init.d/mysqld
54         #init mysql
55         /usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql --datadir=$DATADIR --user=mysql
56         if [ $? -ne 0 ];then
57                 action "install mysql is failed!"  /bin/false
58                 exit $?
59         fi
60         #check mysql
61         /etc/init.d/mysqld start
62         if [ $? -ne 0 ];then
63                 action "mysql start is failed!"  /bin/false
64                 exit $?
65         fi
66         chkconfig --add mysqld
67         chkconfig mysqld on
68         /usr/local/mysql/bin/mysql -e "update mysql.user set password=password(‘$PASSWD‘) where host=‘localhost‘ and user=‘root‘;"
69         /usr/local/mysql/bin/mysql -e "update mysql.user set password=password(‘$PASSWD‘) where host=‘127.0.0.1‘ and user=‘root‘;"
70         /usr/local/mysql/bin/mysql -e "delete from mysql.user where password=‘‘;"
71         /usr/local/mysql/bin/mysql -e "flush privileges;"
72         #/usr/local/mysql/bin/mysql -e "select version();" >/dev/null 2>&1
73         if [ $? -eq 0 ];then
74                 echo "+---------------------------+"
75                 echo "+------mysql安装完成--------+"
76                 echo "+---------------------------+"
77         fi
78         #/etc/init.d/mysqld stop
79 }
80   
81 install_mysql

 


4.创建任务
复制文件

1
2
3
4
5
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/tasks/copy.yml 
- name: copy mysql source code to client
  copy: src={{mysql_version}}.tar.gz dest=/root/{{mysql_version}}.tar.gz owner=root group=root
- name: copy mysql install script to client
  template: src=mysql_install.sh dest=/root/mysql_install.sh owner=root group=root mode=0755



解压安装

1
2
3
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/tasks/install.yml
- name: install mysql
  shell: /bin/sh /root/mysql_install.sh



创建包含文件

1
2
3
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/tasks/main.yml
- include: copy.yml
- include: install.yml



最后的playbook结构是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ansible roles]# tree /etc/ansible/roles/mysql_install/
/etc/ansible/roles/mysql_install/
├── files
│   └── mysql-5.5.37.tar.gz
├── handlers
├── meta
├── tasks
│   ├── copy.yml
│   ├── install.yml
│   └── main.yml
├── templates
│   └── mysql_install.sh
└── vars
    └── main.yml



5.创建mysql的playbook配置文件

1
2
3
4
5
6
[root@ansible ~]# cat /etc/ansible/mysql_server_install.yml
- hosts: mysql
  remote_user: root
  gather_facts: False
  roles:
    - mysql_install





3.4 实例 playbook安装mysql

标签:

原文地址:http://www.cnblogs.com/51runsky/p/b8ad4e3343e6514e8de5446cb38a9a78.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!