使用Ansible自動化安裝MySQL數據庫


使用Ansible自動化部署MySQL數據庫

環境聲明

Ansible版本:2.9.7
MySQL數據庫版本:5.7.28
節點聲明:
192.168.1.62(Ansible管理端)
192.168.1.63(被管理端,MySQL目標節點)

# 兩個節點已經做好免密並且配置好了Yum源

# /etc/ansible/roles目錄結構說明
roles/
├── mysql
│   ├── files
│   │   ├── my.cnf
│   │   ├── mysql-5.7.28-1.el7.x86_64.tar.gz
│   │   └── setpassword.sh
│   └── tasks
│       └── main.yml
└── mysqlinstall.yml

my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysql]
default-character-set=utf8


[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password_policy=0 
validate_password_length=1 
validate_password_special_char_count=0 
validate_password_number_count=0 
validate_password_mixed_case_count=0 
lower_case_table_names=1 
max_connections=50000
!includedir /etc/my.cnf.d

setpassword.sh

#!/bin/bash
#This script is set MySQL password for the frist time!
mysqlinitpasswd=`grep 'temporary password' /var/log/mysqld.log |awk '{print $11}'`
mysql -uroot -p${mysqlinitpasswd} -S /var/lib/mysql/mysql.sock -e "set global validate_password_policy=0;set global validate_password_length=1;set global validate_password_policy=0;set glob
al validate_password_length=1;set password = password('Cosmo_123');grant all privileges on *.* to 'root' @'%' identified by 'Cosmo_123';flush privileges;" --connect-expired-password

main.yml

- name: unarchive Mysql
  unarchive: src=mysql-5.7.28-1.el7.x86_64.tar.gz dest=/opt/cosmo/com/ owner=root group=root
- name: install need
  shell: yum -y install perl libaio* net-tools 
- name: install mysql
  shell: yum install -y /opt/cosmo/com/mysql-5.7.28-1.el7.x86_64/*
- name: initid mysql-server
  command: systemctl start mysqld
- name: set enable mysql
  command: systemctl enable mysqld
- name: cp my.cnf
  copy: src=my.cnf dest=/etc/my.cnf
- name: set password
  copy: src=setpassword.sh dest=/opt/cosmo/com/ mode=755
- name: sh setpassword
  shell: sh /opt/cosmo/com/mysql/setpassword.sh
- name: restart mysqld
  shell: systemctl restart mysqld

mysqlinstall.yml

- hosts: db_server
  remote_user: root
  roles:
    - mysql

/etc/ansible/hosts

[db_server]
192.168.1.63

安裝

ansible-playbook /etc/ansible/roles/mysqlinstall.yml

執行結果如下圖

在192.168.1.63上查看mysql是否安裝成功

systemctl status mysqld
mysql -uroot -p'你的密碼'

安裝成功!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM