LoginSignup
0
0

More than 5 years have passed since last update.

MySQL5.7をインストールしたときに初期パスワードを探してパスワードを変更するまで

Last updated at Posted at 2018-03-28

現象

インストールして、パスワードがわからないという自体に陥る。
初期パスワードは下記コマンドで取得が可能です。
cat /var/log/mysqld.log | grep "temporary password"

パスワードはさっさと変えましょう

パスワード必須文字数を短くする(自己責任でお願いします)

mysql -u root -p初期パスワード --connect-expired-password -e "SET GLOBAL validate_password_length=4;"

パスワードのセキュリティレベルを下げてしまおう(自己責任でお願いします)

mysql -u root -p初期パスワード --connect-expired-password -e "SET GLOBAL validate_password_policy=LOW;"

パスワードを変更する(下記では「root」に変更)

mysql -u root -p初期パスワード --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';"

おまけ:Ansible版はこちら

(どこかの記事を参考にしたのですが、失念してしまいました。すみません)

mysql.yml
---
- name: mysql install
  hosts: all
  become: true
  tasks:
    - name: remove mariadb-libs
      yum:
        state: absent
        name: mariadb-libs

    - name: install mysql repository
      yum:
        state: present
        name: http://dev.mysql.com/get/mysql57-community-release-el6-7.noarch.rpm
        validate_certs: yes

    - name: install yum-utils, mysql-python, mysql-community-server
      yum:
        state: present
        name: "{{ item }}"
      with_items:
        - yum-utils
        - MySQL-python
        - mysql-community-server

    - name: running and enabled mysqld
      service:
        name: mysqld
        state: started
        enabled: yes

    - name: check .my.cnf exists
      stat:
        path: /root/.my.cnf
      register: mycnf_file

    - name: get temporary password
      shell: cat /var/log/mysqld.log | grep "temporary password" | awk '{print $11}'
      register: mysql_default_password
      when: not mycnf_file.stat.exists

    - name: change password validation to the easy way
      shell: |
        mysql -u root -p'{{ mysql_default_password.stdout }}' --connect-expired-password -e "SET GLOBAL validate_password_length=4;"
        mysql -u root -p'{{ mysql_default_password.stdout }}' --connect-expired-password -e "SET GLOBAL validate_password_policy=LOW;"
      when: not mycnf_file.stat.exists

    - name: change root user password
      shell: |
        mysql -u root -p'{{ mysql_default_password.stdout }}' --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';"
      when: not mycnf_file.stat.exists

    - name: remove all anonymous user
      mysql_user:
        name: ''
        state: absent
        host: localhost

    - name: create database
      mysql_db:
        name: ansible_test
        state: present
        encoding: utf8mb4

    - name: create user
      mysql_user:
        name: vagrant
        password: vagrant
        priv: '*.*:ALL,GRANT'
        state: present

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

init_my.cnf.j2
[client]
user = root
password = {{ mysql_default_password.stdout }}
connect-expired-password
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0