Ansible二进制部署MySQL 5.7.36
gitee链接:https://gitee.com/jiayu997/ansible/attach_files/1010740/download/ansible_mysql_57.tar.gz
目录结构
[root@boy ansible-example]
/root/ansible-example/
├── ansible.cfg
├── hosts
├── install_mysql_57.yml
└── roles
└── mysql_install_57
├── files
│ └── mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
├── tasks
│ ├── init.yml
│ ├── main.yml
│ └── mysql.yml
├── templates
│ ├── my.cnf.j2
│ └── mysqld.service.j2
└── vars
└── main.yml
安装过程
[root@boy ansible-example]
PLAY [MySQL 5.7.36 二进制安装] **************************************************************************************************************************
TASK [mysql_install_57 : include_tasks] ************************************************************************************************************
included: /root/ansible-example/roles/mysql_install_57/tasks/init.yml for 192.168.0.12
TASK [mysql_install_57 : Stop Firewalld] ***********************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Setenforce] ***************************************************************************************************************
[WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'. State change will take effect next reboot.
changed: [192.168.0.12]
TASK [mysql_install_57 : Init Dependency] **********************************************************************************************************
changed: [192.168.0.12] => (item=MySQL-python)
changed: [192.168.0.12] => (item=libaio-devel)
TASK [mysql_install_57 : include_tasks] ************************************************************************************************************
included: /root/ansible-example/roles/mysql_install_57/tasks/mysql.yml for 192.168.0.12
TASK [mysql_install_57 : Create MySQL User] ********************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Check MySQL Package] ******************************************************************************************************
ok: [192.168.0.12]
TASK [mysql_install_57 : Copy MySQL Package] *******************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Modify Package Privilege] *************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Export MySQL Path] ********************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Create MySQL Data Dir] ****************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Create MySQL LOG Dir] *****************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Copy MySQL Configuration] *************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Copy MySQL Service Configuration] *****************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Check MySQL Init Status] **************************************************************************************************
ok: [192.168.0.12]
TASK [mysql_install_57 : Init MySQL] ***************************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Enable MySQL SERVICE] *****************************************************************************************************
changed: [192.168.0.12]
TASK [mysql_install_57 : Configure ROOT PASSWORD] **************************************************************************************************
[WARNING]: Module did not set no_log for update_password
changed: [192.168.0.12]
TASK [mysql_install_57 : MySQL Service Status] *****************************************************************************************************
ok: [192.168.0.12] => {
"msg": "MySQL Service Status OK"
}
TASK [mysql_install_57 : MySQL Service Status] *****************************************************************************************************
skipping: [192.168.0.12]
PLAY RECAP *****************************************************************************************************************************************
192.168.0.12 : ok=19 changed=14 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
install_mysql_57.yml
[root@boy ansible-example]
---
- name: MySQL 5.7.36 二进制安装
gather_facts: False
hosts: mysql
roles:
- mysql_install_57
main.yml
[root@boy tasks]
---
- include_tasks: init.yml
- include_tasks: mysql.yml
init.yml
[root@boy tasks]
---
- name: Stop Firewalld
service: name=firewalld enabled=no state=stopped
- name: Setenforce
selinux: state=disabled
- name: Init Dependency
yum: name={{item}} state=present
loop:
- MySQL-python
- libaio-devel
mysql.yml
[root@boy tasks]
---
- block:
- name: Create MySQL User
user: name={{MySQL_USER}} shell=/sbin/nologin state=present create_home=False
- name: Check MySQL Package
stat: path={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}
register: RE
- name: Copy MySQL Package
unarchive: src={{MySQL_Package}} dest={{MySQL_INSTALL_PATH}} remote_src=no
when: RE.stat.exists != True
- name: Modify Package Privilege
file: path={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}} state=directory owner={{MySQL_USER}} group={{MySQL_USER}} recurse=yes mode=0755
- name: Export MySQL Path
lineinfile: path={{MySQL_PATH}} regexp=="export.*PATH=\$PATH:{{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/bin/" line="export PATH=$PATH:{{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/bin/"
- name: Create MySQL Data Dir
file: path={{MySQL_DATA_PATH}} state=directory owner={{MySQL_USER}} group={{MySQL_USER}} mode=0755 recurse=yes
- name: Create MySQL LOG Dir
file: path="{{MySQL_LOG_PATH}}" state=directory owner={{MySQL_USER}} group={{MySQL_USER}} mode=0755 recurse=yes
- name: Copy MySQL Configuration
template: src={{MySQL_CON_J}} dest={{MySQL_CON_F}}
- name: Copy MySQL Service Configuration
template: src=mysqld.service.j2 dest={{MySQL_SERVICE}}
- name: Check MySQL Init Status
stat: path={{MySQL_DATA_PATH}}/auto.cnf
register: RE
- name: Init MySQL
shell: "{{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/bin/mysqld --initialize-insecure --user={{MySQL_USER}} --basedir={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}} --datadir={{MySQL_DATA_PATH}}"
when: RE.stat.exists != True
- name: Enable MySQL SERVICE
systemd: name=mysqld enabled=yes state=started
- block:
- name: Configure ROOT PASSWORD
mysql_user: login_user=root login_port={{MySQL_PORT}} login_password="" user=root password={{MySQL_ROOT_PASSWORD}} update_password=always login_unix_socket={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/mysql.sock
rescue:
- name: CHECK ROOT LOGIN
mysql_db: login_user=root login_port={{MySQL_PORT}} login_password={{MySQL_ROOT_PASSWORD}} login_unix_socket={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/mysql.sock name=mysql state=present
register: RE
always:
- name: MySQL Service Status
debug: msg="MySQL Service Status OK"
when: RE.failed == false
- name: MySQL Service Status
debug: msg="MySQL Service Status Faild, Now will uninstall MySQL"
when: RE.failed == true
rescue:
- name: STOP MySQL SERVICE
systemd: name=mysqld eanbled=no state=stoped
ignore_errors: True
- name: REMOVE CONFIGURE
file: path={{MySQL_CON_F}} state=absent
- name: REMOVE SERVICE
file: path={{MySQL_SERVICE}} state=absent
- name: REMOVE LOG
file: path={{MySQL_LOG_PATH}} state=absent
- name: REMOVE PACKAGE
file: path={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}} state=absent
- name: REMOVE MYSQL DATA
file: path={{MySQL_DATA_PATH}} state=absent
- name: REMOVE mysql USER
user: name={{MySQL_USER}} state=absent remove=yes
vars
[root@boy mysql_install_57]
---
MySQL_Package: mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
MySQL_INSTALL_PATH: /opt
MySQL_DATA_PATH: /opt/MySQL
MySQL_USER: mysql
MySQL_PATH: /etc/profile
MySQL_CON_J: my.cnf.j2
MySQL_SERVICE: /usr/lib/systemd/system/mysqld.service
MySQL_CON_F: /etc/my.cnf
MySQL_LOG_PATH: /var/log/mysql
MySQL_PORT: 3306
MySQL_ROOT_PASSWORD: 'kylin@123'
SERVER_ID: 1
MySQL_BIN_LOG: "yes"
MySQL_RELAY_LOG: "yes"
MySQL_SLOW_LOG: "yes"
my.cnf.j2
[root@boy templates]
[client]
port={{MySQL_PORT}}
socket={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/mysql.sock
default-character-set=utf8
[mysqld]
basedir={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}
datadir={{MySQL_DATA_PATH}}
port={{MySQL_PORT}}
socket={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/mysql.sock
user={{MySQL_USER}}
sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
log-error={{MySQL_LOG_PATH}}/mysqld.log
expire_logs_days=7
{% if MySQL_BIN_LOG == "yes" %}
server_id={{SERVER_ID}}
log-bin={{MySQL_LOG_PATH}}/mysql-bin
binlog-format=Row
max_binlog_size=500M
gtid_mode=on
enforce_gtid_consistency=on
{% endif %}
{% if MySQL_RELAY_LOG == "yes" %}
relay-log={{MySQL_LOG_PATH}}/relay-log
{% endif %}
{% if MySQL_SLOW_LOG == "yes" %}
slow_query_log=ON
slow_query_log_file={{MySQL_LOG_PATH}}/slow_query.log
long_query_time=1
{% endif %}
[root@boy templates]
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/bin/mysqld --defaults-file={{MySQL_CON_F}}
LimitNOFILE = 5000
|