mysql服务部署到db01(192.168.10.51、172.16.1.51)主机。

使用的版本是mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz,大小:1G,由于文件太大,要提前下载好。

建议在阿里云开源镜像站下载:https://mirrors.aliyun.com/mysql/MySQL-8.0/

提前把mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz下载到Windows端,

提前准备:

1、db端

1)安装包准备

提前把mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz传到db01的/tmp/目录中

mysql安装包放在/tmp/

[root@db01 ~]#ll -h  /tmp/
total 1.2G
-rw-r--r--  1 root root 1.2G Jun 18 10:53 mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

2)解压

[root@db01 ~]#cd /tmp/
[root@db01 /tmp]#tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz 

mysql解压后的目录:

[root@db01 /tmp]#ls mysql-8.0.28-linux-glibc2.12-x86_64
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@db01 /tmp]#

3)将解压后的mysql移动到/app/tools/目录

[root@db01 /tmp]#mkdir -p /app/tools/
[root@db01 /tmp]#mv mysql-8.0.28-linux-glibc2.12-x86_64/ /app/tools/
[root@db01 /tmp]#

至此。db端准备完成。

2、管理端(m01)

1)项目目录创建

[root@m01 /server/project_roles]#pwd
/server/project_roles
[root@m01 /server/project_roles]#mkdir -p mysql/{tasks,files,handlers,templates}

2)tasks

[root@m01 /server/project_roles]#vim mysql/tasks/main.yml
- name: 1.分发mysql安装包
  shell: echo "已提前完成"
- name: 2.创建/app/tools/目录
  file:
    path: /app/tools/
    state: directory
- name: 2.解压mysql到/app/tools/目录
  shell: echo "已提前完成"
- name: 3.创建软连接
  file: 
    src: /app/tools/mysql-8.0.28-linux-glibc2.12-x86_64
    path: /app/tools/mysql
    state: link
- name: 4.安装依赖
  yum:
    name: "{{ item }}"
    state: present
  loop:
    - ncurses
    - ncurses-devel
    - libaio-devel
    - openssl
    - openssl-devel 
- name: 5.创建mysql虚拟用户
  user:
    name: mysql
    shell: /sbin/nologin
    create_home: no
    state: present
- name: 6.创建数据存放目录/data/mysql
  file:
    path: /data/mysql/
    owner: mysql
    group: mysql
    state: directory
- name: 7.分发my.cnf配置文件
  copy:
    src: my.cnf
    dest: /etc/my.cnf     
  notify:
    - restart_mysql
- name: 8.初始化数据库
  shell: /app/tools/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/app/tools/mysql/ --datadir=/data/mysql/
  register: init_mysql
- name: debug
  debug:
    msg: "{{ init_mysql }}"
- name: 9.准备systemctl启动文件
  shell: |
      cp /app/tools/mysql/support-files/mysql.server /etc/init.d/mysqld
      export PATH=/app/tools/mysql/bin/:$PATH  #配置环境变量
      echo "export PATH=/app/tools/mysql/bin/:$PATH" >> /etc/profile #配置环境变量
      sed -i 's#/usr/local/mysql#/app/tools/mysql/#g' /etc/init.d/mysqld #修改默认安装目录
      sed -i 's#^basedir=$#basedir=/app/tools/mysql/#g' /etc/init.d/mysqld #修改默认安装目录
- name: 10.启动mysql服务
  systemd:
    name: mysqld
    enabled: yes
    daemon_reload: yes
    state: started
[root@m01 /server/project_roles]#

3)my.cnf配置文件

在mysql项目目录的files目中创建my.cnf文件

[root@m01 /server/project_roles]#vim mysql/files/my.cnf
[mysqld]
user=mysql
datadir=/data/mysql
port=3306
socket=/tmp/mysql.socket
[client]
socket=/tmp/mysql.socket

4)handlers

如果配置文件有修改,则重启mysql服务

[root@m01 /server/project_roles]#vim mysql/handlers/main.yml
- name: restart_mysql
  systemd:
    name: mysqld
    state: restarted

5)脚本入口文件

[root@m01 /server/project_roles]#vim top.yml
- hosts: 172.16.1.51
  gather_facts: no
  roles:
    - role: mysql

6)执行脚本

[root@m01 /server/project_roles]#ansible-playbook -i hosts  top.yml

7)启动mysqld报错

Jun 18 12:20:29 db01 mysqld[8653]: /etc/rc.d/init.d/mysqld: line 239: my_print_defaults: command not found
Jun 18 12:20:29 db01 mysqld[8653]: /etc/rc.d/init.d/mysqld: line 259: cd: /usr/local/mysql: No such file or directory
Jun 18 12:20:29 db01 mysqld[8653]: Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

解决:

1、环境变量问题:
export PATH=/app/tools/mysql/bin/:$PATH
echo "export PATH=/app/tools/mysql/bin/:$PATH" >> /etc/profile
2、默认安装目录问题:
mysql默认安装目录是:/usr/local/mysql,而我们自己定义的是:/app/tools/mysql/,所以报错。修改:
cp /app/tools/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -i 's#/usr/local/mysql#/app/tools/mysql/#g' /etc/init.d/mysqld
sed -i 's#^basedir=$#basedir=/app/tools/mysql/#g' /etc/init.d/mysqld

8)服务启动报错

报错信息查看:

1、systemctl  status  服务名
2、journalctl  -u  服务名
3、查看服务的日志、系统日志