Ansible安裝mysql
阿新 • • 發佈:2021-01-05
利用playbook安裝mysql
下載mysql-5.6.49-linux-glibc2.12到ansible所在的伺服器上
下載並移動到指定目錄下
[[email protected]_1 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6/mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
--2021-01-04 00:12:08-- https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6/mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 343184888 (327M) [application/x-gzip]
Saving to: ‘mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz’
100%[===================================================>] 343,184,888 10.0MB/s in 30s
2021-01-04 00:12:39 (10.8 MB/s) - ‘mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz’ saved [343184888/343184888]
[ [email protected]_1 ~]# ls -lrt
total 392360
-rw-r--r-- 1 root root 28296 Jan 10 2018 percona-zabbix-templates-1.1.8-1.noarch.rpm
-rw-r--r-- 1 root root 58546654 Aug 15 2019 grafana-6.3.3-1.x86_64.rpm
-rw-r--r-- 1 root root 343184888 Jun 2 2020 mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
-rw-------. 1 root root 2246 Dec 23 22:08 anaconda-ks.cfg
-rw-r--r--. 1 root root 2277 Dec 23 22:11 initial-setup-ks.cfg
drwxr-xr-x 2 root root 70 Jan 3 23:28 ansible
[ [email protected]_1 ansible]# mkdir -p /data/ansible/files
[[email protected]_1 ansible]# cd /data/ansible/files/
[[email protected]_1 files]# mv mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz /data/ansible/files
新增兩個配置檔案(mysql的配置檔案設定)
[[email protected]_1 files]# vim my.cnf [mysqld] socket=/tmp/mysql.sock user=mysql symbolic-links=0 datadir=/data/mysql innodb_file_per_table=1 log-bin pid-file=/data/mysql/mysqld.pid [client] port=3306 socket=/tmp/mysql.sock [mysqld_safe] log-error=/var/log/mysqld.log
mysql安全加固設定
[[email protected]_1 files]# vim secure_mysql.sh
#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOF
y
test123
test123
y
y
y
y
EOF
目錄結構如下
[[email protected]_1 files]# tree
.
├── my.cnf
├── mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
└── secure_mysql.sh
0 directories, 3 files
編輯安裝mysql的yml檔案
[[email protected]_1 files]# vim /root/ansible/install_mysql.yml
- hosts: dbsrvs
remote_user: root
gather_facts: no
tasks:
- name: install packages
yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
- name: create mysql group
group: name=mysql gid=306
- name: create mysql user
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
- name: copy tar to remote host and file mode
unarchive: src=/data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root
- name: create linkfile /usr/local/mysql
file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
- name: data dir
shell: chdir=/usr/local/mysql/ ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
tags: data
- name: config my.cnf
copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: enable service
shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
tags: service
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: secure script
script: /data/ansible/files/secure_mysql.sh
tags: script
檢測語法的正確性(會有報錯,因為檢測語句不會執行解壓,會報mysql的解壓後的那個路徑存在的問題 可忽略)
[[email protected]_1 ansible]# ansible-playbook -C install_mysql.yml
執行playbook結果如下(有報錯資訊)
[[email protected]_1 ansible]# ansible-playbook install_mysql.yml
PLAY [dbservers] ********************************************************************************************************************************************
TASK [install packages] *************************************************************************************************************************************
ok: [192.168.1.40]
TASK [create mysql group] ***********************************************************************************************************************************
ok: [192.168.1.40]
TASK [create mysql user] ************************************************************************************************************************************
ok: [192.168.1.40]
TASK [copy tar to remote host and file mode] ****************************************************************************************************************
ok: [192.168.1.40]
TASK [create linkfile /usr/local/mysql] ********************************************************************************************************************
ok: [192.168.1.40]
TASK [data dir] *********************************************************************************************************************************************
fatal: [192.168.1.40]: FAILED! => {"changed": true, "cmd": "./scripts/mysql_install_db --datadir=/data/mysql --user=mysql", "delta": "0:00:00.030984", "end": "2021-01-04 01:13:43.883524", "msg": "non-zero return code", "rc": 1, "start": "2021-01-04 01:13:43.852540", "stderr": "", "stderr_lines": [], "stdout": "FATAL ERROR: The parent directory for the data directory '/data/mysql' does not exist.\nIf that path was really intended, please create that directory path and then\nrestart this script.\nIf some other path was intended, please use the correct path when restarting this script.", "stdout_lines": ["FATAL ERROR: The parent directory for the data directory '/data/mysql' does not exist.", "If that path was really intended, please create that directory path and then", "restart this script.", "If some other path was intended, please use the correct path when restarting this script."]}
PLAY RECAP **************************************************************************************************************************************************
192.168.1.40 : ok=5 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
根據報錯資訊提示遠端主機目錄(/data)不存在,建立即可
執行結果驗證如下
[[email protected]_1 ansible]# ansible-playbook install_mysql.yml
PLAY [dbservers] ********************************************************************************************************************************************
TASK [install packages] *************************************************************************************************************************************
ok: [192.168.1.40]
TASK [create mysql group] ***********************************************************************************************************************************
ok: [192.168.1.40]
TASK [create mysql user] ************************************************************************************************************************************
ok: [192.168.1.40]
TASK [copy tar to remote host and file mode] ****************************************************************************************************************
ok: [192.168.1.40]
TASK [create linkfile /usr/local/mysql] ********************************************************************************************************************
ok: [192.168.1.40]
TASK [data dir] *********************************************************************************************************************************************
changed: [192.168.1.40]
TASK [config my.cnf] ****************************************************************************************************************************************
changed: [192.168.1.40]
TASK [service script] ***************************************************************************************************************************************
changed: [192.168.1.40]
TASK [enable service] ***************************************************************************************************************************************
changed: [192.168.1.40]
TASK [PATH variable] ****************************************************************************************************************************************
changed: [192.168.1.40]
TASK [secure script] ****************************************************************************************************************************************
changed: [192.168.1.40]
PLAY RECAP **************************************************************************************************************************************************
192.168.1.40 : ok=11 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
驗證mysql是否在遠端主機上安裝成功
[[email protected]_3 ~]# mysql -uroot -ptest123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.6.49-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.49-log |
+------------+
1 row in set (0.00 sec)
mysql>