Skip to content

Commit 6526b44

Browse files
authored
Merge pull request geerlingguy#131 from geerlingguy/fix-root-pw-57
Update secure-installation for correct root password in 5.7+.
2 parents a892a5c + 8c8240d commit 6526b44

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

tasks/secure-installation.yml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
---
2+
- name: Get MySQL version.
3+
command: 'mysql --version'
4+
register: mysql_cli_version
5+
changed_when: false
6+
27
- name: Disallow root login remotely
38
command: 'mysql -NBe "{{ item }}"'
49
with_items:
510
- DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')
6-
changed_when: False
11+
changed_when: false
712

813
- name: Get list of hosts for the root user.
914
command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = "root" ORDER BY (Host="localhost") ASC'
@@ -13,12 +18,21 @@
1318

1419
# Note: We do not use mysql_user for this operation, as it doesn't always update
1520
# the root password correctly. See: https://goo.gl/MSOejW
16-
- name: Update MySQL root password for localhost root account.
21+
# Set root password for MySQL >= 5.7.x.
22+
- name: Update MySQL root password for localhost root account (5.7.x).
23+
shell: >
24+
mysql -u root -NBe
25+
'ALTER USER "{{ mysql_root_username }}"@"{{ item }}" IDENTIFIED WITH mysql_native_password BY "{{ mysql_root_password }}";'
26+
with_items: "{{ mysql_root_hosts.stdout_lines }}"
27+
when: ((mysql_install_packages | bool) or mysql_root_password_update) and ('5.7.' in mysql_cli_version.stdout)
28+
29+
# Set root password for MySQL < 5.7.x.
30+
- name: Update MySQL root password for localhost root account (< 5.7.x).
1731
shell: >
1832
mysql -u root -NBe
1933
'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}");'
2034
with_items: "{{ mysql_root_hosts.stdout_lines }}"
21-
when: mysql_install_packages | bool or mysql_root_password_update
35+
when: ((mysql_install_packages | bool) or mysql_root_password_update) and ('5.7.' not in mysql_cli_version.stdout)
2236

2337
# Has to be after the root password assignment, for idempotency.
2438
- name: Copy .my.cnf file with root password credentials.

0 commit comments

Comments
 (0)