Vagrant Ansible CentOS 7 如何安装 MySQL 5.7 并更改默认密码

标签 vagrant ansible centos7

我在 CentOS 7 上使用 Vagrant 和 Ansble。 我正在尝试安装 MySQL 5.7,但在尝试更新 MySQL 密码时遇到问题。

- name: Install MySQL 5.7 repo
  yum: name=http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm state=present

- name: Install MySQL 5.7
  yum: pkg={{ item }}
  with_items:
    - mysql-community-server
    - MySQL-python

- name: Start the MySQL service
  service: name=mysqld state=started enabled=true

- name: update mysql root passwd
  mysql_user: name=root
          host={{ item }}
          password='PassW0rd'
          check_implicit_admin=yes
          login_user=root
          login_password=''
          state=present
  with_items:
    - 127.0.0.1
    - ::1
    - localhost

我研究发现 MySQL 5.7 会自动生成默认密码,所以我的脚本失败了。有人能帮我解决这个问题吗?

最佳答案

---
tasks:
  - name: Install MySQL 5.7 repo
    yum: name=http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm state=present

  - name: Install MySQL 5.7
    yum: pkg={{ item }}
    with_items:
    - mysql-community-server
    - mysql-community-client
    - MySQL-python

  - name: Start the MySQL service
    service: name=mysqld state=started enabled=true

  - name: Change mysql root password and keep track in 
    shell: |
      password_match=`awk '/A temporary password is generated for/ {a=$0} END{ print a }' /var/log/mysqld.log | awk '{print $(NF)}'`
      echo $password_match
      mysql -uroot -p$password_match --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'PassW0rd'; flush privileges; "
      echo "[client]"
      user=root
      password=PassW0rd > /root/.my.cnf
    args:
      creates: /root/.my.cnf
    register: change_temp_pass
    notify: restart mysqld

  - meta: flush_handlers
  - debug:
      var: change_temp_pass

handlers:
  - name: restart mysqld
      service: 
      name: mysqld 
      state: restarted

关于Vagrant Ansible CentOS 7 如何安装 MySQL 5.7 并更改默认密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38523192/

相关文章:

linux - 时间命令的问题

docker - 在MacOS上使用docker作为提供程序的游民,无法在容器中挂载目录

python - 如何在 jinja2 过滤器中使用嵌套 if

ubuntu - 使用 Sublime Text 编辑我用 vagrant 创建的本地服务器上的文件的解决方案?

variables - 如何解析 Ansible group_vars 字典中的变量?

ansible - 如何避免 Ansible 上的 ask-vault-pass 参数

mongodb - Centos 7.2 上的防火墙配置

docker - 阻止外部访问Docker容器

python - 当我通过 ssh 进入 vagrant box 时,如何自动激活 Python venv?

php - 在 Vagrant Server 中创建虚拟主机