ansbile安装:

# ansible在CentOS7中需要安装epel仓库
yum install -y epel-release
yum install -y ansible

安装有好几种方法,yum安装是最简单的,安装ansible不是重点。

我的版本如下:

[root@szwpldb1081 mysqltools]# ansible --version
ansible 2.7.
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/python-3.7./lib/python3./site-packages/ansible
executable location = /usr/local/python/bin/ansible
python version = 3.7. (default, Dec , ::) [GCC 4.8. (Red Hat 4.8.-39.0.)]

我的目录树如下:

[root@szwpldb1081 mysql]# tree
.
├── common
│   ├── config_systemd.yaml
│   ├── create_my_cnf.yaml
│   ├── create_user.yaml
│   ├── export_path.yaml
│   ├── init_mysql_data_dir.yaml
│   ├── install_mysql.yaml
│   └── mysql_dependencies.yaml
├── install_group_replication.yaml
├── install_master_slaves.yaml
├── install_multi_source_replication.yaml
├── install_single.yaml
├── templates
│   ├── 5.7
│   │   └── my.cnf.jinja
│   ├── 8.0
│   │   ├── my.cnf
│   │   └── my.cnf.jinja
│   ├── init_mysql_data_dir.sh
│   ├── ld.so.conf
│   ├── mysqld.service
│   ├── mysqldump_backup.sh
│   ├── untar_mysql_pkg.sh
│   ├── users.sql
│   └── users.sql.jinja
├── upgrad_single_mysql.yaml
└── vars
├── group_replication.yaml
├── master_slaves.yaml
├── multi_source_replication.yaml
└── mysql.yaml

以安装单机版为例, install_single.yaml 如下:

[root@szwpldb1081 mysql]# cat install_single.yaml
---
- hosts: uat
remote_user: root
become_user: root
become: yes
vars_files:
- ../../config.yaml
- ./vars/mysql.yaml
tasks:
- name: create mysql user
import_tasks: common/create_user.yaml - name: install dependencies
import_tasks: common/mysql_dependencies.yaml - name: "create /etc/my-{{mysql_port}}.cnf"
import_tasks: common/create_my_cnf.yaml - name: "install {{mysql_binary_pkg}}"
import_tasks: common/install_mysql.yaml - name: "init data dir"
import_tasks: common/init_mysql_data_dir.yaml - name: "config path"
import_tasks: common/export_path.yaml - name: "config systemd and start mysqld"
import_tasks: common/config_systemd.yaml

create_user.yaml

[root@szwpldb1081 mysql]# cat common/create_user.yaml
---
- name: create mysql group
group:
name: "{{mysql_group}}"
state: present
gid: "{{mysql_gid}}" - name: create user "{{mysql_user}}"
user:
name: "{{mysql_user}}"
state: present
uid: "{{mysql_uid}}"
group: mysql

mysql_dependencies.yaml

[root@szwpldb1081 mysql]# cat common/mysql_dependencies.yaml
---
- name: install libaio
yum:
name: libaio-devel
state: present - name: install numactl
yum:
name: numactl-devel
state: present - name: install perl-Data-Dumper
yum:
name: perl-Data-Dumper
state: present

install_mysql.yaml

[root@szwpldb1081 mysql]# cat common/install_mysql.yaml
---
- name: "transfer {{mysql_binary_pkg}} to target host(s)."
copy:
src: "../../../sps/mysql/{{mysql_binary_pkg}}"
dest: "/tmp/mysql/"
owner: "{{mysql_user}}" - name: "generate untar script /tmp/untar_mysql_pkg.sh"
template:
src: "../templates/untar_mysql_pkg.sh"
dest: "/tmp/mysql/untar_mysql_pkg.sh" - name: "untar {{mysql_binary_pkg}} "
shell: "bash /tmp/mysql/untar_mysql_pkg.sh > /tmp/untar_mysql_pkg.log" - name: "rm /tmp/untar_mysql_pkg.sh"
file:
path: "/tmp/mysql/untar_mysql_pkg.sh"
state: absent - name: "create libmysqlclient_r.so"
file:
src: "{{mysql_base_dir}}/lib/libmysqlclient.so"
dest: "{{mysql_base_dir}}/lib/libmysqlclient_r.so"
state: link - name: "update file privileges"
file:
path: "{{mysql_base_dir}}"
owner: "{{mysql_user}}"
group: "{{mysql_group}}"
recurse: yes - name: "config ldconfig"
template:
src: "../templates/ld.so.conf"
dest: "/etc/ld.so.conf.d/{{mysql_version}}.conf" - name: "load so"
shell: ldconfig - name: "conifg header file"
file:
src: "{{mysql_base_dir}}/include"
dest: "/usr/include/{{mysql_version}}"
state: link

