Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant Ansible CentOS 7 How to install MySQL 5.7 and change default password

I'm using Vagrant and Ansble on CentOS 7. I'm trying to install MySQL 5.7 but I have the problem when trying update MySQL password.

- 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

I have researched and found that MySQL 5.7 automatic generate a default password so my script failed. Would anyone help me resolve this issue?

like image 508
Tho Nguyen Avatar asked Oct 16 '25 04:10

Tho Nguyen


1 Answers

---
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
like image 85
hungtd Avatar answered Oct 18 '25 00:10

hungtd