gpt4 book ai didi

mysql - 使用ansible安装和配置mysql

转载 作者:行者123 更新时间:2023-11-29 17:21:03 40 4
gpt4 key购买 nike

我已经使用下面的 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 playbook使用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 文件的存在,我尝试执行删除匿名用户、删除测试数据库和更新 root 密码等任务。但我的剧本在删除匿名用户步骤中抛出错误。以下是我收到的错误:

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)\")"}

我正在关注 Apress - Ansible Beginner to Pro 的书以及以下链接 Installing MySQL on Centos 7 server using Ansible 。我的 ansible Controller 是一台 Macbook Pro,在虚拟机上运行 Centos 7 Minimal。

生成的.my.cnf:

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

有人可以解释一下这里出了什么问题以及如何删除匿名用户吗?

最佳答案

这会失败,因为一旦您创建文件/root/.my.cnf,它将用于连接到mysql,但您尚未更新root用户的密码。将任务名称:更新 root 密码移动到任务名称:创建 my.cnf

之前

关于mysql - 使用ansible安装和配置mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251225/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com