098098 发表于 2015-11-26 09:07:31

ansible 自动化安装 mysql

ansible 安装mysql
一、首先下载mysql二进制安装包(我使用的是二进制包安装的mysql)
下载地址;http://dev.mysql.com/downloads/mysql/
我使用的是mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz
1、整合
#cd /usr/local/src
#tar xf mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz
#mv mysql-5.6.19-linux-glibc2.5-x86_64 mysql
再次打包
#tar -zcf mysql-5.6.19.tar.gz mysql/
#mv mysql-5.6.19.tar.gz /tmp
2、创建目录并配置yml文件:
#cd /etc/ansible/roles
#mkdir -pv mysql_install/{files,meta,tasks,templates,vars}
#cd mysql_install
#mv /tmp/mysql-5.6.19.tar.gzfiles/

meta目录:
#cat meta/main.yml
galaxy_info:
author: liguang xing
description: Install Mysql
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
    versions:
    - 6
categories:
- Service
dependencies: []

tasks目录:
# cat tasks/copy.yml
- name: copy mysql software to in client
copy: src=mysql-5.6.19.tar.gz dest=/tmp/mysql-5.6.19.tar.gz owner=root group=root
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: create mysql user Inclient
user: name={{ mysql_user }} state=present createhome=no shell=/sbin/nologin
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and ansible_distribution_version|int >=6
- name: Copy Mysql Start Script To Redhat Client
template: src=mysqld dest=/etc/init.d/mysqld owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Copy Install Mysql ScriptTo Redhat Client
template: src=install_mysql.sh dest=/tmp/ owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Copy Mysql Config To Redhat Client
template: src=my.cnf dest=/tmp/ owner=root group=root mode=0644
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Copy Mysql Security Script To Redhat Client
template: src=mysql_security.sh dest=/tmp/ owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat tasks/delete.yml
- name: Delete Mysql compression Software In Redhat Client
shell: rm -f /tmp/mysql-5.6.19.tar.gz /tmp/mysql_security.sh
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat tasks/install.yml
- name: Create Mysql Install Dir
file: dest=/data/mysql state=directory
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Uncompression Mysql Software To Redhat Client
shell: tar zxf /tmp/mysql-5.6.19.tar.gz -C /usr/local/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Modify Mysql Dir Permission In Redhat Client
file: path={{ item }} owner={{ mysql_user }} group={{ mysql_user }} mode=0755
with_items:
    - "{{ mysql_datadir }}"
    - "{{ mysql_basedir }}"
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Install Mysql Script In Redhat Client
shell: /bin/bash /tmp/install_mysql.sh
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Start Myql Security Script In Redhat Client
shell: /bin/bash /tmp/mysql_security.sh
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Add Boot Start Mysql Service In Redhat Client
shell: chkconfig --add mysqld;chkconfig mysqld on
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat tasks/main.yml
- include: copy.yml
- include: install.yml
- include: delete.yml

templates目录:
#cat templates/install_mysql.sh
#!/bin/bash
mv /tmp/my.cnf /etc/my.cnf
chown -R {{ mysql_user }}:{{ mysql_user }} {{ mysql_datadir }} {{ mysql_basedir }}
####init mysql DB##
cd {{ mysql_basedir }}
./scripts/mysql_install_db --defaults-file=/etc/my.cnf --datadir={{ mysql_datadir }} --user={{ mysql_user }} >> /dev/null 2>&1 &

sleep 3
service mysqld start
##add mysql env##
echo 'MANPATH/usr/local/mysql/man' >> /etc/man.config
ln -sv /usr/local/mysql/include /usr/include/mysql
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig

chkconfig --add mysqld
chkconfig mysqld on
rm -f /tmp/$(basename $0)

mysql的配置文件my.cnf:
# cat templates/my.cnf

port    = {{ mysql_port }}
socket= {{ mysql_sock }}


character-set-server = utf8
port      = {{ mysql_port }}
socket    = {{ mysql_sock }}
datadir   = {{ mysql_datadir }}
log-error = {{ mysql_datadir }}/mysql-error.log
pid-file= {{ mysql_datadir }}/mysql.pid
log-bin   = {{ mysql_datadir }}/mysql-bin
skip-external-locking
skip-name-resolve
skip-symbolic-links
server-id = 123
binlog_format=mixed
binlog_cache_size=8M
max_binlog_cache_size=24M
max_binlog_size=1G
expire_logs_days=7
default_storage_engine = innodb
key_buffer_size = 384M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
join_buffer_size = 2M
thread_cache_size = 32
thread_concurrency = 16
explicit_defaults_for_timestamp=true
interactive_timeout = 120
wait_timeout = 120
table_open_cache = 4096
open_files_limit = 10240
back_log=600
max_connections = 5000
max_connect_errors = 5000
max_allowed_packet = 16M
tmp_table_size = 256M
max_heap_table_size = 512M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 64M
myisam_repair_threads =1
myisam-recover-options
long_query_time = 2
slow_query_log
slow_query_log_file = {{ mysql_datadir }}/slow.log
innodb_buffer_pool_size = 1G
innodb_data_file_path = ibdata1:1G:autoextend
innodb_data_home_dir = {{ mysql_datadir }}
innodb_log_group_home_dir = {{ mysql_datadir }}
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 256M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 1
innodb_support_xa=0
innodb_flush_method = O_DIRECT