init_mysql_data_dir.yaml

[root@szwpldb1081 mysql]# cat common/init_mysql_data_dir.yaml
---
- name: "transfer users.sql to target host(s)."
template:
src: "../templates/users.sql"
dest: "/tmp/mysql/users.sql" - name: "transfer init_mysql_data_dir.sh to target host(s)."
template:
src: "../templates/init_mysql_data_dir.sh"
dest: /tmp/mysql/init_mysql_data_dir.sh - name: "init data dir"
shell: bash /tmp/mysql/init_mysql_data_dir.sh - name: "rm mysql_pkg_scripts"
file:
path: "/tmp/mysql/"
state: absent

export_path.yaml

[root@szwpldb1081 mysql]# cat common/export_path.yaml
---
- name: /etc/profile
lineinfile:
path: /etc/profile
line: "export PATH={{mysql_base_dir}}/bin/:$PATH"
insertafter: EOF
state: present - name: ~/.bash_profile
lineinfile:
path: "/home/{{mysql_user}}/.bash_profile"
line: "export PATH={{mysql_base_dir}}/bin/:$PATH"
insertafter: EOF
state: present - name: ~/.bashrc
lineinfile:
path: "/home/{{mysql_user}}/.bashrc"
line: "export PATH={{mysql_base_dir}}/bin/:$PATH"
insertafter: EOF
state: present

config_systemd.yaml

[root@szwpldb1081 mysql]# cat common/config_systemd.yaml
---
- name: "config mysqld-{{mysql_port}}.service"
template:
src: "../templates/mysqld.service"
dest: "/usr/lib/systemd/system/mysqld-{{mysql_port}}.service" - name: "conifg mysqld-{{mysql_port}} auto start"
systemd:
name: "mysqld-{{mysql_port}}"
enabled: yes
daemon_reload: yes - name: "start mysqld-{{mysql_port}}"
systemd:
name: "mysqld-{{mysql_port}}"
state: started
daemon_reload: yes

定义一些变量配置参数 config.yaml :

[root@szwpldb1081 mysql]# cat ../../config.yaml
max_memory_size_mb: "{{ 1024 * 512 }}" # 512G内存
mysql_port:
mysql_binary_pkg: "mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz"

mysql.yaml

[root@szwpldb1081 mysql]# cat ./vars/mysql.yaml
#mysql configure
mysql_user: "mysql"
mysql_group: "mysql"
mysql_uid: "{{mysql_port}}"
mysql_gid: "{{mysql_port}}"
mysql_packages_dir: "/usr/local/mysqltools/sps/mysql/"
mysql_base_dir: "/usr/local/{{ mysql_binary_pkg | replace('.tar.gz','') | replace('.tar.xz','') }}"
mysql_data_dir: "/database/mysql/node1"
mysql_version: "{{ mysql_binary_pkg | replace('.tar.gz','') | replace('.tar.xz','') }}"
mysql_root_pwd: 'xxxx'
mysql_monitor_user: 'xxx'
mysql_monitor_pwd: 'xxxx'
mysql_binlog_format: "row"
mysql_xport: "{{ mysql_port + 100 }}"
mysql_mgrport: "{{ mysql_port + 101 }}"
mysql_admin_port: "{{ mysql_port + 102 }}"

create_my_cnf.yaml

