【实验需求】

1
对 dbservers 组部署 MySQL5.7

【实验步骤】

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
108
109
110
111
112
# 准备相关文件
[root@control ~]# mkdir -p /data/ansible/files/mysql/
[root@control ~]# ls /data/ansible/files/mysql/
my.cnf mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz
[root@control ~]# cat /data/ansible/files/mysql/my.cnf
[client]
port=3306
socket=/data/mysql/mysql.sock

[mysql]
prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"

[mysqld]
log-bin=/mysql/binlog/mysql-bin
server-id=1
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
symbolic-links=0
skip_name_resolve=1

log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

# 准备playbook剧本
[root@control ~]# cd /etc/ansible/
[root@control ansible]# vim mysql57_install.yml
---
# install mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz
- hosts: dbservers
vars:
PackageDir: "/data/ansible/files/mysql"
PackageName: "mysql-5.7.40-linux-glibc2.12-x86_64"
Suffix: ".tar.gz"
ConfigFile: "my.cnf"
MysqlRootPassword: wuhaolam
ID: "{{ ansible_eth0.ipv4.address.split('.')[-1] }}"
tasks:
- name: install packages
yum:
name:
- libaio
- numactl-libs
- libncurses*
state: present

- name: create user and group
user:
name: mysql
uid: 306
shell: /sbin/nologin
system: yes
create_home: no

- name: create mysql data dir
file:
path: /data/mysql
state: directory
owner: mysql
group: mysql
recurse: yes

- name: create binlog file directory
file:
path: /mysql/binlog
state: directory
owner: mysql
group: mysql
recurse: yes

- name: copy file to remote host
unarchive:
src: "{{ PackageDir }}/{{ PackageName }}{{ Suffix }}"
dest: "/usr/local/"
owner: root
group: root

- name: create linkfile /usr/local/myql
file:
src: "/usr/local/{{ PackageName }}"
dest: /usr/local/mysql
owner: root
group: root
state: link

- name: PATH variable
copy:
content: 'PATH=/usr/local/mysql/bin:$PATH'
dest: /etc/profile.d/mysql.sh

- name: create config my.cnf
copy:
src: "{{ PackageDir }}/{{ ConfigFile }}"
dest: /etc/my.cnf

- name: modify config file
lineinfile:
path: /etc/my.cnf
regexp: '^server-id='
line: "server-id={{ ID }}"

- name: service start script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

- name: initial mysql
shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

- name: enables service
shell: chkconfig --add mysqld;chkconfig mysqld on;/etc/init.d/mysqld start

- name: change password
shell: /usr/local/mysql/bin/mysqladmin -uroot password {{ MysqlRootPassword }}