quick
max_allowed_packet = 32M

no-auto-rehash

key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M

interactive-timeout

拷贝mysqld启动脚本:
#cp /usr/local/src/mysql/support-files/mysql.server templates/
设置密码脚本:
# cat templates/mysql_security.sh
#!/bin/bash
{{ mysql_basedir }}/bin/mysql -h localhost -u root -e "set password for 'root'@'localhost' = password('mima');"

vars 目录:
# cat vars/main.yml
mysql_basedir: /usr/local/mysql
mysql_datadir: /data/mysql
mysql_user: mysql
mysql_database_user: root
mysql_passwd: 'mima'
mysql_port: 3306
mysql_sock: /data/mysql/mysql.sock
mysql_charset: utf8
mysql_collation: utf8_general_ci
mysql_version: mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz

3、创建安装yml脚本
#cd /etc/ansible/roles
#mkdir -pv common/{meta,tasks}
#cat common/meta/main.yml
galaxy_info:
author: liguang xing
description: Install initializtion Software
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
    versions:
    - 5
    - 6
- name: Ubuntu
    versions:
    - precise
categories:
- system
dependencies: []
# cat common/tasks/main.yml
- name: install initializtion require software
shell: yum -y install make cmake bc man* gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel openldap-clients libxslt-devel libevent-devel ntp libtool-ltdl bison libaio libtool vim-enhanced wget readline-devel libyaml-develpatch telnet dmidecode pcre pcre-devel
poll: 0
注:libaio rpm包一定要安装,mysql启动要依赖着包
# cat mysql_install.yml
---
- hosts: "{{host}}"
remote_user: "{{user}}"
gather_facts: True
roles:
    - common
    - mysql_install
4、开始执行
#ansible-playbook mysql_install.yml --syntax-check   --检测语法
playbook: mysql_install.yml
没错误开始执行:
# ansible-playbook mysql_install.yml --extra-vars "host=web user=root"
PLAY ********************************************************************
GATHERING FACTS ***************************************************************
ok:
TASK: ***********************
changed:
TASK: **********************
ok:
TASK: **************************
changed:
TASK: **************
changed:
TASK: ***********
changed:
TASK: ********************
changed:
TASK: ***********
changed:
TASK: ******************************
changed:
TASK: *********
changed:
TASK: **********
changed: => (item=/data/mysql)
changed: => (item=/usr/local/mysql)
TASK: *****************
changed:
TASK: ***********
changed:
TASK: *********
changed:
TASK: ****
changed:
PLAY RECAP ********************************************************************
10.0.90.24               : ok=15   changed=13   unreachable=0    failed=0   

成功之后到90.24服务器上查看mysql是否安装成功!我测试没问题
二、删除mysql
1、创建目录:
#cd /etc/ansible/roes
#mkdir -pv mysql_delete/{meta,tasks,vars}
# cat meta/main.yml
galaxy_info:
author: liguang xing
description: Delete Mysql
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
    versions:
    - 6
categories:
- Service
dependencies: []
# cat tasks/delete.yml
- name: Stop Mysql Service
service: name=mysqld state=stopped
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql Boot Start Script
shell: chkconfig --del mysqld
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql Dir And Socket
shell: rm -rf {{ mysql_basedir }} {{ mysql_datadir }}
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql User
shell: userdel {{ mysql_user }}
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql Service Start Script
shell: rm -f /etc/init.d/mysqld
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql lib
shell: rm -f /etc/ld.so.conf.d/mysql.conf
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql include file
shell: rm -f /usr/include/mysql
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete Mysql config file
shell: rm -f /etc/my.cnf
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat tasks/main.yml
- include: delete.yml
# cat vars/main.yml
mysql_basedir: /usr/local/mysql
mysql_datadir: /data/mysql
mysql_user: mysql

2、创建删除mysql的yml文件:
#cd /etc/ansible/roles
# cat mysql_delete.yml
---
- hosts: "{{host}}"
remote_user: "{{user}}"
gather_facts: True
roles:
    - mysql_delete
3、检测语法并执行:
# ansible-playbook mysql_delete.yml --syntax-check
playbook: mysql_delete.yml
无报错,开始执行
# ansible-playbook mysql_delete.yml --extra-vars "host=web user=root"
PLAY ********************************************************************
GATHERING FACTS ***************************************************************
ok:
TASK: *************************************
changed:
TASK: *************************
changed:
TASK: ****************************
changed:
TASK: **************************************
changed:
TASK: **********************
changed:
TASK: ***************************************
changed:
TASK: ******************************
changed:
TASK: *******************************
changed:
PLAY RECAP ********************************************************************
10.0.90.24               : ok=9    changed=8    unreachable=0    failed=0   
执行成功,无报错!

页: [1]
查看完整版本: ansible 自动化安装 mysql