Skip to content

Commit

Permalink
feat: use socket authentication for root account on linux modern mysq…
Browse files Browse the repository at this point in the history
…l versions

Since MariaDB 10.4 / MySQL 8.4, the root mysql user is configured to use
`unix_socket` instead of password authentication. No password is privisioned
at MariaDB installation time for the root user.

Using socket authentication is recommended and considered a good security
practice.
cf. https://mariadb.com/kb/en/authentication-from-mariadb-10-4/
cf. https://dev.mysql.com/doc/refman/8.4/en/native-pluggable-authentication.html

Fix geerlingguy#550
Fix geerlingguy#522
Fix geerlingguy#431
Fix geerlingguy#421
  • Loading branch information
Al-thi committed Jan 31, 2025
1 parent a2e1442 commit b9c5683
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
16 changes: 15 additions & 1 deletion molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
ansible.builtin.command: "mysql -u root -proot -e 'show databases;'"
changed_when: false

- name: Make sure we can connect to MySQL via TCP.
- name: Make sure we can connect to MySQL via TCP on old MySQL version.
ansible.builtin.command: "mysql -u root -proot -h 127.0.0.1 -e 'show databases;'"
register: result
changed_when: false
failed_when:
- result.rc != 0 and not (
ansible_system == "Linux" and (
(mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or
(mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb')
)
)
- result.rc == 0 and (
ansible_system == "Linux" and (
(mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or
(mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb')
)
)
19 changes: 18 additions & 1 deletion tasks/secure-installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,31 @@
check_mode: false
when: mysql_install_packages | bool or mysql_root_password_update

# Note: We do not use mysql_user for this operation, as it doesn't always update
# the root password correctly. See: https://goo.gl/MSOejW
# Set root password for MySQL >= 8.4 and MariaDB ≥ 10.4
- name: Update MySQL root authentication via socket for localhost (Linux, MySQL >= 8.4)
ansible.builtin.shell: >
mysql -u root -NBe
"ALTER USER '{{ mysql_root_username }}'@'{{ item }}'
IDENTIFIED {{ (mysql_daemon == 'mariadb') | ternary('VIA unix_socket', 'WITH auth_socket') }}; FLUSH PRIVILEGES;"
no_log: "{{ mysql_hide_passwords }}"
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
when:
- (mysql_install_packages | bool) or mysql_root_password_update
- ansible_system == "Linux"
- (mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or
(mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb')

# Note: We do not use mysql_user for this operation, as it doesn't always update
# the root password correctly. See: https://goo.gl/MSOejW
# Set root password for 5.7.x. ≤ MySQL < 8.4 and MariaDB ≥ 10.4
- name: Update MySQL root password for localhost root account (5.7.x ≤ MySQL < 8.4)
ansible.builtin.shell: >
mysql -u root -NBe
"ALTER USER '{{ mysql_root_username }}'@'{{ item }}'
IDENTIFIED WITH mysql_native_password BY '{{ mysql_root_password }}'; FLUSH PRIVILEGES;"
IDENTIFIED {{ (mysql_daemon == 'mariadb') | ternary('VIA', 'WITH') }} mysql_native_password
BY '{{ mysql_root_password }}'; FLUSH PRIVILEGES;"
no_log: "{{ mysql_hide_passwords }}"
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
when:
Expand Down
7 changes: 7 additions & 0 deletions templates/root-my.cnf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@

[client]
user="{{ mysql_root_username }}"
{% if ansible_system == "Linux" and (
(mysql_cli_version is version('8.4', '>=') and mysql_daemon == 'mysql') or
(mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb')
) %}
socket={{ mysql_socket }}
{% else %}
password="{{ mysql_root_password }}"
{% endif %}

0 comments on commit b9c5683

Please sign in to comment.