0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CentOS7にMySQLにインストールしたら最初にやること

Posted at

環境

$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

$ mysql --version
mysql  Ver 8.0.25 for Linux on x86_64 (MySQL Community Server - GPL)

MySQLのバージョンに注意しよう

mysqlの検索でよくヒットするはmysql5の方のバージョンだと思われます。
んでこれがまた重要なんですな。

どうやら5以降でコマンドが変わっていたりするので、見つけた記事のいコマンドをそのまま書いてもSytax Errorとかなってまう。

ちなみにCentOS7のコマンドライン操作でMySQLのインストール手順のメモはこちら
https://qiita.com/mako0104/items/f6a21a23a01b02dd8fdf

CUIってわかってくるとめっちゃ楽しい\(^o^)/

手順

  1. rootのパスワードを確認
  2. rootでログイン
  3. パスワードポリシーを低くする
  4. わかりやすいパスワードを設定する
  5. ログアウトして新しいパスワードでログイン
  6. ユーザー登録
  7. 権限付与

1. rootのパスワードを確認

$ sudo cat /var/log/mysqld.log | grep password
2021-05-16T01:10:01.338785Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: =Nt;,33Q+&&s
2021-05-17T11:59:10.774167Z 8 [Warning] [MY-013360] [Server] Plugin sha256_password reported: ''sha256_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'

// 1行目のroot@localhost:の後が初期設定のパスワード
// メモメモしましょう

2. rootでログイン

上のパスワードを使えばrootユーザーでログインできます

$ sudo mysql -u root -p
Enter password:[ココにさっきパスワードを入力] // 何も反応はないけど入力できているよ

// 正しくパスワードを入力したら以下のようなメッセージ
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> // ←これが表示されたらSQLが実行できるよ

3.パスワードポリシーを低くする

開発用に単純なパスワードを設定したいけど、デフォルトの設定だとできない。
気合の入ったポリシーになっている。

  • 8文字以上にせなあかん
  • 大文字とか記号とかを入れなあかん

など。

ちょいと面倒な気がするので、rootをrootで入りたい。。。ほんまはあかんのかな?でもローカルならいいよね!

ポリシーを低めにしちゃいましょ

// どんな設定になってんのかなって見るとき
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 8      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | MEDIUM |
| validate_password.special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.00 sec)

127.0.0.1 - makoto@localhost_~ VT 2021-05-17 22.25.43.png

設定の変更

// これだとエラー
mysql> set global validate_password_length=4;
ERROR 1193 (HY000): Unknown system variable 'validate_password_length'

// lengthの前は.でしたね、上の表では
mysql> set global validate_password.length=4;
// policyも変更
mysql> set global validate_password.policy=low;
Query OK, 0 rows affected (0.00 sec)

他の設定も全部0にしたけど「root」ではダメでした。

4. わかりやすいパスワードを設定する

rootにはできなかったので、root..に設定

mysql> alter user root@localhost identified with mysql_native_password by 'root..'
// このあたりのコマンドがMySQL5と違ったりする
mysql> set password root@localhost='root'; // MySQL5のバージョン

5.ログアウトして新しいパスワードでログイン

mysql> exit

$ mysql -u root -p
Enter password: (root..を入力)

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create user 'makoto' identified by 'makoto';
Query OK, 0 rows affected (0.01 sec)

// 入れた!!

6.ユーザー登録

mysql> create user 'takeshi' identified by 'takeshi';
Query OK, 0 rows affected (0.01 sec)
// できた!

7.権限付与

ユーザーと権限の確認

mysql> select user, host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| takeshi          | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

takeshiにすべての権限を与えたもう。


mysql> grant all on *.* to `takeshi`@`%`;
Query OK, 0 rows affected (0.01 sec)

以上でひとまず使いやすくなる

最初はログインできずにびっくりしたけど、焦らずに初期設定をします!!

なにか間違いがありましたらご指摘よろしくおねがいします\(^o^)/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?