[root@szwpldb1081 mysql]# cat common/create_my_cnf.yaml
---
- name: "/etc/my-{{mysql_port}}.cnf for mysql-8.0.x "
when: mysql_binary_pkg.find('8.0') != -
template:
src: ../templates/8.0/my.cnf
dest: "/etc/my.cnf"
owner: "{{mysql_user}}"
group: "{{mysql_group}}"
backup: yes - name: "/etc/my-{{mysql_port}}.cnf for mysql-5.7.x "
when: mysql_binary_pkg.find('5.7') != -
template:
src: ../templates/5.7/my.cnf
dest: "/etc/my.cnf"
owner: "{{mysql_user}}"
group: "{{mysql_group}}"
backup: yes

文件目录设置的比较多,主要是为了配置方便灵活,可以适当的增加,看每个人自己实际的情况来操作就好。

[root@szwpldb1081 mysql]# cat /etc/ansible/ansible.cfg
[defaults]
host_key_checking = False #不检测host key
stdout_callback = debug
ANSIBLE_STDOUT_CALLBACK=debug

修改ansible配置文件hosts主机里面的主机,然后与 install_single.yaml:里面的host对应上:

[root@szwpldb1081 ~]# cat /etc/ansible/hosts
[uat]
x.x.x.x ansible_host=x.x.x.x ansible_user=root ansible_ssh_pass=xxxxx ansible_su_pass=xxxxx

执行过程报错:

TASK [init data dir] *********************************************************************************************************************
fatal: [172.18.1.192]: FAILED! => {
"changed": true,
"cmd": "bash /tmp/mysql/init_mysql_data_dir.sh",
"delta": "0:00:14.202501",
"end": "2020-05-01 12:36:32.514086",
"rc": ,
"start": "2020-05-01 12:36:18.311585"
} MSG: non-zero return code to retry, use: --limit @/usr/local/mysqltools/ansible/mysql/install_single.retry PLAY RECAP *******************************************************************************************************************************
172.18.1.192 : ok= changed= unreachable= failed=

手动执行提示:

hing off the --initialize-insecure option.
--01T12::17.339226+: [Note] [MY-] [Server] Creating the system tables.
--01T12::17.506281+: [Note] [MY-] [Server] Filling in the system tables, part .
--01T12::17.517417+: [Note] [MY-] [Server] Filling in the system tables, part .
--01T12::17.776951+: [Note] [MY-] [Server] Filling in the mysql.help table.
--01T12::17.858580+: [Note] [MY-] [Server] Creating the system users for internal usage.
--01T12::17.886104+: [Note] [MY-] [Server] Creating the sys schema.
--01T12::18.387866+: [Note] [MY-] [Server] Bootstrapping complete
--01T12::18.486722+: [ERROR] [MY-] [Server] You are not allowed to create a user with GRANT.
--01T12::18.489065+: [Note] [MY-] [Server] Execution of init_file '/tmp/mysql/users.sql' ended.
--01T12::18.489096+: [ERROR] [MY-] [Server] The newly created data directory /database/mysql/node1/ by --initialize is unusable. You can remove it.

脚本如下:

[root@SZWPLDB1091 ~]# cat /tmp/mysql/init_mysql_data_dir.sh
#!/bin/bash cd /usr/local/mysql-8.0.-linux-glibc2.-x86_64 if [ -d /database/mysql/node1 ]
then
echo "`date` | datadir has been inited" >> /tmp/mtls8.log
exit ;
else
mkdir -p /database/mysql/
mkdir -p /database/mysql//binlogs
mkdir -p /database/mysql//undolog
mkdir -p /database/mysql//relaylogs
mkdir -p /database/mysql//tmpdir
chown mysql:mysql -R /database/mysql
chmod /root/.mylogin.cnf
chmod -R /database/mysql
./bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize-insecure --init-file=/tmp/mysql/users.sql
fi

最后排查发现是 --init-file=/tmp/mysql/users.sql 有问题,修改以后然后执行:

[root@szwpldb1081 mysql]# ansible-playbook install_single.yaml 

