Ansible-批量安装mysql

Ansible-批量安装mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
yum install -y libselinux-python
#创建用户、文件夹
groupadd mysql
useradd mysql -g mysql -s /sbin/nologin -M
mkdir -p /home/data
#安装rpm包
rpm -e --nodeps mysql-libs-5.1.73-8.el6_8.x86_64
rpm -ivh MySQL-devel-5.6.37-1.el6.x86_64.rpm MySQL-client-5.6.37-1.el6.x86_64.rpm MySQL-server-5.6.37-1.el6.x86_64.rpm
#复制配置文件
cp my.cnf /etc/my.cnf
#转移文件
mv /var/lib/mysql /home/data
chown -R mysql:mysql /home/data
##启动mysql
service mysql start
##开机启动
chkconfig mysql on

##初始化脚本
sh setmysql.sh
setmysql.sh
passwd=`awk -F"time):" '{print $2}' /root/.mysql_secret |awk '{print $1}'`
mysql -uroot -p$passwd -b --connect-expired-password <setmysql.sql
setmysql.sql
SET PASSWORD = PASSWORD('ShSflow!@#123');
create user mysql identified by "ShSflow!@#123";
grant all privileges on *.* to 'mysql'@'%' identified by 'ShSflow!@#123' with grant option;
flush privileges;
##重启mysql
service mysql restart
##防火墙设置
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
service iptables save
service iptables restart
##ansible脚本
---
- hosts: ggall
remote_user: root
tasks:
- name: make user,group,dir
shell: groupadd mysql; useradd mysql -g mysql -s /sbin/nologin -M; mkdir -p /home/data ;mkdir -p /home/shanghang/soft
- name: remove mysql-libs*
shell: yum remove mysql-libs* -y
- name: install numactl
shell: yum install -y numactl
- name: copy rpm
copy: src=/soft/ansible/MySQL-client-5.6.37-1.el6.x86_64.rpm dest=/home/shanghang/soft/MySQL-client-5.6.37-1.el6.x86_64.rpm
- name: copy rpm
copy: src=/soft/ansible/MySQL-devel-5.6.37-1.el6.x86_64.rpm dest=/home/shanghang/soft/MySQL-devel-5.6.37-1.el6.x86_64.rpm
- name: copy rpm
copy: src=/soft/ansible/MySQL-server-5.6.37-1.el6.x86_64.rpm dest=/home/shanghang/soft/MySQL-server-5.6.37-1.el6.x86_64.rpm
- name: install rpm
shell: rpm -ivh MySQL-devel-5.6.37-1.el6.x86_64.rpm MySQL-client-5.6.37-1.el6.x86_64.rpm MySQL-server-5.6.37-1.el6.x86_64.rpm
args:
chdir: /home/shanghang/soft/
- name: copy my.cnf
copy: src=/soft/ansible/my.cnf dest=/etc/my.cnf
- name: move mysqldir to /home/data
shell: mv /var/lib/mysql /home/data ; chown -R mysql:mysql /home/data
- name: start mysql
shell: service mysql start
- name: set autorun
shell: chkconfig mysql on
- name: set autorun
shell: chkconfig mysql on
- name: copy setmysql.sh
copy: src=/soft/ansible/setmysql.sh dest=/home/shanghang/soft/setmysql.sh
- name: copy setmysql.sql
copy: src=/soft/ansible/setmysql.sql dest=/home/shanghang/soft/setmysql.sql
- name: run setmysql.sh
shell: sh setmysql.sh
args:
chdir: /home/shanghang/soft/
- name: restart mysql
shell: service mysql restart
- name: set iptables
shell: iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
- name: save iptables
shell: service iptables save
- name: restart iptables
shell: service iptables restart
##my.cnf
[client]
socket=/home/data/mysql/mysql.sock
[mysqld]
skip-name-resolve
port=3306
basedir=/usr
datadir=/home/data/mysql
socket=/home/data/mysql/mysql.sock
server-id=2
max_allowed_packet = 20M
federated
# InnoDB settings
innodb_buffer_pool_size=2G
innodb_log_file_size=1G
# Other
character-set-server=utf8
collation-server=utf8_general_ci
max-connect-errors=6000
max-connections=3000
thread_cache_size = 300
thread_concurrency =32
innodb_thread_concurrency =16
max-connect-errors=6000
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1