使用ansible安装和配置mysql

3wabscal  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(514)

我使用以下ansible playbook安装了mysql、nginx和php:

---
- hosts: all
  become: true
  tasks:
    - name: Make Sure we can connect to hosts
      ping:
    - name: Install PHP
      yum: name={{item}} state=present update_cache=yes
      with_items:
        - php
        - php-fpm
        - php-mysql
        - php-xml
    - name: add repository nginx-release (CentOS6/CentOS7)
      yum: name="http://nginx.org/packages/centos/{{ansible_distribution_major_version}}/noarch/RPMS/nginx-release-centos-{{ansible_distribution_major_version}}-0.el{{ansible_distribution_major_version}}.ngx.noarch.rpm"
    - name: Install nginx
      yum: name=nginx state=installed enablerepo=nginx
    - name: Download MySQL Community Repo
      command: /usr/bin/rpm -ivh /tmp/mysql-community-release-el7-7.noarch.rpm
    - name: Install MySql
      yum: name={{item}} state=installed
      with_items:
        - mysql-server
        - MySQL-python

我正在尝试使用以下ansible剧本使用ansible配置mysql:

---
- hosts: ansible-ctl
  become: true
  tasks:
    - name: Make Sure we can connect to hosts
      ping:
    - name: Start MySQL Server and enable it
      service: name=mysqld state=started enabled=yes
    - name: Generate new root password
      command: openssl rand -hex 7 creates=/root/.my.cnf
      register: mysql_new_root_pass
    - name: Create my.cnf
      template: src=templates/mysql/my.cnf dest=/root/.my.cnf
      when: mysql_new_root_pass.changed
    - name: Remove anonymous users
      mysql_user: name='' host_all=yes state=absent
      when: mysql_new_root_pass.changed
    - name: Remove test database
      mysql_db: name=test state=absent
      when: mysql_new_root_pass.changed
    - name: Output new root password
      debug: msg="New root password is {{mysql_new_root_pass.stdout}}"
      when: mysql_new_root_pass.changed
    - name: Update root password
      mysql_user: name=root host={{item}} password={{mysql_new_root_pass.stdout}}
      with_items:
        - "{{ ansible_hostname }}"
        - 127.0.0.1
        - ::1
        - localhost
      when: mysql_new_root_pass.changed

my.cnf模板文件位于/templates/mysql/位置:

[client]
user=root
password={{ mysql_new_root_pass.stdout }}

文件 /root/.my.cnf 正在使用密码生成文件。
首先我要启动mysql服务。然后使用openssl创建新密码并将其存储在 mysql_new_root_pass . 然后基于 /root/.my.cnf 我试图执行的任务,如删除匿名用户,删除测试数据库和更新根密码。但我的行动手册在这一步上犯了错误 Remove Anonymous Users . 下面是我收到的错误:

fatal: [ansible-ctl]: FAILED! => {"changed": false, "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1045, \"Access denied for user 'root'@'localhost' (using password: YES)\")"}

我跟随这本书从一个press-ansible初学者到pro,还有下面的链接使用ansible在centos7服务器上安装mysql。我的ansible控制器是macbook pro,在虚拟机上运行centos 7。
生成的.my.cnf:

[client]
user=root
password=<generated password>

有谁能解释一下这里出了什么问题,我如何删除匿名用户?

q5iwbnjs

q5iwbnjs1#

这会失败,因为一旦创建文件 /root/.my.cnf 它将用于连接到mysql,但您尚未更新根用户的密码。移动任务 name: Update root password 就在任务开始之前 name: Create my.cnf

相关问题