PLAY [uat] *******************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************
ok: [172.18.1.192] TASK [create mysql group] ****************************************************************************************************************
ok: [172.18.1.192] TASK [create user "mysql"] ***************************************************************************************************************
ok: [172.18.1.192] TASK [install libaio] ********************************************************************************************************************
ok: [172.18.1.192] TASK [install numactl] *******************************************************************************************************************
ok: [172.18.1.192] TASK [install perl-Data-Dumper] **********************************************************************************************************
ok: [172.18.1.192] TASK [/etc/my-.cnf for mysql-8.0.x] *************************************************************************************************
changed: [172.18.1.192] TASK [/etc/my-.cnf for mysql-5.7.x] *************************************************************************************************
skipping: [172.18.1.192] TASK [transfer mysql-8.0.-linux-glibc2.-x86_64.tar.xz to target host(s).] ************************************************************
ok: [172.18.1.192] TASK [generate untar script /tmp/untar_mysql_pkg.sh] *************************************************************************************
changed: [172.18.1.192] TASK [untar mysql-8.0.-linux-glibc2.-x86_64.tar.xz] **********************************************************************************
changed: [172.18.1.192] TASK [rm /tmp/untar_mysql_pkg.sh] ********************************************************************************************************
changed: [172.18.1.192] TASK [create libmysqlclient_r.so] ********************************************************************************************************
ok: [172.18.1.192] TASK [update file privileges] ************************************************************************************************************
ok: [172.18.1.192] TASK [config ldconfig] *******************************************************************************************************************
ok: [172.18.1.192] TASK [load so] ***************************************************************************************************************************
changed: [172.18.1.192] TASK [conifg header file] ****************************************************************************************************************
ok: [172.18.1.192] TASK [transfer users.sql to target host(s).] *********************************************************************************************
changed: [172.18.1.192] TASK [transfer init_mysql_data_dir.sh to target host(s).] ********************************************************************************
ok: [172.18.1.192] TASK [init data dir] *********************************************************************************************************************
changed: [172.18.1.192] TASK [rm mysql_pkg_scripts] **************************************************************************************************************
changed: [172.18.1.192] TASK [/etc/profile] **********************************************************************************************************************
changed: [172.18.1.192] TASK [~/.bash_profile] *******************************************************************************************************************
changed: [172.18.1.192] TASK [~/.bashrc] *************************************************************************************************************************
changed: [172.18.1.192] TASK [config mysqld-.service] *******************************************************************************************************
changed: [172.18.1.192] TASK [conifg mysqld- auto start] ****************************************************************************************************
changed: [172.18.1.192] TASK [start mysqld-] ****************************************************************************************************************
changed: [172.18.1.192] TASK [create backup dir] *****************************************************************************************************************
changed: [172.18.1.192] TASK [create backup script dir] **********************************************************************************************************
changed: [172.18.1.192] TASK [transfer backup script to target host (mysqldump)] *********************************************************************************
changed: [172.18.1.192] TASK [config backup job (mysqldump)] *****************************************************************************************************
changed: [172.18.1.192] PLAY RECAP *******************************************************************************************************************************
172.18.1.192 : ok= changed= unreachable= failed=

至此单机mysql一键安装算是完毕,借鉴了https://galaxy.ansible.com/home,看什么时候能自己整合一个平台,把ansible集成到界面,鼠标点记下,就完成一台机器安装,就好了。

ansible一键安装mysql8.0的更多相关文章

  1. ansible一键安装GreatSQL并构建MGR集群

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 利用ansible一键安装GreatSQL并完成MGR部署. 本次介绍如何利用ansible一键安装GreatSQL并完成 ...

  2. CentOS7.4 源码安装MySQL8.0

    MySQL 8 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 以下为本人2018.4.23日安装过程的记录.整个过程大 ...

  3. centos 7下安装mysql-8.0

    本篇文章主要介绍在centos7 环境下安装mysql8.0并设置为开机自启. 安装步骤 1.配置yum源 首先在 https://dev.mysql.com/downloads/repo/yum/  ...

  4. 安装mysql8.0.12

    安装mysql8.0.12 https://blog.csdn.net/zwj1030711290/article/details/80039780 问题1:忘记记录日志打印的密码就把窗口给关了 解决 ...

  5. Linux 6.8 源码安装MySQL8.0

    搭建环境说明: 系统版本:Red Hat Enterprise Linux Server release 6.8 (Santiago) 内核版本:Linux 2.6.32-642.el6.x86_64 ...

  6. Centos7安装MySQL8.0 - 操作手册

    MySQL 8 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 一.  Mysql8.0版本相比之前版本的一些特性 1) ...

  7. 安装mysql8.0.12以及修改密码和Navicat的连接

    mysql8.0+与安装其他版本不同一.安装mysql8.0.121.到官网https://www.mysql.com/  下载mysql-8.0.12-winx64.zip(不要.mis),直接解压 ...

  8. 源码编译安装MySQL8.0

    源码编译安装MySQL8.0 0.前期准备条件 查看linux的版本 [root@mysql etc]# cat /etc/redhat-release CentOS Linux release 7. ...

  9. Win10安装mysql-8.0.11-winx64详细步骤

    安装 mysql-8.0.11-winx64 https://blog.csdn.net/qq_20788055/article/details/80372577

随机推荐

  1. 1~n的之间的k个数组成和为n的方案数(动态规划)

    绯色的子弹 Description 众所周知,夏季奥林匹克运动会时隔56年第二次在东京举办,紧接着出来的<名侦探柯南 M24绯色的子弹>竟也是有奥运会的背景,最重要的是重归主线!!!(赤井 ...

  2. lly的瞬移方块(并查集)

    lly的瞬移方块 Description llyllylly最近发明了一个叫瞬移方块的游戏,为啥llyllylly这么闲呢,这得从一只蝙蝠说起..... llyllylly决定给大家也分享一下这个游戏 ...

  3. CF1292C Xenon's Attack on the Gangs

    题目链接:https://codeforces.com/problemset/problem/1292/C 题意 在一颗有n个节点的树上,给每个边赋值,所有的值都在\([0,n-2]\)内并且不重复, ...

  4. Linux配置dhcp服务器

    一.安装dhcp软件 yum -y install dhcp 二.配置 dhcp 主配置文件 /etc/dhcp/dhcpd.conf ns-update-style interim; log-fac ...

  5. 最小生成树算法【图解】--一文带你理解什么是Prim算法和Kruskal算法

    假设以下情景,有一块木板,板上钉上了一些钉子,这些钉子可以由一些细绳连接起来.假设每个钉子可以通过一根或者多根细绳连接起来,那么一定存在这样的情况,即用最少的细绳把所有钉子连接起来. 更为实际的情景是 ...

  6. (js描述的)数据结构[哈希表1.3](10)

    1.哈希表的完善 1.容量质数(limit):需要恒为质数,来确保元素的均匀分布. 1)普通算法: 判断一个数是否为质数 function isPrime(num) { for (var i = 2; ...

  7. Deep Dream模型与实现

    Deep Dream是谷歌公司在2015年公布的一项有趣的技术.在训练好的卷积神经网络中,只需要设定几个参数,就可以通过这项技术生成一张图像. 本文章的代码和图片都放在我的github上,想实现本文代 ...

  8. MySQL学习之路3-MySQL中常用数据类型

    MySQL中常用数据类型 字符型 存储字符型数据.例如姓名,地址,电话号码等.使用引号括起来,一般使用单引号. 常用类型: char(255) 定长字符串,最大长度255个字符. varchar(25 ...

  9. Python 1基础语法一(注释、行与缩进、多行语句、空行和代码组)

    一.注释Python中单行注释以 # 开头,实例如下: # 第一个注释 print ("Hello, Python!") # 第二个注释 输出结果为: ============== ...

  10. .NET Core 3 WPF MVVM框架 Prism系列之导航系统

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism基于区域Region的导航系统 在讲解Prism导航系统之前,我们先来看看一个例子,我在之前的demo项目创建一个登录界面: 我们看 ...